Java Day 4: Data Types & Variables

In Java, data types define the kind of values a variable can store. Java has two main categories:

1️⃣ Primitive Data Types

2️⃣ Non-Primitive Data Types


1. Primitive Data Types

These are built-in data types that store simple values. There are 8 primitive types in Java:

Whole Numbers (Integer Types)

Data Type Size Range
byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2³¹ to 2³¹-1
long 8 bytes -2⁶³ to 2⁶³-1

Decimal Numbers (Floating-Point Types)

Data Type Size Precision
float 4 bytes 6–7 decimal places
double 8 bytes 15 decimal places

Other Primitive Types

Data Type Size Stores
char 2 bytes Single character (Unicode)
boolean 1 bit true or false

Example:

byte b = 127;
short s = 3000;
int i = 100000;
long l = 1000000000L;
float f = 10.5f;
double d = 99.99;
char c = 'A';
boolean isJavaFun = true;

2. Non-Primitive Data Types (Reference Types)

  • These store references to objects, not actual values.
  • Examples: String, Arrays, Classes, Interfaces
  • Can be null (primitive types cannot).

Example:

String name = "Vignesh";
int[] numbers = {1, 2, 3, 4, 5};

Why are they called "Primitive" and "Non-Primitive"?

  • Primitive types store actual values in memory.
  • Non-primitive types store a reference (address) to the value.

3. Variable Declaration, Initialization, and Assignment

Declaration: Only declaring a variable (no value assigned).

int age;

Initialization: Assigning a value when declaring.

int age = 21;

Assignment: Changing the value later.

age = 25;

4. Global vs Local Variables

Global Variables (Declared outside methods, available globally)

✅ Use static keyword to access without an object.

public class Home {
    static String name = "Vignesh";
    static int age = 21;
}

Access them using ClassName.variableName:

System.out.println(Home.name); // Vignesh
System.out.println(Home.age);  // 21

Local Variables (Declared inside methods, only available within that method)

🚫 Local variables cannot be static because they belong to a method, not a class.

public class Test {
    public void display() {
        int num = 10;  // Local variable
        System.out.println(num);
    }
}

5. Variable Naming Rules

✔️ Can contain letters, digits, _ , $

✔️ Cannot start with a digit

✔️ No Java keywords allowed (like int, class)

✔️ Case-sensitive

int myAge = 20;
int MyAge = 25;  // Different variable

6. Identifiers in Java

  • Variables are called identifiers because they "identify" memory locations.
  • Examples: age, number, totalSalary are all identifiers.

7. Interesting Java Cases

Case 1: Mixed Data Types in Concatenation

System.out.println(25 + 69 + ", " + "Hello");

📌 Output: 94, Hello

🔹 Numbers are added first, then concatenated with the string.

Case 2: String First in Concatenation

System.out.println("Hello" + 25 + 69);

📌 Output: Hello2569

🔹 When a string appears first, everything after is treated as a string.

Case 3: Byte Overflow

byte no = 127;
no = 128;  // Error: Exceeds byte range (-128 to 127)

🔹 Solution: Use short, int, or long.


8. More Variable Access Examples

public class Home {
    static String name = "Vignesh";
    static int age = 21;
}

Accessing Static Variables

System.out.println(Home.age);      // 21
System.out.println(Home.name);     // Vignesh
System.out.println(Home.name + "45" + 66);  // Vignesh4566

Variable Reassignment

int no = 120;
no = 1233;   // Allowed (variable can change)

Conclusion

✅ Java has primitive (simple values) and non-primitive (object references) types.

Global variables use static, while local variables cannot be static.

String concatenation follows order, and byte overflow occurs if exceeded.

✅ Understanding variables helps in memory management and code efficiency.


*Reference

1.Classroom notes
2.Chat gpt.