Variables

Java Syntax Zero
Level 1 , Lesson 3
Available

1. Variables and boxes

Variables are special things for storing data. Any data. All data in Java is stored using variables. One of the best ways to conceive of a variable is as a box: a completely ordinary box.

For example, let's say that you write the number 13 on a piece of paper and put it in a box. Now we can say that "the box stores the value 13".

Every variable in Java has three important properties: type, name, and value.

The name is used to distinguish one variable from another. It's like a label on a box.

The type of a variable determines the type of values/data that can be stored in it. We store a cake in a cake box, shoes in a shoe box, etc.

The value is some object or the data stored in the variable.

Every object in the Java language has its own type. For example, we can have the following data types: integer, fractional number, text, Cat, House, etc.

Each variable (box) also has its own type. A variable can only store values that correspond to its type. Different boxes are used to store different things: a box of chocolates, a carton for a dozen eggs, etc. It's just like in real life.


2. Creating a variable

In the Java language, we create a variable using a command that takes this form:

type name;
Declaring a variable

where type is the type of the variable (which corresponds to the type of the values that the variable can store), and name is the name of the variable.

Examples:

Creating a variable: first the type, then the name. Description
int a;
Create a variable named a whose type is int.
String s;
Create a variable named s whose type is String.
double c;
Create a variable named c whose type is double.

The two types most commonly used are integers (denoted by int) and text (denoted by String). The double type is also popular. It represents fractional (real) numbers.


3. Assignment

As mentioned above, a variable has a name, type, and value. We already considered the name and type, but what about the value? How do I put a value into a variable?

To assign a value to a variable, we have the assignment operator. It copies a value from one variable to another. It does not move the value. It copies. Like a file on disk. Assignment looks like this:

name = value;
Assignment operator

where name is the name of the variable and value is the value that will be put into the variable. The value can be a literal value, the name of another variable, or even some expression that includes variables.

Examples:

Code Description
int i;
int a, b;
int x;
The i variable is created
The a and b variables are created
A x variable is created
i = 3;
The i variable is set to the value 3.
a = 1;
b = a + 1;
The a variable is set to the value 1.
The b variable is set to the value 2.
x = 3;
x = x + 1;
The x variable is set to the value 3.
On the next line, the value of x is increased by 1. x is now 4.

The assignment operator is the = symbol. This is not a comparison. It is nothing more or less than the command to copy the value to the right of the equals sign into the variable, which is on the left. For a comparison operation, Java uses double equals: ==.


4. Cats and boxes

How to catch a cat:

  1. Take an empty box.
  2. Wait.

That's a joke 🙂

Of course, you may be able to fit a dozen cats into a box, but only one value can be put into a variable. This is related to next task.


1
Task
New Java Syntax, level 1, lesson 3
Locked
It's cool to be a programmer!
Write a program that displays: "I think being a programmer is cool". Example output: I think being a programmer is cool
Comments (63)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
sharmila sharmila Level 1, India
30 July 2024
public class var{ public static void main (String[] args{ int a=15; int b=35; System.out.println(a+b); }}
Anonymous Level 2, Morocco
6 February 2024
public class addition { public static void main (String[] args) { int a=5,b=4; System.out.println(" 5 + 4 = " +(a+b)); } }
Anonymous #11438843 Level 2, Katy, United States
18 December 2023
public class Solution { public static void main(String[] args) { int a = 5, b = 10; System.out.println("a:" + a); System.out.println("b:" + b); } }
Anonymous #11446492 Level 2, Romania
3 January 2024
int a = 5; b = 10; // semicolon instead of comma
19 January 2024
Please, don't do that!! it's a comma between the two variables, like this: int a = 6, b = 12; System.out.println(a + b);
Camilo Mangq Level 17, Colombia
19 February 2024
What it is the ":" after the variables?
Mark Palmer Level 6, Brooklyn, United States
20 November 2023
"in the same line in which the variables are declared," that part threw me, I was trying to do both a and b on one line... phrasing is confusing
sophia slorach Level 2, Mexico
17 November 2023
public class Solution { public static void main(String[] args) { int a = 4; int b = a + 6; } }
Patrik H. Level 1, Zilina, Slovakia
16 November 2023
kind of stupid that i can't use "public int" but i have to do that kind of stuff for my school projects in BlueJ....
Anonymous #11411242 Level 2, Cambridge, United States
18 October 2023
Hi sir we can helping i want java language?
Khadija Level 1, United Kingdom
11 October 2023
What a cool place to learn :) im loving this interface and how easy the games are to understand
Akshay Nambiar Level 2, India
10 September 2023
In the example of 3. Assignment the last example showing x = 3; x= x+1 will show error because there cant be use of same variable name in same method. Always change the variable name, Instead we can write int x = 3; int x1 = x+1; System.out.println(x1); It will give the result. I request you to please teach correctly. Thank you.
13 September 2023
You are correct, but not completely. I entered the code in IntelliJ IDEA, and the single change needed to make the program work was declaring the x as an int. Without stating the x as an int, the IDE would not recognize the variable and would not let me run the code. Here is the simple fix to their code:         int x = 3;         x = x + 1;         System.out.println(x); The output is: 4 So you can use the same variable name (int x) repeatedly without problems. Keep in mind that our x is storing 3 at the beginning, and at the end of the code, x is storing 4 because we added the x = x + 1:  I hope I made it clear! Have a great time with Java! 😁
Parsa Level 83, Bangalore, India Expert
10 October 2023
Perhaps your original code had a small bug. x = x+1 works just fine.
Thaitwo Level 4, United States
1 September 2023
Let’s Go!