Java Primitive Data Types

Java Primitive Datatypes are the basic data types. Java support eight primitive data types



Java primitive data types

A data type is nothing but type of data. The broad classification of data are integer numbers, decimal numbers, text data, boolean data. Data types have predefined size in memory, each data limit is set based on those sizes. This memory is created while java program executes and destroyed at the end.

Below is the list of eight primitive data types in java

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


Java primitive data type default values

Sr. NoData TypeDefault Value
1byte0
2short0
3int0
4long0
5float0.0f
6double0.0d
7char\u0000
8booleanfalse
9String & Java Objectsnull


Java primitive data type size

The primitive data type size is always same. It is platform independent. The size is measured using bits and bytes. One can choose the data types based on the range of values to use.

The below program shows the size of each primitive data types

package com.yawintutor;

public class JavaDataTypes {

	public static void main(String[] args) {
		// integer data types
		System.out.println("Integer data types");
		System.out.println("------------------");
		System.out.println("Size of byte    : " + (Byte.SIZE / 8) + " bytes.");
		System.out.println("Size of short   : " + (Short.SIZE / 8) + " bytes.");
		System.out.println("Size of int     : " + (Integer.SIZE / 8) + " bytes.");
		System.out.println("Size of long    : " + (Long.SIZE / 8) + " bytes.");

		// decimal number data types
		System.out.println("\nDecimal number data types");
		System.out.println("---------------------------");
		System.out.println("Size of float   : " + (Float.SIZE / 8) + " bytes.");
		System.out.println("Size of double  : " + (Double.SIZE / 8) + " bytes.");

		// text data types
		System.out.println("\nCharacter data types");
		System.out.println("----------------------");
		System.out.println("Size of char    : " + (Character.SIZE / 8) + " bytes.");

		// boolean data types
		System.out.println("\nBoolean data types");
		System.out.println("----------------------");
		System.out.println("Size of boolean : " + "1 bit.");

	}
}

Output

The below output shows the size of primitive data types in java

Integer data types
------------------
Size of byte    : 1 bytes.
Size of short   : 2 bytes.
Size of int     : 4 bytes.
Size of long    : 8 bytes.

Decimal number data types
---------------------------
Size of float   : 4 bytes.
Size of double  : 8 bytes.

Character data types
----------------------
Size of char    : 2 bytes.

Boolean data types
----------------------
Size of boolean : 1 bit.



Related Articles

Leave a Reply

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