Have a question?
Message sent Close
View Categories

Primitive Data Types

What are data types in java?

  1. Primitive Data Types
  2. Reference/Object Data Type

  • Size: 1 byte (8 bits)
  • Range: -128 to 127
  • Usage: Useful for saving memory in large arrays where the memory savings matter.

📄
filename.js
September	Hours	Total Cost(LKR)	Tamary Work Details	Vinoth Work Details
  • Size: 2 bytes (16 bits)
  • Range: -32,768 to 32,767
  • Usage: Can be used to save memory in large arrays when the range of values is known to be limited.

📄
filename.js
short s = 1000;

System.out.println("Short value: " + s);  // Output: Short value: 1000

📄
filename.js
int i = 100000;

System.out.println("Int value: " + i);  // Output: Int value: 100000

📄
filename.js
int i = 100000;

System.out.println("Int value: " + i);  // Output: Int value: 100000

📄
filename.js
float f = 10.5f;

System.out.println("Float value: " + f);  // Output: Float value: 10.5

📄
filename.js
double d = 10.5;

System.out.println("Double value: " + d);  // Output: Double value: 10.5
  • Size: 1 bit (Java uses 1 byte internally)
  • Values: true or false
  • Usage: Used for simple flags that track true/false conditions.

📄
filename.js
boolean flag = true;

System.out.println("Boolean value: " + flag);  // Output: Boolean value: true
  • Size: 2 bytes (16 bits)
  • Range: 0 to 65,535 (unsigned)
  • Usage: Used to store any character.

📄
filename.js
char c = 'A';

System.out.println("Char value: " + c);  // Output: Char value: A
📄
filename.js
public class Main {

    public static void main(String[] args) {

        // byte example

        byte b = 100;

        System.out.println("Byte value: " + b);  

        // short example

        short s = 1000;

        System.out.println("Short value: " + s); 

        // int example

        int i = 100000;

        System.out.println("Int value: " + i); 

        // long example

        long l = 100000L;

        System.out.println("Long value: " + l);  

        // float example

        float f = 10.5f;

        System.out.println("Float value: " + f); 

        // double example

        double d = 10.5;

        System.out.println("Double value: " + d);  

        // boolean example

        boolean flag = true;

        System.out.println("Boolean value: " + flag);  

        // char example

        char c = 'A';

        System.out.println("Char value: " + c); 

    }

}
📄
filename.js
Byte value: 100
Short value: 1000
Int value: 100000
Long value: 100000
Float value: 10.5
Double value: 10.5
Boolean value: true
Char value: A