Font Awesome Icons

Tutorial

Data Types in Java

A data type is an attribute of a variable which tells the compiler  how the programmer wishes to use the variable. It defines the operations that can be done on the data and what type of values can be stored in it.

Types of Data Types in Java

1)Primitive Data Types

 A primitive data type is pre-defined by the programming language. The size and type of variable values are specified.

2)Non-Primitive Data Types:

These data types are not  defined by the programming language but are created by the programmer itself.

1)Primitive Data Types

There are 8 data types in Java. They are as follows:

1)boolean data type

 A boolean data type contains a bit of information and can store only true or false values. This data type is used to track true/false conditions.

 

Example:

 

class BooleanExample{
public static void main(String args[])
{
boolean a = true;
boolean b = false;
System.out.println(a);
System.out.println(b);
}

}

2)byte data type

It stores whole numbers that lie between -128 to 127.   Its default value is 0.

Example:

 

class ByteExample {
public static void main(String[] args) {
byte a,b;
a = 127;
b=56;
System.out.println(a); // prints 127
System.out.println(b); // prints 56
}
}

3)char data type

This data type is used to store a single character. The character must be enclosed within single quotes, like ‘P’ or ‘p’.

Example:

 

class CharExample {
public static void main(String[] args) {

char ak = ‘J’;

char a = 65, b = 66, c = 67;
System.out.println(ak); // prints J

System.out.println(a); // Displays 65
System.out.println(b); // Displays 66
System.out.println(c); // Displays 67

}}

(a); /em.out.println(b); // Displays .out.println(c); // Displays

4)short data types

A short data type is greater than byte in terms of size and less than a integer. It stores the value that ranges from  -32,768 to 32767. The default size of this data type: 2 bytes.

Example:

 

class ShortExample {

public static void main(String[] args) {

short n=1231,

System.out.println(n); // prints the value present in n i.e. 1231

}

}

(a); /em.out.println(b); // Displays .out.println(c); // Displays

5)int data types

This data type can store whole numbers from -2147483648 to 2147483647. Generally, int is the preferred data type when we create variables with a numeric value.

Example:

class Intexample{

public static void main(String args[]){

int a=12356789;

System.out.println(a);//

}}

6)long data types

This data type is a 64-bit two’s complement integer. By default, the size of a long data type is 64 bit and its value ranges from -263 to 263-1.

Example:

class Intexample{

public static void main(String args[]){

long a=12356789L;

System.out.println(a);//

}}

7)float data types

This data type can store fractional numbers from 3.4e−038 to 3.4e+038. 

Example:

class Floatexample{

public static void main(String args[]){

float a=6.43f;

System.out.println(a);//

}}

8)double data types

The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. The double should end the value with a “d”:

Example:

class Doubleexample{

public static void main(String args[]){

double a=6.43d;

System.out.println(a);//

}}