Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 0

Variables, types and printing

The vocabulary every later lesson assumes

12m30 XP
→→→

What you'll be able to do

  • Declare a variable and explain why Java forces you to give it a type
  • Tell the primitive types (int, double, boolean, char) apart from String
  • Print values and join them together with +
  • Read the skeleton of a Java program without being scared of it
1

Variables

A variable is a labelled box

Programs are mostly about storing values and moving them around. A variable is where a value waits.

A variable is a named place in memory that holds one value. In Java you must say *what kind* of value it will hold before you use it — that is called the type. Java checks types while compiling, which means many mistakes are caught before the program ever runs.

Boxes in a warehouse

Imagine labelled boxes: the box labelled age only accepts whole numbers, the box labelled price accepts decimals, and the box labelled name accepts text. If you try to drop a decimal into the whole-number box, the warehouse manager (the compiler) stops you at the door.

Four declarations, read one by one

Main.java
1int age = 20;
2double price = 19.99;
3boolean isStudent = true;
4String name = "Sara";
int age = 20;
Type int = whole number. age is the name. = 20 puts the first value in the box. The ; ends the statement.
double price = 19.99;
double stores decimals. Money, averages and measurements usually need double, not int.
boolean isStudent = true;
boolean holds only true or false — the type behind every if decision you will ever write.
String name = "Sara";
String holds text and is written with a capital S because it is not a primitive: it is an object type. Remember that word — Module 1 starts there.

Naming matters more than you think

Java convention: variables and methods use camelCase (bankBalance), classes use PascalCase (BankAccount). Good names are the cheapest documentation you will ever write.

2

Data Types

Primitives, String, and the division trap

One of the most common beginner bugs in Java is not a typo — it is integer division.

The types you will actually use in this course

TypeStoresExampleDefault value
intwhole numbers420
doubledecimal numbers3.50.0
booleantrue / falsetruefalse
chara single character'A''\u0000'
Stringtext (an object)"Sara"null

The default-value column matters later: fields you never assign start with exactly these values.

int / int is an int — always
Main.java
1int a = 7;
2int b = 2;
3System.out.println(a / b);
4System.out.println(a % b);
5System.out.println((double) a / b);
Prints `3`, then `1`, then `3.5`. Java does not round — it throws the fraction away. `%` gives the remainder, and casting one side to `double` gives the decimal answer.

Why this will bite you in Module 4

Bank balances are double. If you store money in an int, balance / 3 silently loses cents. Choosing the type *is* a design decision.

Joining values with +

When one side of + is a String, Java turns the other side into text and glues them: "Age: " + 20 produces Age: 20. When both sides are numbers, + is ordinary addition. This one rule explains most surprising output in beginner code.

3

Java Syntax

Every Java program starts in main

The skeleton you will type hundreds of times

Main.java
1public class Main {
2 public static void main(String[] args) {
3 System.out.println("Hello OOP");
4 }
5}
public class Main {
All Java code lives inside a class. For now treat it as a container; from Module 2 it becomes the blueprint of an object.
public static void main(String[] args) {
The entry point. public = callable from outside, static = belongs to the class not to an object, void = returns nothing, String[] args = command-line arguments.
System.out.println("Hello OOP");
Prints one line to the console. println = print line; print would stay on the same line.
}
Every { needs its }. Unbalanced braces are the number one compile error for beginners.

About `static`

static means 'belongs to the class itself, not to an individual object'. That is why main can run before any object exists. Once you meet objects in Module 2, this sentence will suddenly make a lot more sense — come back and reread it.

Try it: change the values and run

Change `hours` to 9 and predict the new output before pressing Run.

Output
Press Run to execute this example.

Common mistakes

Wrongint average = 7 / 2;
WhyBoth sides are int, so Java computes 3 and the .5 is gone before the assignment even happens.
Fixdouble average = 7 / 2.0; — make at least one side a decimal.
Wrongstring name = "Sara";
WhyJava is case sensitive and there is no type called `string`.
FixString name = "Sara"; — capital S, because String is a class.
WrongSystem.out.println("Total: " + 1 + 2);
Why`+` runs left to right: the String eats the 1, then eats the 2, giving `Total: 12`.
FixSystem.out.println("Total: " + (1 + 2)); — parentheses force the addition first.

Lesson playground

Free practice

Print a receipt line for any product. Try changing the types and watch the output change.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 5

You are storing a bank balance that must keep cents. Which type do you choose?

Summary

  • A variable = type + name + value, and Java checks the type at compile time.
  • int, double, boolean and char are primitives; String is an object type (capital S).
  • int / int stays int — cast to double when you need the fraction.
  • `+` means addition between numbers and concatenation as soon as one side is text.