CodeGym /Courses /JAVA 25 SELF /Variable Types

Variable Types

JAVA 25 SELF
Level 1, Lesson 4
Available

1. Declaring Variables

Let’s once again recall how to create variables. To create a variable, you need to write the following command: type name;

Examples:

Statement Explanation
String s;
A variable s of type String is created. Such a variable can store text.
int x;
A variable x of type int is created. Such a variable can store integers.
int a, b, c;
int d;
Variables a, b, c, d of type int are created. Such variables can store integers.
Important!

You cannot create two variables with the same name within the same block of code. However, in different blocks — you can. It’s like boxes sitting in different houses. More about code blocks — in the next lectures.

There are also restrictions on the variable name. On the one hand, it can be anything, but on the other, it cannot contain spaces, the characters +, -, etc. It’s best to use only Latin letters and digits in a variable name.

And as a reminder, in the Java language, case matters — whether you write uppercase or lowercase letters. int a is not the same as Int a.

By the way, in Java you can declare a variable and assign a value at the same time. This saves time and space:

Compact code Longer code equivalent to the one on the left
int a = 5;
int a; a = 5;
int b = 6;
int b; b = 6;
int c = 7;
int c; c = 7;
int d = c + 1;
int d; d = c + 1;
String s = "I'm Amigo";
String s; s = "I'm Amigo";

This is much more compact and clearer.

Now that we’ve figured out how to create variables, let’s get acquainted with two of the most commonly used types in Java. These are int (integers) and String (text/strings).

2. The int type

A variable of type int can store integers: 5, -10, 100500. You can also perform various arithmetic operations on int: addition, subtraction, multiplication, division, and others. Examples:

int x = 1;					//	x will equal 1
int y = x*2;				//  y will equal 2
int z = 5*y*y + 2*y + 3;	//	z will equal 20+4+3, which equals 27

int a = 5;					//	a will equal 5
int b = 1;					//  b will equal 1
int c = (a-b) * (a+b);		//  c will equal 4*6, which equals 24

int a = 64;					//	a will equal 64
int b = a/8;				//	b will equal 8
int c = b/4;				//  c will equal 2
int d = c*3;				//  d will equal 6

If you didn’t skip math in school, everything here should make sense 🧐

3. The String type

The String type allows you to store text strings. To specify a text string in Java, write the text and put double quotes on both sides. Example:

Code Explanation
String s = "Amigo";
s will contain the text Amigo
String s = "123";
s will contain the text 123
String s = "Bond 007";
s will contain the text Bond 007

Looks simple, right? If so, here’s another interesting fact.

In Java, strings can be concatenated using the plus sign — +. Example:

String s1 = "Amigo" + " the best";	//	s1 will contain `Amigo the best`
String s2 = "";						// 	s2 will contain an empty string — with no characters at all.

int x = 2025;
String s3 = "Amigo" + x;				// s3 will contain Amigo2025

Notice the last example: we added a string and a number. Here, too, it’s simple: the number will be converted into a string, then the two strings will be concatenated. When adding strings and numbers, the result is always a string.

4. Printing a Variable to the Screen

Everything seems obvious and elementary. So, can you guess right away which command can print a variable to the screen?

It’s actually simple. To output something to the screen, we use the command System.out.println(), passing what we want to print as the parameter.

System.out.println("Amigo");		// Amigo

System.out.println("Ami" + "go");	// Amigo

String s1 = "Amigo";
System.out.println(s1);				// Amigo

String s2 = "Am";
System.out.println(s2 + "igo");		// Amigo

I hope things are a bit clearer now. And whether you’ve understood everything correctly, we’ll check right away. Practice — the criterion of truth: only through practice can you verify how well you’ve understood everything.

1
Task
JAVA 25 SELF, level 1, lesson 4
Locked
Summing game scores 🎮
Summing game scores 🎮
1
Task
JAVA 25 SELF, level 1, lesson 4
Locked
Creating a User Profile 👤
Creating a User Profile 👤
1
Task
JAVA 25 SELF, level 1, lesson 4
Locked
Calculating the order cost 🛍️
Calculating the order cost 🛍️
Comments (9)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Muksud Alam Level 4, Bangladesh, Bangladesh
6 June 2026
step by step stride to a great kingdom
Anonymous #11780498 Level 2, Kiev, Greece
26 March 2026
totalCost is (itemPrice + itemCount) * discount ??? Right...
Anonymous #11782527 Level 2, Melbourne, Australia
31 March 2026
i thought it would be itemPrice * itemCount 😂
Anonymous #11758447 Level 3, Greater Sudbury, Canada
25 April 2026
You are right!
Anonymous #11769142 Level 1, Ambasamudram, India
6 March 2026
bad going
Leah Mae Suan Level 1
22 February 2026
😁
Axtonicx Level 1, Phoenix, India
18 October 2025
good going
Anonymous #11707549 Level 2, Atlacomulco, Mexico
17 October 2025
Hello World!
Phoebe Rei C. Bustamante Level 2, Makati, Philippines
8 October 2025
yes