A gorgeous woman with pink hair entered the cabin. "I wonder if all human women have hair like this," Amigo managed to think, before quickly starting to feel a bit uncomfortable under her gaze.
"Hi! My name is Eleanor Carrey. You can call me Ellie. I'm the navigator here on the Galactic Rush."
"Hi, Ellie," Amigo willed himself to say. He had no idea why his cheeks felt hot, as if there were an oil leak somewhere inside.
"I’m going to explain the most interesting part of the entire Java language: variables."
"I'm ready to listen. What are these variables you speak of?"
"Variables are special entities used to store data. Any data. In Java, all data is stored in variables. The closest analogy here is a box."
"A box? What box?"
"Just any old box. Suppose you write number 13 on a piece of paper and put it into the box. Now we can say that the box is storing the value 13."
"In Java, every variable has three important properties: type, name, and value."
"Can you clarify what that means?"
"Sure. We use a name, so we can distinguish one variable from another. It’s like a label on a box."
"A variable’s type determines the kinds of values/data that can be stored in it.We put a hat into a hatbox, shoes into a shoebox, etc."
"The value is the specific object, data, or information stored in the variable."
"Can you tell me more about types?"
"Sure. Every object in Java has a certain type. Some examples include integer, fraction, text, Cat, House, etc."
"A variable also has a type. It can only store values whose type is the same as its own."
"You can see this in real life. Different kinds of boxes are used to store different things:"
"To create (or declare) a variable, we use the name of the type: «type name» «variable name»"
Here are some examples:
|To declare a variable:|
first the type, then the name.
|1||Create an int variable named a.|
|2||Create a String variable named s.|
|3||Create a double variable named c.|
"The two most common types are integers (declared as int) and text (declared as String)."
"What is a double?"
"Doubles are fractional, or real, numbers."
"You said a variable has three properties: type, name, and value. But I can only see two. So, my question is, how do you assign a value to a variable?"
"Let’s go back to our box analogy. Imagine that you take a piece of paper, write the number 42, and put it into the box. Now the box stores the value 42."
"We use a special operation (assignment) to assign values to variables. Assignment copies values from one variable into another.
It doesn’t move values, but rather it copies them. Like a file on a disk.
This is how it looks:"
|1||Assign the value 3 to variable i.|
|2||Assign the value 1 to variable a.|
Assign the value 2 to variable b.
|3||Assign the value 3 to variable x.|
In the next line, the value of x increases by 1, making x equal to 4
"To perform the assignment operation, we use the equal sign «=»."
"I'll say it again: This isn’t making a comparison. We are copying the value on the right of the equal sign to the variable on the left.
To perform a comparison, Java uses a double equal sign «==»."
"I know how to put a cat into a variable. It’s almost like a program."
"How to Trap a Cat:"
1. Take an empty box.
"No, Amigo. You can only squeeze one cat into a box. Uh, I mean you can only assign one value to a variable."
"I see. Could you give me more examples of creating variables?"
"OK. Let me repeat: to create «or declare» a variable, you need use the name of the variable's type."
String variable named s is created.
This variable can store text.
int variable named x is created.
This variable can store integers.
int variables named a, b, c and d are created.
These variables can store integers.
"Oh, now I see!"
"Keep in mind that you can’t create two variables with identical names in the same method."
"How about in different methods?"
"Yes, you can do that. That’s like having boxes at different houses."
"Can I name a variable anything I like?"
"Almost. Variable names cannot contain spaces, +, -, etc.
It’s best to just use letters and numbers in a variable’s name."
"Remember that Java is case-sensitive. «int a» is not the same as «Int a»."
"By the way, in Java you can create a variable and simultaneously assign a value to it. This saves time and space."
|Compact code||Equivalent but longer code|
a = 5;
b = 6;
int c = 7;
int d = c + 1;
c = 7;
d = c + 1;
s = "I'm Amigo";
"That way is a lot more compact and clear."
"That’s how we roll."
"There are two types every Java novice needs to become familiar with. int (integers) and String (text/strings)."
"The int type lets you store numbers in variables and perform operations on them: addition, subtraction, multiplication, division, etc."
int x = 1;
int y = x * 2;
int z = 5 * y * y + 2 * y + 3;
|x equals |
равен 20 + 4 + 3, equals
int a = 5;
int b = 1;
int c = (a - b) * (a + b);
|a equals |
4 * 6, equals
int a = 64;
int b = a / 8;
int c = b / 4;
int d = c * 3;
|a equals |
"Got it. Is programming always this easy?"
"Nice! So, what’s next?"
"The String type lets you store lines of text, also known as «strings»."
"To assign a string in Java, you need to place the text inside quotation marks. Here are some examples:"
«123 + 456».
"Got it. It doesn’t look very hard."
"Here’s one more fun fact for you."
"You can join strings together with a
+ sign. Look at these examples."
String s = "Amigo" + " is the best";
«Amigo is the best».
s contains an empty string – a string with no symbols at all.
int x = 333;
String s = "Amigo" + x;
"So, you can add strings to numbers?"
"Yes, but remember that when you add strings and numbers, the result is always a string."
"I figured that out from your example."
"If you’re so smart, try figuring out how to display a variable on the screen."
"Hmmm. A variable? On the screen? Nothing comes to mind."
"Actually, it’s simple. To display something on the screen, we use a
System.out.println() command, and we pass whatever we want to print as an argument."
System.out.println("Ami" + "go");
String s = "Amigo";
String s = "Am";
System.out.println(s + "igo");
"A-ha! That makes everything much clearer."
"Great. Here are three more exercises for you."