A woman with pink hair entered the cabin. "I wonder if all human women have hair like this," Amigo managed to think.

"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.

"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 kind of box?"

"Just any old box. Suppose you write the number 13 on a piece of paper and put it into a 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, fractional number, 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: TypeName variableName."

"Here are some examples:"

To declare a variable:
first the type, then the name.
int a;
Create an int variable named a.
String s;
Create a String variable named s.
double c;
Create a double variable named c.

"The two most common types are integers (declared using the word int) and text (declared using the word 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."

"I see."

"We use a special operation (assignment) to assign values to variables. Assignment copies values from one variable into another. It doesn’t move values. It copies them. Like a file on a disk. This is how it looks:"

Code Description
i = 3;
Assign the value 3 to variable i.
a = 1;
b = a+1;
Assign the value 1 to variable a.
Assign the value 2 to variable b.
x = 3;
x = x + 1;
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.

2. Wait."

"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 «TypeName variableName»."

Code Explanation
String s;
A String variable named s is created.
This variable can store text.
int x;
An int variable named x is created.
This variable can store integers.
int a, b, c;
int d;
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
int a = 5;
int b = 6;
int a;
a = 5;
int b;
b = 6;
int c = 7;
int d = c+1;
int c;
c = 7;
int d;
d = c+1;
String s = "I'm Amigo";
String s;
s = "I'm Amigo";

"That way is a lot more compact and clear."

"That's how we roll."

"There are two types that 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."

Code Explanation
int x = 1;
int y = x*2;
int z = 5*y*y + 2*y + 3;
x equals 1
y equals 2
z equals 20+4+3, which equals 27
int a = 5;
int b = 1;
int c = (a-b) * (a+b);
a equals 5
b equals 1
c equals 4*6, which equals 24
int a = 64;
int b = a/8;
int c = b/4;
int d = c*3;
a equals 64
b equals 8
c equals 2
d equals 6

"Got it. Is programming always this easy?"

"Actually, yes."

"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:"

Code Explanation
String s = "Amigo";
s contains "Amigo".
String s = "123";
s contains "123".
String s = "123 + 456";
s contains "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 plus sign (+). Look at these examples."

Code Explanation
String s = "Amigo" + " is the best";
s contains "Amigo is the best".
String s = "";
s contains an empty string – a string with no symbols at all.
int x = 333;
String s = "Amigo" + x;
s contains "Amigo333".

"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."

Code Screen output
String s = "Amigo";
String s = "Am";

"A-ha! That makes everything much clearer."

"Great. Here are three more exercises for you."