Practice English Speaking&Listening with: [Intro, Video 3B] Static Typing

Difficulty: 0

So let's try doing something a little more complicated.

I'm going to write a program over here

called Hello numbers dot py.

What it's going to do is it's going

to print all the numbers between 0 and 10.

So while x is less than 10.

x equals x plus 1.

And we'll also print out x.

We run this.

Here they are-- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

Let's try writing that same program in Java.

So we're going to call this Hello numbers dot Java.

And what I'm going to do is name my class Hello numbers,

and I'll put my code here.

Now you notice in my text editor I

had some funny thing that happened more like all

that stuff popped up at once.

That's a little trick in this text editor I'm using.

If you're curious, there will be a link in the book that

will show you how to do that.

But it's not necessary for this class.

And in fact, starting next week, we're

going to encourage you to use not a text editor

to use something called an IDE.

We'll talk about that later.

So here we have a class called Hello numbers.

And in Java, what I'm going to do

is I'm just going to start naively

and I'm going to try and guess how it works.

So I might say, while x is less than 10,

we'll add a curly braces to delimit the beginning and end.

Because that's what we do in Java.

Then I'll say, system doc out dot print line x.

And then I'll say x equals x plus 1.

So I try running this code over here, I get error,

cannot find symbol x.

Now while you might originally be

tempted to say OK, well, maybe it's too small.

Just increase the font size.

You'll see that doesn't work.

You also can't do things like this,

you know, like help the compiler find the symbol.

That's just going to confuse it even more.

Actually, what it is trying to tell

you is that you should be declaring your variable.

That's something we need to do in Java.

I'm not sure if I got that size right.

Close enough.

So what it's saying is before a variable could be used,

you have to specify that it exists.

So I could do that by saying, for example, int x.

So this is a variable declaration that says one,

x exists and two, it's an integer.

So if I now run this code, I get the same behavior out

of my Java program.

And just kind of a minor seeming thing,

but it ends up having really big consequences for the way

that we're going to write code in Java.

So just to show you a shorthand, you can also if you want just

write out int x equals 0.

So that's all in one line.

So in other words you're declaring and setting

the variable at the same time.

And that'll work fine.

It's all the same.

So there are some big differences

between Java and Python in terms of how variables work.

With Python or other languages that

are not so-called statically typed languages,

you can create variables willy nilly.

You can put whatever you want in them.

Know I can decide later that x is actually horse.

Run it, and you'll get 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, horse.

But if try and pull a similar trick in Java,

it won't work so well.

In this case, I get error, string cannot be converted

to int.

And even if you try and do something like this, now

it'll yell at you and say, well x is already defined.

So in Java, variables have a specific type.

And those types can never change.

And that's going to have really big consequences in that when

we're looking at our programs, especially when we start

talking about functions, you'll know

exactly what's in each type of variable

and what's more the compiler will actually check and make

sure all of the types are good before you even

run the program.

So let's make some notes.

Before Java variables can be used they must be declared.

Java variables must have a specific type.

Java variable types can never change.

Hello, Roy.

All right.

Fun stuff.

Now type errors are not unique to Java.

They also exist in languages like Python.

For example, in Python, it is not

valid to say print 5 plus horse.

And if you try running this Python program,

you'll get 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, horse.

And then we see an error appear.

So it's something that you can't do in Python.

But what's interesting about Java

is that when we try and run code that has a type error,

you'll notice that there's a big difference.

This code actually prints out a bunch of stuff

before getting to the type error,

whereas in Java, we get error incompatible types

and the program doesn't even run.

So what's interesting about Java's typing system

is that types are verified before the code even runs.

And that's a big difference between Java and Python.

Here, this code basically gets a certificate

of correctness in terms of types before the code runs.

And what that means is that one, that's fewer bugs that your end

users are going to run into, two, makes it easier

for you to debug.

And then a third aspect of static types

that's really nice is that while you're working with your code,

you know what everything should be,

that x should always be an integer.

And we'll see when we get to functions and especially

in the coming weeks why that's such an amazing and important

piece of the Java language.

The Description of [Intro, Video 3B] Static Typing