No announcement
 

Primitive Data Types

Primitive data types are fundamental data types from which all other data types are constructed.

There are two categories of programming language.

  1. Statically typed language
    Each variable type and expression is known at compile time. Once a variable has been declared to be of a particular type, then it cannot hold value of any other data type. Example, Java, C++.
  2. Dynamically typed language
    A variable can hold data of different type. Example, Python.

Following are the primitive datatypes:

  1. byte
    The byte data type is an 8-bit signed two's complement integer.
    It has a minimum value of -128 and a maximum value of 127 (inclusive).
    Default value is 0
    Example. byte id = 100;
  2. short
    The short data type is a 16-bit signed two's complement integer.
    It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).
    Default value is 0
    Example. short result = 1000;
  3. int
    The int data type is a 32-bit signed two's complement integer, which has a minimum value of -2^31 and a maximum value of (2^31)-1.
    Default value is 0
    Example. int sum = 24000;
    In Java SE 8 and later, we can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of (2^32)-1.
    Use the Integer class to use int data type as an unsigned integer.
    Example. Integer result = 450000;
  4. long
    The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -2^63 and a maximum value of (2^63)-1.
    Default value is 0L
    Example. long summation = -234000L;
    In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of (2^64)-1.
    Example. Long proValue = 430040L;
  5. float
    The float data type is a single-precision 32-bit IEEE 754 floating point.
    Default value is 0.0f
    Example. float omega = 0.5671432f;
  6. double
    The double data type is a double-precision 64-bit IEEE 754 floating point.
    Default value is 0.0d
    Example. double pi = 3.141592653589793;
  7. boolean
    The boolean data type has only two possible values: true and false.
    Default value is false
    Example. boolean isVisible = true;
  8. char
    The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive)
    Example. char tag = 'p';

Example:

DataTypesDemo.java

package com.codingnous.tutorial;

public class DataTypesDemo {

	public static void main(String[] args) {
		byte id = 100;
		short result = 1000;
		int sum = 24000;
		long summation = -234000L;
		float omega = 0.5671432f;
		double pi = 3.141592653589793;
		boolean isVisible = true;
		char tag = 'p';
		System.out.println("Primitive data types.");
		System.out.println("byte value   : " + id);
		System.out.println("short value  : " + result);
		System.out.println("int value    : " + sum);
		System.out.println("long value   : " + summation);
		System.out.println("float value  : " + omega);
		System.out.println("double value : " + pi);
		System.out.println("boolean value: " + isVisible);
		System.out.println("char value   : " + tag);
	}

}

Output
Primitive data types. byte value : 100 short value : 1000 int value : 24000 long value : -234000 float value : 0.5671432 double value : 3.141592653589793 boolean value: true char value : p