Java Basics
Java is a statically typed language, which means every variable has a known type before the program runs. In this lesson, you will learn how a Java program is organized, how values are stored, and how expressions turn those values into useful results.
Learning objectives
- Explain the role of a class and the main method
- Choose suitable primitive and reference types
- Create readable expressions with Java operators
Build a strong mental model
The shape of a Java program
Java source code lives inside classes. The JVM starts a simple console program by calling public static void main(String[] args). Statements end with semicolons, and braces group statements into blocks. Java is case-sensitive, so score and Score are different names.
Variables and types
A variable combines a type, a name, and a value. Primitive types such as int, double, boolean, and char store simple values. Reference types such as String point to objects. Use descriptive camelCase names and prefer the narrowest type that correctly represents the data.
Operators and conversion
Arithmetic, comparison, and logical operators build expressions. Integer division removes the decimal part, while casting can request a conversion explicitly. Java also performs safe widening conversions, such as int to double, but rejects unsafe narrowing without a cast.
Read the code, understand the design
public class StudentProfile {
public static void main(String[] args) {
String name = "Alex";
int birthYear = 2005;
double javaScore = 8.5;
boolean passed = javaScore >= 5.0;
int age = 2026 - birthYear;
System.out.println(name + " is " + age + " years old.");
System.out.println("Passed Java: " + passed);
}
}How this example works
- The variables use types that match the meaning of their values.
- The comparison javaScore >= 5.0 produces a boolean result.
- The + operator performs addition for numbers and concatenation when a String is involved.
Common mistakes to avoid
- Using int when a value may contain decimals
- Forgetting that integer division truncates the result
- Using unclear names such as x or data in learning projects
Build a console profile for a course student.
- Store the student's name, student ID, Java score, and attendance percentage
- Calculate whether the student passes: score >= 5 and attendance >= 80
- Print a clear, multi-line summary