In Java, data types define the kind of data a variable can hold. This helps the compiler allocate memory and ensure you don’t accidentally assign text to a number or vice versa.

At GoNimbus, we believe mastering data types is the key to writing bug-free, optimized Java programs.


📦 Two Main Categories of Data Types

Java data types are divided into:

CategoryDescription
PrimitiveBuilt-in types for simple values
Non-PrimitiveObjects and more complex structures

Let’s explore each.


1️⃣ Primitive Data Types

Java has 8 built-in primitive types:

TypeDescriptionSizeExample Value
intWhole numbers4 bytes25, -100, 0
floatDecimal numbers (single precision)4 bytes3.14f, -7.5f
doubleDecimal numbers (double precision)8 bytes99.99, -0.001
charA single character2 bytes'A', '9'
booleanTrue or false1 bittrue, false
byteSmall integers (-128 to 127)1 byte100, -50
shortMedium-range integers2 bytes5000, -1500
longLarge whole numbers8 bytes150000L, -99999L

Quick facts:

  • Use float for memory-efficient decimals (add f suffix)
  • Use long for big numbers (add L suffix)
  • Use boolean for condition flags and checks

👨‍💻 Example – Using Primitives

public class PrimitiveDemo {
  public static void main(String[] args) {
    int age = 25;
    float score = 95.5f;
    char grade = 'A';
    boolean passed = true;

    System.out.println("Age: " + age);
    System.out.println("Score: " + score);
    System.out.println("Grade: " + grade);
    System.out.println("Passed: " + passed);
  }
}

2️⃣ Non-Primitive Data Types

Also called reference types, these include:

TypeDescription
StringA sequence of characters (text)
ArraysA collection of values (fixed-size)
ClassesCustom objects and blueprints
InterfacesContracts that classes can implement

Let’s start with the most common one: String.


📘 String: Most Used Non-Primitive Type

String city = "Hyderabad";
System.out.println("City: " + city);

✅ Strings support powerful built-in methods:

System.out.println(city.length());       // Output: 9
System.out.println(city.toUpperCase());  // Output: HYDERABAD

📌 While String may feel like a primitive, it’s actually an object (with methods!).


🧠 GoNimbus Tips for Choosing the Right Type

  • Use int for counting items or IDs
  • Use float/double for prices, weights, scores
  • Use char for gender, grades, initials
  • Use boolean for true/false conditions
  • Use String for names, messages, text inputs

⚠️ Be Careful With Type Mismatch

int age = "twenty";   // ❌ Error – type mismatch
boolean active = "true"; // ❌ Error – needs true/false without quotes

Use appropriate types without quotes unless it’s a String.


Scroll to Top