Data Types In Java

Data Types define the values that can store variables in Java. Java is a statically-typed language . A language is statically-typed if variables data types know at compile time.This means that all variables must be declared before they can be used.

int salary;

Here we have declare variable of int data type.

    Table Of Content

There are two types of data types in Java:                                                                        Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.                                                                                                                        Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.

java data types
Java Data Types

Now will see data types in java with example .

Primitive Data Types

We have 8 primitive data types in java .

1. boolean data type

  • boolean datatype is used to store one bit information either true or false .
  • Default value : false.

boolean data type in java 

public class BooleanInJava {

	public static void main(String[] args) {
		boolean flag = true;
		if (flag) {
			System.out.println("flag  is true");
		} else {
            System.out.println("flag is false");
		}
		
	}
}

Output:

flag  is true

2. byte data type

  • byte data type in java is an 8-bit signed two’s complement integer.
  • Range of byte data type in java lies between -128 to 127 (inclusive). Its minimum value is -128 and maximum value is 127.
  • Default value is 0.
  • Default size: 1 byte
  • byte data type is used to save memory in large arrays . It can used in place of int

byte data type in java

public class ByteInJava {

	public static void main(String[] args) {
		byte counter =126;
		System.out.println(counter);
	}
}

Output :

126

3. short data type

  • short data type in java is an 16-bit signed two’s complement integer.
  • Range of short data type in java lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768 and maximum value is 32,767.
  • Default value is 0.
  • Default size: 2 byte
  • short data type is used to save memory in large arrays like byte. It can used in place of int

short data type in java

public class DataTypesInJava {

	public static void main(String[] args) {
		short counter =2000;
		System.out.println(counter);
	}
}

Output:

2000

4.  int data type

  • int data type in java is an 32-bit signed two’s complement integer.
  • Range of short data type in java lies between -2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is 2,147,483,648 and maximum value is 2,147,483,647.
  • Default value is 0.
  • Default size: 4 byte

int data type in java

public class DataTypesInJava {

	public static void main(String[] args) {
		int counter =5000;
		System.out.println(counter);
	}
}

Output :

5000

5. long data type

  • long data type in java is an 64-bit signed two’s complement integer.
  • Range of short data type in java lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1) (inclusive). Its minimum value is -9,223,372,036,854,775,808 and maximum value is 9,223,372,036,854,775,807.
  • Default value is 0.
  • Default size: 8 byte

long data type in java example 

public class DataTypesInJava {

	public static void main(String[] args) {
		long counter =9223372036854775801L;
		System.out.println(counter);
	}
}

Output :

9223372036854775801

Note : To store long values we need to use L at the end of values.

6. float Data Type

  • float data type in java is single-precision 32-bit IEEE 754 floating point.
  • It can hold upto 7 decimal digits.
  • Default value is 0.0(0.0f)
  • Default size: 4 byte
  • It can save memory if we use it instead of double

float data type in java example

public class DataTypesInJava {

	public static void main(String[] args) {
		float counter = 10.1f;
		System.out.println(counter);
	}
}

output :

10.1

Note in this example we are using 10.1f instead of 10.1 . Because 10.1 is double and 10.1f is float .

 7. double data type

  • double data type in java is single-precision 64-bit IEEE 754 floating point.
  • It can hold upto 16 decimal digits.
  • Default value is 0.0(0.0d)
  • Default size: 8 byte

double data type in java example

public class DataTypesInJava {

	public static void main(String[] args) {
		double counter = 10.1;
		System.out.println(counter);
	}
}

Output:

10.1

8. char data type

  • char data type in java is single 16-bit Unicode character.
  • Range of char data type in java is ‘\u0000’ (0) to ‘\uffff’ (65535).
  • Default value is ‘\u0000’
  • Default size: 2 byte

char data type in java example

public class DataTypesInJava {

	public static void main(String[] args) {
		char counter = 'A';
		System.out.println(counter);
	}
}

Output :

A

It stores character constants in the memory. It assumes a size of 2 bytes, because Java use Unicode system not the ASCII code system.

Non Primitive Data Types or Reference Types

Non-primitive data types are called reference types because they refer to objects. Examples of non-primitive types in java are Strings, Arrays, Classes, Interface, etc.

A Guide To Java

Java Doc

Leave a Reply

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

− 1 = 1