CodeGym /Java Course /Module 1. Java Syntax /The int type: whole numbers

The int type: whole numbers

Module 1. Java Syntax
Level 2 , Lesson 5
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 2, lesson 5
Locked
Kiss my shiny metal rear actuator
For human students, the first program displays the phrase "Hello World!" Do you know what brutal robot programmers write? Just don't tell anyone: "Kiss my shiny metal rear actuator!" In this task, see what a real coding robot feels like by writing a program that will display this phrase on the scree
Comments (20)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Meet Patel Level 2, CodeGym University in India, India
12 January 2024
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
I don't understand the instructions of the task quite well
Anonymous #11353136 Level 2, Federal Way, Australia
26 July 2023
Cool
15 July 2023
Cool
Maahin panchal Level 2, India
4 June 2023
if I take 1 month subscription then I will get all things ?
John Squirrels Level 41, San Francisco, Poland
6 June 2023
Yes, you will get a full access for 30 days.
Varsha K Level 2, Canada
15 March 2023
$99 for a month?
Anonymous #11313983 Level 3, United States of America, United States
15 March 2023
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
can I do remote job while learing java?
Abdul Wahid Kohgadai Level 2, United States of America, United States
21 August 2022
$99 for a year or month?
nocelotflame Level 2, United States of America, United States
21 September 2022
for a month ;/
Leho Level 3, Philippines
26 December 2022
you can buy $25 too for a month
Anonymous #11007571 Level 2, Hillsboro, United States
3 June 2022
What if i dont got that money
jozef898x Level 2, United Kingdom, United Kingdom
10 July 2022
same
atb2199 Level 19, United Kingdom, United Kingdom
16 August 2022
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
Yeah no I can't afford that xd
Margarita Kholostova Level 4, United States of America, United States
21 August 2022
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
its per month not year
micahrobertson29 Level 2, United Kingdom, United Kingdom
24 October 2022
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
Well sometimes they have a sale where it's 99 for a year when you first sign up, I just did that.