CodeGym/Java Course/Java Syntax Zero/The int type: whole numbers

The int type: whole numbers

Available

1. The int type

If you want to store whole number in variables, then you need to use the int type.

The word int is short for Integer , which of course is a good hint that this type lets you store integer numbers.

Variables whose type is int are capable of storing integer numbers ranging from -2 billion to +2 billion. To be more precise, from -2,147,483,648 to +2,147,483,647.

Interesting fact

These decidedly non-round numbers are related to how the computer's memory is organized.

In Java, 4 bytes of memory are allocated for the int type. Each byte of memory consists of 8 bits. Each bit can only represent 2 values: 0 or 1. An int variable contains 32 bits and can represent 4,294,967,296 values.

Half of this range was set aside for negative numbers, and the other half for positive numbers. And that's how we get the range from -2,147,483,648 to +2,147,483,647.


2. Creating an int variable

The int type is for storing integers. To create a variable in code that can store integer numbers, you need to use a statement like this:

int name;
Declaring an int variable

Where name is the name of the variable. Examples:

Statement Description
int x;
An x integer variable is created
int count;
A count integer variable is created
int currentYear;
A currentYear integer variable is created

The case of the letters matters. That means the commands int color and int Color will declare two different variables.

And the commands Int Color and INT COLOR won't make any sense to the compiler, causing it to report an error. int is a special keyword for the integer type and it must be written in lowercase.


3. Shorthand for creating variables

If you need to create many variables of the same type in the same place in a program, you can use this shorthand notation:

int name1, name2, name3;
Shorthand for creating multiple variables of the same type

Examples:

Statements Shorthand
int x;
int y;
int z;
int x, y, z;
int count;
int totalCount;
int count, totalCount;
int day;
int month;
int year;
int day, month, year;

4. Assigning values

To put a value into an int variable, you need to this statement:

name = value;
Assigning a value to a variable

Where the value can be any integer expression. Examples:

Statement Note
int a;
a = 5;
int b;
b = 2*1000*1000*1000;
int c;
c = -10000000;
int d;
d = 3000000000;
This code won't compile, because 3,000,000,000 is greater than the maximum possible value for an int, which is 2,147,483,647

5. Shorthand for creating and initializing a variable

You can use a single command to create (declare) a variable and assign a value to it. This is what is done most often, since we usually declare a variable when we need to store a value.

Here's what the command looks like:

int name = value;
Shorthand for creating and initializing a variable

Examples:

Statement Note
int a = 5;
int b = 2*1000*1000*1000;
The value of the variable will be 2 billion
int c = -10000000;
The value of the variable will be negative 10 million
int d = 3000000000;
This code won't compile, because 3,000,000,000 is greater than the maximum possible value for an int: 2,147,483,647

You can also declare several variables in a single line. In this case, the command will look like:

int name1 = value1, name2 = value2, name3 = value3;
Shorthand for creating and initializing multiple variables

Examples:

Statement Note
int a = 5, b = 10, c = a + b;
a equals 5, b equals 10, c equals 15
2
Task
New Java Syntax,  level 2lesson 3
Locked
We solemnly publish and declare these variables...
Declare int variables called a and b. In the same line in which the variables are declared, immediately assign them different values. The values can be any integers.
2
Task
New Java Syntax,  level 2lesson 3
Locked
A variable on our screen
Declare a String variable called name. In the line where we declare the variable, immediately assign it some value. Display the variable "name" on the screen.
2
Task
New Java Syntax,  level 2lesson 3
Locked
One variable is not enough
Declare int variables a and b and String variable s. In the same line in which the variables are declared, immediately assign them any different values.
2
Task
New Java Syntax,  level 2lesson 3
Locked
Display the square of a number
The variable number is given. Write a program that displays the square of this variable: (number * number).
Comments (20)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Meet Patel
Level 2 , CodeGym University in India, India
12 January 2024, 09:07
public class Unlock{ public static void man(String[] atgs){ int intValue= 20 , intNumeric=40, size=33 , number=10; } }
sophia slorach
Level 2 , Mexico
18 November 2023, 14:40
I don't understand the instructions of the task quite well
Anonymous #11353136
Level 2 , Federal Way, Australia
26 July 2023, 13:12
Cool
15 July 2023, 10:02
Cool
Maahin panchal
Level 2 , India
4 June 2023, 06:42
if I take 1 month subscription then I will get all things ?
John Squirrels Website Admin at CodeGym
6 June 2023, 11:11
Yes, you will get a full access for 30 days.
Varsha K
Level 2 , Canada
15 March 2023, 02:40
$99 for a month?
Anonymous #11313983 Software Developer
15 March 2023, 12:01
That is for premium pro. If you took advantage of the promotion, it is $99 a year for premium.
kautilya Kumar
Level 2 , India
2 January 2023, 06:48
can I do remote job while learing java?
Abdul Wahid Kohgadai
Level 2 , United States of America, United States
21 August 2022, 15:49
$99 for a year or month?
nocelotflame
Level 2 , United States of America, United States
21 September 2022, 02:59
for a month ;/
Leho
Level 3 , Philippines
26 December 2022, 14:41
you can buy $25 too for a month
Anonymous #11007571
Level 2 , Hillsboro, United States
3 June 2022, 20:29
What if i dont got that money
jozef898x
Level 2 , United Kingdom, United Kingdom
10 July 2022, 12:14
same
atb2199
Level 19 , United Kingdom, United Kingdom
16 August 2022, 14:05
99$ for a year is not much... and it's worth it. The same thing at college will cost you thousands of bucks!
Learningjavascript
Level 2 , United Kingdom, United Kingdom
20 August 2022, 10:22
Yeah no I can't afford that xd
Margarita Kholostova
Level 4 , United States of America, United States
21 August 2022, 17:54
I didn't find a better place to learn it. If you don't have money you can always download free books and learn Java the other way.
Klyner
Level 2 , Canada
30 September 2022, 13:52
its per month not year
micahrobertson29
Level 2 , United Kingdom, United Kingdom
24 October 2022, 09:09
Bro there is a difference between a full blown college and a coding website. Ive got like 3 dark matter there is no way im paying $99 for 2 more
Anonymous #11156462
Level 2 , Canada
26 October 2022, 03:05
Well sometimes they have a sale where it's 99 for a year when you first sign up, I just did that.