Java Essentials ยท Month 1
Variables & Primitive Types
Goal: store data in variables, know the primitive types and convert between them. Duration: 90 min
1. What is a variable?
A variable is a named box in memory that holds a value. In Java every variable has a type, and the type never changes.
int age = 26; // type name = value;
String city = "Baku";
age = 27; // we can change the value...
// age = "old"; // ...but not the type โ compile error
Declaring and assigning separately:
int balance; // declaration
balance = 1000; // assignment (initialization)
2. The 8 primitive types
| Type | Size | Example | Use for |
|---|---|---|---|
byte |
8 bit | byte b = 100; |
raw data, files |
short |
16 bit | short s = 30000; |
rarely used |
int |
32 bit | int count = 42; |
default for whole numbers (ยฑ2.1 billion) |
long |
64 bit | long id = 9_000_000_000L; |
ids, timestamps, big numbers |
float |
32 bit | float f = 3.14f; |
rarely used |
double |
64 bit | double price = 19.99; |
default for decimals |
boolean |
โ | boolean active = true; |
yes/no |
char |
16 bit | char grade = 'A'; |
a single character |
Notes:
longliterals end withL,floatliterals withf.- Underscores make big numbers readable:
1_000_000. charuses single quotes,Stringuses double quotes.Stringis not primitive; it is a class (lesson 4).
โ ๏ธ Money:
doubleis imprecise (0.1 + 0.2 = 0.30000000000000004). In real banking code we useBigDecimal. For learning,doubleis fine.
3. Type casting
Widening (automatic, safe)
int i = 100;
long l = i; // int โ long
double d = l; // long โ double
Narrowing (manual, may lose data)
double price = 19.99;
int whole = (int) price; // 19, decimals are cut, not rounded
long big = 3_000_000_000L;
int broken = (int) big; // overflow! โ -1294967296
Integer division trap
int a = 7, b = 2;
System.out.println(a / b); // 3 (int / int = int)
System.out.println((double) a / b); // 3.5
4. var: local type inference
var name = "Anar"; // compiler infers String
var total = 0.0; // double
Use var only when the type is obvious from the right side.
5. Constants and naming
final double VAT_RATE = 0.18; // cannot be reassigned
// VAT_RATE = 0.2; // compile error
| Kind | Convention | Example |
|---|---|---|
| variable | camelCase | totalPrice, isActive |
| constant | UPPER_SNAKE_CASE | MAX_ATTEMPTS |
| class | PascalCase | BankAccount |
Good names explain what the value is: int d โ, int daysUntilPayment โ
.
6. Step by step: a shopping calculation
public class Checkout {
public static void main(String[] args) {
String product = "Mechanical keyboard";
double price = 120.50;
int quantity = 2;
final double DISCOUNT = 0.10;
final double VAT = 0.18;
double subtotal = price * quantity;
double afterDiscount = subtotal * (1 - DISCOUNT);
double total = afterDiscount * (1 + VAT);
System.out.println("Product: " + product);
System.out.println("Subtotal: " + subtotal);
System.out.println("Total: " + total);
}
}
7. Common mistakes
- โ Using a variable before assigning it:
int x; System.out.println(x);โ compile error. - โ
long n = 3000000000;โ error, addL. - โ Expecting
(int) 9.99to be10. It is9. - โ
char c = "A";โ use single quotes.
8. Exercises
- Declare variables for a bank customer: name, age, balance, isVip, initial of surname. Print them.
- Convert 37.5 ยฐC to Fahrenheit (
F = C * 9 / 5 + 32). - Given
int totalMinutes = 135, print2 h 15 min. - Show the overflow:
int max = Integer.MAX_VALUE; max + 1and explain the result.
9. Homework
Salary calculator: gross salary 2500 AZN, income tax 14%, social insurance 3%, unemployment insurance 0.5%. Print each deduction and the net salary.
10. Self-check
- Which type would you use for a customer id in a big bank? Why?
- What is printed:
System.out.println(5 / 2);? - What is the difference between widening and narrowing?
- Is
Stringa primitive type?
Answers
long:intcan overflow after 2.1 billion.2(integer division).- Widening goes to a bigger type and is automatic and safe; narrowing needs a cast and may lose data.
- No, it is a class (reference type).