You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Every program stores and manipulates data. Java is statically typed, which means every variable must be declared with a specific type before it can be used. This lesson covers primitive types, strings, type casting, and the var keyword.
int age = 25; // integer
double price = 19.99; // floating-point number
char grade = 'A'; // single character
boolean passed = true; // true or false
String name = "Alice"; // text (reference type)
A variable declaration has three parts:
type variableName = value;
| Convention | Example | Used For |
|---|---|---|
| camelCase | firstName, accountBalance | Variables and methods |
| PascalCase | BankAccount, StudentRecord | Classes and interfaces |
| UPPER_SNAKE_CASE | MAX_SIZE, PI | Constants (static final) |
Java has 8 primitive types built into the language:
| Type | Size | Range / Precision | Default | Example |
|---|---|---|---|---|
byte | 1 byte | -128 to 127 | 0 | byte b = 100; |
short | 2 bytes | -32,768 to 32,767 | 0 | short s = 30000; |
int | 4 bytes | -2^31 to 2^31 - 1 | 0 | int x = 42; |
long | 8 bytes | -2^63 to 2^63 - 1 | 0L | long l = 100_000L; |
float | 4 bytes | ~7 decimal digits | 0.0f | float f = 3.14f; |
double | 8 bytes | ~15 decimal digits | 0.0d | double d = 3.14159; |
char | 2 bytes | Unicode character (0 to 65,535) | '\u0000' | char c = 'Z'; |
boolean | 1 bit* | true or false | false | boolean ok = true; |
Tip: Use
intfor whole numbers anddoublefor decimals unless you have a specific reason to choose another type. Use underscores in numeric literals for readability:1_000_000.
String is not a primitive — it is a reference type (a class). However, Java provides special syntax for strings:
String greeting = "Hello"; // string literal
String fullName = "Alice" + " " + "Smith"; // concatenation
int length = greeting.length(); // 5
char first = greeting.charAt(0); // 'H'
String upper = greeting.toUpperCase(); // "HELLO"
boolean eq = greeting.equals("Hello"); // true
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.