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
- 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 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
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 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//Program demonstrate the int data type public class IntVariable { public static void main(String[] args) { int amount; //amount declared as int amount=400; //amount assigned 400 which is int int remaining; //remaining variable declared as int remaining= -30; //remaining variable assigned -30 which is int System.out.println("My account balance used to be $" + amount + " but it's now " + remaining); } } |
Output
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
1 2 3 4 5 6 7 8 9 10 11 |
//Program demonstrates how to declare and intiliaze int variables public class IntVariable2 { public static void main(String[] args) { int amount=400, remaining=-30; //'amount' and 'remaining' are both declared and initialized System.out.println("My account balance used to be $" + amount + " but it's now " + remaining); } } |
Output
You May Also Like…
Variables and Literals in Java
Variables are used in many situations. For Instance, game applications use variables to collect...
double data type in Java
The double data type is also a floating point number. It is called double because it has a...
The print and println methods in Java
The print() and println() methods are Java's predefined methods used to display text output on the...
0 Comments