Day 2: Variables and Data Types
A variable is a name attached to a memory space that stores data. Java is a strongly typed language, so you must specify the data type when declaring a variable. Types are broadly divided into 8 primitive types and reference types.
Primitive Types
Java’s 8 primitive types store the value itself directly in memory.
public class PrimitiveTypes {
public static void main(String[] args) {
// Integer types
byte smallNum = 127; // 1 byte, -128 to 127
short mediumNum = 32000; // 2 bytes
int number = 2_000_000_000; // 4 bytes (most commonly used)
long bigNum = 9_000_000_000L; // 8 bytes, L suffix required
// Floating-point types
float pi = 3.14f; // 4 bytes, f suffix required
double precise = 3.141592653; // 8 bytes (default floating-point)
// Character type
char letter = 'A'; // 2 bytes, Unicode
char korean = '\uAC00'; // Korean characters supported
// Boolean type
boolean isJavaFun = true; // true or false
System.out.println("Integer: " + number);
System.out.println("Double: " + precise);
System.out.println("Char: " + letter);
System.out.println("Boolean: " + isJavaFun);
}
}
Variable Declaration and Initialization
Variables can be initialized at the time of declaration or assigned a value later.
public class VariableExample {
public static void main(String[] args) {
// Declaration with initialization
int age = 25;
String name = "John";
// Declaration first, initialization later
double salary;
salary = 3500.50;
// Constants: use the final keyword (value cannot be changed)
final int MAX_SCORE = 100;
final String COMPANY = "NITEU";
// MAX_SCORE = 200; // Compile error!
// var keyword (Java 10+): type inference
var message = "Type is inferred"; // Inferred as String
var count = 42; // Inferred as int
System.out.println(name + "'s age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Max score: " + MAX_SCORE);
}
}
Reference Types and String
Reference types store the memory address of an object. String is the most commonly used reference type.
public class ReferenceTypeExample {
public static void main(String[] args) {
// String is a reference type but gets special treatment
String greeting = "Hello";
String language = new String("Java");
// String comparison
String a = "hello";
String b = "hello";
String c = new String("hello");
System.out.println(a == b); // true (same literal pool)
System.out.println(a == c); // false (different objects)
System.out.println(a.equals(c)); // true (content comparison)
// null: default value for reference types (points to no object)
String empty = null;
// empty.length(); // NullPointerException!
}
}
Variable Scope
A variable is only valid within the block {} where it is declared.
public class ScopeExample {
// Class variable (static): shared across all instances
static int classVar = 10;
// Instance variable: separate for each object
int instanceVar = 20;
public static void main(String[] args) {
// Local variable: valid only within the method
int localVar = 30;
{
// Block variable: valid only within this block
int blockVar = 40;
System.out.println(blockVar); // Accessible
}
// System.out.println(blockVar); // Compile error!
System.out.println("Class variable: " + classVar);
System.out.println("Local variable: " + localVar);
}
}
Today’s Exercises
-
Personal Information Storage: Write a program that stores a name (String), age (int), height (double), and marital status (boolean) in variables and prints each on a separate line.
-
Data Type Size Comparison: Write a program that prints
Byte.MAX_VALUE,Short.MAX_VALUE,Integer.MAX_VALUE, andLong.MAX_VALUEto compare the maximum values of each integer type. -
String Equality Experiment: Experiment with the difference between
==and.equals(). Create two objects usingnew String("test"), compare them with both==and.equals(), print the results, and explain why they differ in comments.