Module lessons (1/2)
Variables and Basic Structure
Java is a strongly typed, object-oriented programming language. Every line of code in Java must reside inside a class, and program execution always starts from a special entry point called the main method.
The Basic Structure
A minimal Java program looks like this:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Let's analyze the key components:
public class Main: In Java, all code is enclosed in a class. The name of the public class must exactly match the filename (e.g.,Main.java).public static void main(String[] args): This is the starting point (entry point) of any Java application. When we run the program, the JVM (Java Virtual Machine) looks for this method and executes the code inside it.System.out.println(...): It is the standard statement to print text to the screen, followed by a new line.
Primitive Variables
In Java, before using a variable, we must declare its type. The fundamental primitive types are:
int: Integers (e.g.,42,-10).double: Double-precision floating-point numbers (e.g.,19.99,-0.5).boolean: Logical values, which can only betrueorfalse.char: A single character, enclosed in single quotes (e.g.,'A','7').
The declaration syntax is:
type variableName = value;
Practical example:
int score = 100;
double temperature = 36.5;
boolean isGameOver = false;
char grade = 'A';
Strings
Although not a primitive type, strings (String) in Java are used to handle sequences of characters. They are declared with a capital S, and the value must be enclosed in double quotes:
String name = "Alice";
System.out.println("Hello, " + name);
Comments in Java
To make code readable and documented, Java supports two types of comments:
- Single-line comments: start with
//and continue to the end of the line. - Multi-line comments: enclosed between
/*and*/.
// This is a single-line comment
int age = 30;
/* This is a
multi-line comment */
String role = "Developer";
Naming Conventions and Case Sensitivity
Java is strictly case-sensitive (it distinguishes uppercase from lowercase letters). A variable named score is different from Score.
By convention:
- Variables and methods use
camelCasenotation (e.g.,discountedPrice,calculateTax). - Classes use
PascalCasenotation (e.g.,InvoiceManager,Main).
Printing to the Screen: print vs println
The System.out system class offers two main methods to display output:
System.out.print(...): prints the value without adding a new line. Subsequent output will start on the same line.System.out.println(...): prints the value and moves the cursor to the next line (print line).
Try it yourself
Declare an integer variable named age with an initial value of 25. Then, print it to the screen using System.out.println.
Show hint
Write `int age = 25;` to declare the variable, then use `System.out.println(age);` on the next line.
Solution available after 3 attempts
Declare a double variable named price with an initial value of 19.99 and a boolean variable named isAvailable set to true. Print both variables on separate lines.
Show hint
Define `double price = 19.99;` and `boolean isAvailable = true;`, then print them one by one using `System.out.println`.
Solution available after 3 attempts
Declare a char variable named grade set to 'A' and a String variable named student set to "John". Print to the screen the text: student + " : " + grade.
Show hint
Declare char grade = 'A'; and String student = "John";, then print them concatenated using System.out.println(student + " : " + grade);.
Solution available after 3 attempts