Data Types in Java

In today’s topic, we will discuss various data types in Java. These are very important concepts that are necessary to remember before writing a Java program. Previously we also describe briefly about various topics of Java such as Features of Java, History of Java, Difference between C++ & Java, and many more. So without wasting time let’s dive into our topic:

Data Types in Java

Data types are basically used for declaring variables and functions of different types. It can define different sizes and values which are used to store variables. If you haven’t any idea about variables & constants then you must have to remember what is Variables & Constant in Java? It is a very important concept that is necessary to remember before starting the concept of data types.

In Java, data types are two types i.e.:

  1. Primitive Data Types (Intrinsic)
  2. Non-Primitive Data Types (Derived)

Primitive Data Types

There are 8 types of primitive data types in JAVA these are

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

Byte Data Type

  • Its value range from -128 to 127 i.e.: [ -(28)/2 to (28)/2 – 1 ]
  • It takes 1 byte of space.
  • Mainly byte data type is an 8-bit signed two’s complement integer which is used to store byte type to data that is four times smaller than an integer.
  • Its default value is 0 (Zero).

Example:-

public class GeektoCode {
	public static void main (String[] args){
		byte num = 5;
		System.out.Println("The byte number is "+num);
	}
}

Output:- The byte number is 5

Short Data Type

  • Its value range from –32768 to 32767 i.e.: [ -(216)/2 to (216)/2 – 1 ]
  • It takes 2 bytes of space.
  • Mainly the short data type is a 16-bit signed two’s complement integer which is used to store a short type of data that is two times smaller than an integer.
  • Its default value is 0 (Zero).

Example:-

public class GeektoCode {
	public static void main (String[] args){
		short num = 333;
		System.out.Println("The short number is "+num);
	}
}

Output:- The short number is 333

Int data type

  • Its value range from -4194304 to 4194303 i.e.: [ -(232)/2 to (232)/2 – 1 ]
  • It takes 4 bytes of space.
  • Generally, the int data type is a 32-bit signed two’s complement integer which is used to store integer values and it is a default data type for integers.
  • Its default value is 0 (Zero).

Example:-

public class GeektoCode {
	public static void main (String[] args){
		int num = 85963;
		System.out.Println("The int number is "+num);
	}
}

Output:- The int number is 85963

Long Data Type

  • Its value range from -9223372036854775808 to 9223372036854775807 i.e.: [ -(264)/2 to (264)/2 – 1 ]
  • It takes 8 bytes of space.
  • Mainly the long data type is a 64-bit two’s complement integer which is use when we need to store a long range of integer values.
  • Its default value is 0 (Zero).

Example:-

public class GeektoCode {
	public static void main (String[] args){
		long num = 385963423L;
		System.out.Println("The float number is "+num);
	}
}

Output:- The long number is 385963423L

Float Data Type

  • Its value range is unlimited.
  • It takes 4 bytes of space.
  • The float data type is a single-precision 32-bit IEEE 754 floating point it is used to store floating numbers including point numbers.
  • Its default value is 0.0F

Example:-

public class GeektoCode {
	public static void main (String[] args){
		float num = 38.6f;
		System.out.Println("The float number is "+num);
	}
}

Output:- The float number is 38.6f

Double data type

  • Its value range is also unlimited.
  • It takes 8 bytes of space.
  • The double data type is a double-precision 64-bit IEEE 754 floating point which is also used to store floating numbers with a long range of values.
  • Its default value is 0.0d

Example:-

public class GeektoCode {
	public static void main (String[] args){
		double num = 864.85;
		System.out.Println("The double number is "+num);
	}
}

Output:- The double number is 864.85

Char data type

  • Its value range is from 0 to 65535 i.e.: (216 – 1).
  • It takes 2 bytes of space.
  • The char data type is a single 16-bit Unicode character that is used to store character values.
  • Its default value is ‘\u0000’.

Example:-

public class GeektoCode {
	public static void main (String[] args){
		char c = ‘A’;
		System.out.Println("The character  is "+c);
	}
}

Output:- The character is A

Boolean data type

  • The value of the boolean data type can only be true and false.
  • It takes 1 bit of space.
  • It is only used for declaring variables with a true or false values.
  • Its default value is false.

Example:-

public class GeektoCode {
	public static void main (String[] args){
		boolean flag = true;
		System.out.Println("The boolean value is "+flag);
	}
}

Output:- The boolean value is true;

So these are all about primitive data types in Java. Now a question arises where is the non-primitive data type, so don’t worry non-primitive data type is also used to store value but unlike primitive data types non-primitive data types have various other functionalities which we will discuss in our next upcoming post so wait for it. I hope that today’s topic is really helpful to you. We will meet in the next post. Thank you.

Leave a Reply

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