Java Primitive Types (8 + 1)

Primitive types are fundamental data types in Java. In Java, primitive types refer to types that store actual data values, such as integers, floating-point numbers, characters, and boolean literals. They offer fast access and occupy less memory.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/58bfed14-c08b-42eb-a41b-d2f0254dd191/Untitled.png

Booleans

boolean isJavaFun = true;
boolean isFoodTasty = false;
System.out.println(isJavaFun);     // Outputs true
System.out.println(isFoodTasty);   // Outputs false

Characters

char myGrade = 'B';
System.out.println(myGrade);  //B

Numbers

  1. Integer types - ① byte, ② short, ③ int, ④ long

    ① byte

    byte myNum = 120;
    System.out.println(myNum); //120
    
    

    ② short

    short myNum = 5000;
    System.out.println(myNum); //5000
    
    

    ③ int

    int myNum = 120000;
    System.out.println(myNum); //120000
    
    

    ④ long

    long myNum = 12000000000L;
    System.out.println(myNum); //12000000000
    
    
  2. Floating-point types - ① float, ② double

    ① float

    float myNum = 5.75f;
    System.out.println(myNum); //5.75
    
    

    ② double

    double myNum = 19.99d;
    System.out.println(myNum); //19.99
    
    

    ③ Scientific Notation

    float f1 = 35e3f;
    double d1 = 12E4d;
    System.out.println(f1); //35000.0
    System.out.println(d1); //120000.0
    
    

    💡 A float variable can handle 6-7 digits, while a double variable can handle up to 15 digits. Therefore, double is considered safer.

String

String greeting = "Hello World";
System.out.println(greeting);  //Hello World

💡 The String type is commonly used and is often referred to as the 9th type. However, it is not a primitive type because it is a reference type.