Datatypes in Java
Data Types In Java
Data types are the types of data that can be stored inside variables. Java is a statically typed language, so all the variables must be declared before use.
Example
int age = 21;
Here age is variable, and int is a data type. int means it stores an integer value, i.e., numbers without a decimal point.
Data Types are categorized into:
- Primitive Data Types: byte, short, int, long, char, boolean, double
- Reference Data Types: String, Arrays, Classes
Primitive Data Types
Primitive data types are the data types that can be stored without the use of any other data types. It has no additional methods. They are:
Data Types | Description |
---|---|
byte | Stores whole numbers from -127 to 127. |
short | Stores whole numbers from -32,768 to 32,768. |
int | Stores whole numbers from -2,147,483,648 to 2,147,483,647. |
long | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854.,775,807 |
boolean | Stores either true or false. |
char | Stores single character from A-Z, a-z, and symbol. |
float | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits. |
double | Stores fractional numbers. Sufficient for storing 15 decimal digits. |
Example
public class Main {
public static void main(String[] args) {
// Declaring various data types.
byte bNum = 127;
short sNum = 32767;
int myNum = 2147483647; // Mostly Used
long lNum = 9223372036854775807L;
float fNum = 3.14f;
double dNum = 3.14; // Mostly Used
boolean isMarried = true;
char myCh = 'A';
// Displaying Result
System.out.println("The value of bNum is "+bNum);
System.out.println("The value of sNum is "+sNum);
System.out.println("The value of myNum is "+myNum);
System.out.println("The value of lNum is "+lNum);
System.out.println("The value of fNum is "+fNum);
System.out.println("The value of dNum is "+dNum);
System.out.println("The value of isMarried is "+isMarried);
System.out.println("The value of myCh is "+myCh);
}
}
Reference Data Types
Reference data type is a data type that is created by the programmer. It is a class, interface, or an array. It is created using the keyword new. It has additional methods. You will learn String, Arrays, and Classes later in this guide. It is also called reference type because it refers to an object.
Type Casting
Type casting is the process of converting one data type to another. It is also called type conversion. It is done to store a value of one data type into another data type. It is done in two ways:
-
Implicit Type Casting: It is done automatically when the value is stored in a variable of a larger data type. For example, if you store an int value into a long variable, it will be done automatically.
-
Explicit Type Casting: It is done manually by the programmer. For example, if you store a long value into an int variable, it will give an error. So, you have to convert it manually.
Example: Convert int to long
In this example, we will convert an int value to long.
public class Main {
public static void main(String[] args) {
int myInt = 9;
long myLong = myInt; // Automatic casting: int to long
System.out.println("Value of myInt is: "+myInt); // Outputs 9
System.out.println("Value of myLong is: "+myLong); // Outputs 9
}
}
Example: Convert long to int
In this example, we will convert a long value to int.
public class Main {
public static void main(String[] args) {
long myLong = 9;
int myInt = (int) myLong; // Manual casting: long to int
System.out.println("Value of myLong: "+myLong); // Outputs 9
System.out.println("Value of myInt: "+ myInt); // Outputs 9
}
}
Interview Questions
- What is type casting in Java? Why is it needed?
- What is the difference between a primitive and a reference type?
- Why Selection of correct type is important?
- Is the declaration of a below is correct? Explain.
short salary = 100000;