You are here: Home / Topics / Primitive Data Types in java

Primitive Data Types in java

Filed under: Java Tutorial on 2024-03-01 21:08:59

There are eight primitive data types that are supported in Java programming language. These are the fundamental and predefined data
types of the programming language. Java determines the size of each primitive data type, it cannot be changed. Reserved keywords represent primitive data types.

The primitive data type is divided into the following categories

  1. Integer data type
  2. Float data type
  3. Char data type
  4. Boolean data type


Integer data type

An integer data type stores integer numbers (number without a decimal point). The minimum and maximum value that a variable can store
depends on the size of the data type.
 

The range of Integer data types
 

The following table lists represent all integer data types, their storage requirements in bytes and the numeric range they support.

TypeSizeMin valueMax value
byte8 bit/1 byte-128127
short16 bits/2 byte-3276832767
int32 bits/4 byte-2,147,483,6482,147,483,647
long64 bit/8 byte-9,223,372,036,854,775,8089,223,372,036,854,775,807

Depending upon the requirement we must choose the appropriate data type.

byte: 

The size of the byte type is 8 bit/ 1byte. The lower limit value of the byte is -128 and the upper limit value is 127.

short: 

The short data type is an integer-compatible data type and is used to contain an integer value or an integer number. The size of the short type is 16 bit/2 byte. The minimum value of the short is -32768 and the maximum value is 32767.

int: 

The size of the int value is 32 bit/4 byte. The lower limit value of int is - 2147483648 and the upper range is 2147483647.

long: 

The size of the long data type is 64 bit/8 byte. While declaring a long data type always uses the suffix “L” by default Java considered it as an integer. Java programming language is a case-sensitive language so it is recommended use “L” in upper case.

Initialization of long variable:
long longMax = 9223372036854775807L;
long longMin = -9223372036854775808L;
L specifies that the value is long.

Floating Point data type

Floating point variables are used to deal with decimal values. Two types of floating point data types are there. The following table provides you details about floating point types with storage sizes and ranges of values.

TypeStorage SizePrecision
float4 byte7 decimal places
double8 byte16 decimal places

In the program, whenever we want to store a floating-point value in a variable, we can use one of these data types accordingly.

Float–float is used for floating- point (decimal) values, it occupies 32-bit memory

Initialization of float variable:

float var = 9.0
f specifies that value is a float.

Precision in float:

public static void main(String[] args) {
      float var = 10f / 6f;
      System.out.println(var);
}


public static void main(String[] args) {
     float var = 10f / 6f;
     System.out.println(var);
}


Output: 1.6666666
Double –double is used for high precision floating point values, it occupies 64-bit memory

Initialization of double variable:


double var = 9.5d;
d specifies that the value is double.
 

The precision of a double data type is higher than a float data type.


Precision in double: 


public static void main(String[] args) {
     double var = 10d / 6d;
    System.out.println (var);
}


public static void main(String[] args) {
     double var = 10d / 6d;
    System.out.println (var);
}


Output: 1.6666666666666667

boolean

Boolean is a data type which represents one bit of information either true or false.


public static void main(String[] args) {
    boolean var = true;
    System.out.println (var);
}


public static void main(String[] args) {
    boolean var = true;
    System.out.println (var);
}


Output: true

char

A char data type is a single-length entity. This could be an alphabet, a digit or a symbol. It is also used to hold the Unicode for symbols. Char’s Value should be within single quotes. 

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

Output:

$

A

Note: We must only use the single quotes pointing to left The length should be one.

A single quote point to the left should be used Any character on the keyboard is allowed Example: ‘A’,’*’,’1’

About Author:
J
Java Developer     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.