Select Page

int data type in Java


The int data type is an integer. int is short for Integer. int holds non-fractional or non-decimal values.


nanadwumor

May 27, 2023

Primitive Data types-int


  • The int data type is an integer.
  • int is short for Integer
  • int values are stored in Java using 32 bits of memory.

RECOMMENDED ARTICLES


Variables and Literals in Java

Variables and Literals in Java

Variables are used in many situations. For Instance, game applications use variables to collect your name when you sign in; to store and keep track of your number of trials, successes and failures. In Java, a variable is a container used to store...

double data type in Java

double data type in Java

The double data type is also a floating point number. It is called double because it has a double-precision decimal number.   The double data type is a floating point number. It is double precision data type The range is 4.9406564584124654 x...

The print and println methods in Java

The print and println methods in Java

The print() and println() methods are Java's predefined methods used to display text output on the console. These methods are part of the Java API. The print and println methods are used to print string of characters  onto the console The print...


The int data type is an integer. int is short for Integer. int holds non-fractional or non-decimal values.

int values are stored in Java using 32 bits of memory. That’s, an int value can hold values from -2,147,483,648  to 2,147,483,647.


Join Other Subscribers on Our YouTube Channel and Don’t Miss a thing!


Declaring an int variable

An int variable can be declared by writing int first and writing the variable name after space character.

int age;

int height;

int weight;

Alternatively, you can also declare all int variables in a row. That’s,

int age, height, weight;

Program demonstrates int data type

Output

Program demonstrates int data type

Explanation of code

Two variables, amount and remaining, are declared as int. The two are later assigned values. amount is assigned a positive value, 400 while remaining is assigned a negative number, -30.

Note that int values can hold negative numbers just like in math, integers do.

Alternatively, we could have declared amount and remaining on the same line because they’re all int data types.

int amount, remaining;

Besides, we could have initialized or set their values at the same time as we declared them.

int amount=400, remaining=-30;

Let’s take a look at a program that declared the similar data types on a line and initialize them at the same time to save us from typing too many characters.

Program demonstrates declaring int types on one line and initializing them

Output

Program demonstrates int data type

You May Also Like…


double data type in Java

double data type in Java

The double data type is also a floating point number. It is called double because it has a...



0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *