Data Type in Java

Data type: Data type is a way to categorize data.It is fundamental to programming language because they ensure the data is used correctly and efficiently.

Primitive data type:

Primitive data types are the fundamental building blocks of data in computer programming.They are predefined in programming language and don't need to be defined by the programmer.


byte:It is used to store a small whole number.

Example: byte b=-3, byte age=22


short: It is used to store a small whole number but its length can be more than byte.

Example: short distance=1200,short populationOfVillage=4000


Integer:It is used to store the whole numbers.

Example: int x=234, int y=-234


Long: It is used to store large whole numbers.

long pulationOfCountry=80000000000 L


Float: It is used to store floating point numbers with less precision than double.

Example: float f=45.563f, float area=345.1234f


double: It is also used to store floating point numbers with higher precision than float.

Example:double d=234.123456789


Character:It stores a single character,because it supports unicode.

Example:Char ch= 'C',Char ch= 'a'


boolean: It stores logical values(true or false).It is used for conditional testing.

Boolean isTrue=true , Boolean isFalse=false


Java offers eight primitive data types,each designed to store specific kinds of data efficiently.

Note:In floating point numbers “e” indicate the power of 10.
float f1=345e3f
double d1=654E65d

Non-primitive data types:

This is the data type created by programmers and not defined by java. These represent real world entities.


Value of the non-primitive data type is always null.

Non-primitive data types are also known as reference types,because they refer to objects.


String: A sequence of characters used for text data. Unlike primitive data types, strings are objects.

Example: String str=“Welcome to APNASTUDY”


short: It is used to store a small whole number but its length can be more than byte.

Example: short distance=1200,short populationOfVillage=4000


Array: Array is an ordered collection of items of the same data type.

Example: Array[] arr={1,2,3,4}


Class: Class is defined as a blueprint that defines the properties (attributes) and behavior (methods) of objects.

Example:
Class Student{
int age =12; //attributes
Void show() { // methods
} }


Object: Object is an instance of a class that encapsulates data (attributes) and actions (methods).


Interface: Interface in java is a blueprint that specifies the behavior of a class.It defines what a class can do,not how it does it.


type to another type. This is a process of manually converting a value from one data type to another.It is like forcing a value to fit into a container of different size.



In java there are two type of casting:

Widening casting(Automatically):

converting a smaller type of data to a larger one.

byte->short->char->int->long->float->double.


Example:
int myNum=23; double myDouble=myInt;
OUTPUT:23 to 23.0
Automatic casting int to double.


Narrowing casting(manually):

double->float->long->int->char->short->byte.


Example:
double myDouble=97.234d;
int myInt=(int) myDouble;
OUTPUT:9.78 to 9


Forcefully casting double to int.

Type conversion in java:

Type conversion in java is also known as coercion . Type conversion is performed by JVM implicitly during program execution.


Note: For type conversion in java the “instance of” operator is used.


Widening conversion(Safe) : During widening conversion you assign a smaller data type to a larger one.No data loss occurs in this method.


Narrowing conversion(Risky):During narrowing conversion you assign a larger data type to a smaller one.No data loss occurs in this method. This method might lead to loss of precision or even a compile-time error.

Previous Next