CodeGym /Courses /Java Syntax /Ellie, variables, and data types

Ellie, variables, and data types

Java Syntax
Level 0 , Lesson 4
Available

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


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

Ellie, variables, and data types - 2

"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.
Description
1
int a;
Create an int variable named a.
2
String s;
Create a String variable named s.
3
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
1
i = 3;
Assign the value 3 to variable i.
2
a = 1;
b = a+1;
Assign the value 1 to variable a.
Assign the value 2 to variable b.
3
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."

Ellie, variables, and data types - 3

"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
1
String s;
A String variable named s is created.
This variable can store text.
2
int x;
An int variable named x is created.
This variable can store integers.
3
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
1
int a = 5;
int b = 6;
int a; a = 5;
int b; b = 6;
2
int c = 7;
int d = c+1;
int c; c = 7;
int d; d = c+1;
3
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
1
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
2
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
3
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
1
String s = "Amigo";
s contains "Amigo".
2
String s = "123";
s contains "123".
3
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
1
String s = "Amigo" + " is the best";
s contains "Amigo is the best".
2
String s = "";
s contains an empty string – a string with no symbols at all.
3
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
1
System.out.println("Amigo");
Amigo
2
System.out.println("Ami"+"go");
Amigo
3
String s = "Amigo";
System.out.println(s);
Amigo
4
String s = "Am";
System.out.println(s+"igo");
Amigo

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

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

Comments (325)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Nisarga P Shetty Level 1, CodeGym University in India, India
12 September 2025
public class Solution { public static void main(String[] args) { int a=3126; System.out.println(a-8); //write your code here } }
Anonymous #11684176 Level 2, -, India
19 August 2025
public class Main { public static void main(String[] args){ int a = 18; for(int i = 0; i <= 10; i++){ System.out.println("amigo is very hardworking" + a); } } }
Anonymous #11678825 Level 3, Vishakhapatnam, India
4 August 2025
public class Solution { public static void main(String[] args) { System.out.println("Ellie is very smart"); System.out.println("Ellie is very smart"); System.out.println("Ellie is very smart"); System.out.println("Ellie is very smart"); System.out.println("Ellie is very smart"); } }
Anonymous #11677057 Level 2, Tiruchchirappalli, India
2 August 2025
public class Main { public static void main(String[] args) { int age = 10; String city = "karur"; System.out.println("age: " + age); System.out.println("city: " + city); } }
ivo Level 2, Bulgaria
9 July 2025

public class Main{
    public static void main(String[] args){
        int a = 23;
        for (int i = 0; i < 9; i++){
            System.out.println("humans are friends to the robots");
            System.out.println("and the robots are " + a);
        }
    }
}
khadija Level 1, Pakistan
3 July 2025
If we do some changes in the code after having an output ,it doesn't give us another output instead it says you're task has been completed already.Like why not to give an output again??
Anonymous #11667069 Level 2, Khandela, India
6 July 2025
yeahhh i've the same problem.
askme Level 1, Lahore, Pakistan
20 July 2025
same prob.
Anonymous #11662188 Level 2, Kissimmee, United States
18 June 2025
package en.codegym.task.jdk13.task01.task0102; /* Kind words for the teacher */ public class Solution { public static void main(String[] args) { System.out.println("Ellie is very smart"); System.out.println("Ellie is very smart"); System.out.println("Ellie is very smart"); System.out.println("Ellie is very smart"); System.out.println("Ellie is very smart"); } }
ivo Level 2, Bulgaria
9 July 2025
this is the better way to do it

public class Main{
    public static void main(String[] args){
        for (int i = 0; i < 5; i++){
            System.out.println("Ellie is very smart");
        }
    }
}
Anonymous #11662188 Level 2, Kissimmee, United States
18 June 2025
first one
G Venkata Prasanna Kumar Reddy Level 3, Vishakhapatnam, India
12 June 2025
System.out.println(3126-8)
comfort akika Level 5, Kaduna state , Nigeria
6 June 2025
What is a class
Koosty Level 1, Indonesia
9 July 2025
you can think class as a container for your code, although there is more than that.