Java short tutorial
Java Tutorial (Easy & Modern Features)
Java is a robust, object-oriented language widely used for enterprise applications, Android development, and large-scale systems.
1. "Hello, World!"
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java World!");
}
}
Explanation:
-
public class HelloWorld
: Defines a class namedHelloWorld
. In Java, all code resides within classes. -
public static void main(String[] args)
: This is the entry point of the Java program.public
: The method is accessible from anywhere.static
: The method belongs to the class itself, not to any specific object of the class.void
: The method doesn't return any value.main
: The special name for the entry point method.String[] args
: An array of strings to receive command-line arguments.
-
System.out.println(...)
: Prints output to the console, followed by a newline.
2. var
for Local Variable Type Inference (Java 10+):
Allows the compiler to infer the type of a local variable based on its initialization, similar to C++ auto
or Python's typing.
auto
or Python's typing.
import java.util.ArrayList;
import java.util.List;
public class VarExample {
public static void main(String[] args) {
var message = "Java is fun!"; // Compiler infers 'message' is String
var count = 10; // Compiler infers 'count' is int
var names = new ArrayList<String>(); // Compiler infers 'names' is ArrayList<String>
names.add("Alice");
names.add("Bob");
System.out.println(message);
System.out.println("Count: " + count);
System.out.println("Names: " + names);
}
}
Explanation:
var message = ...
: Declaresmessage
usingvar
, letting the compiler determine its type (String
).
3. Records (Java 14+):
A concise way to create classes that primarily hold data. They automatically generate constructors, getters, equals()
, hashCode()
, and toString()
.
public record Point(int x, int y) {
public static void main(String[] args) {
Point p1 = new Point(10, 20);
Point p2 = new Point(10, 20);
Point p3 = new Point(30, 40);
System.out.println("Point 1: " + p1); // Uses generated toString()
System.out.println("Point 1 X: " + p1.x()); // Uses generated getter
System.out.println("p1 equals p2? " + p1.equals(p2)); // Uses generated equals()
System.out.println("p1 equals p3? " + p1.equals(p3)); // Uses generated equals()
}
}
Explanation:
public record Point(int x, int y)
: Defines a record namedPoint
with two integer components,x
andy
.p1.x()
: Automatically generates a getter method for thex
component.
4. Text Blocks (Java 15+):
A convenient way to define multi-line strings without needing escape sequences like \n
.
strings without needing escape sequences like \n
.
public class TextBlockExample {
public static void main(String[] args) {
String html = """
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Welcome!</h1>
<p>This is a simple paragraph.</p>
</body>
</html>
""";
System.out.println(html);
}
}
Explanation:
""" ... """
: Uses triple double quotes to define a text block. Newlines and indentation are generally preserved.
5. Switch Expressions (Java 14+, enhanced in Java 17+):
A more concise way to write switch
statements, allowing them to return a value.
public class SwitchExpressionExample {
public static void main(String[] args) {
int dayOfWeek = 3;
String dayName = switch (dayOfWeek) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6 -> "Saturday";
case 7 -> "Sunday";
default -> "Invalid day";
};
System.out.println("The day is: " + dayName);
}
}
Explanation:
switch (dayOfWeek) { ... }
: Defines the switch expression.case 1 -> "Monday";
: Uses the->
operator to specify the value to return for each case.default -> "Invalid day";
: The default case.
Running Java Code:
Save the code in a .java
file (e.g., MyProgram.java
). Open your terminal or command prompt, navigate to the directory where you saved it.
- Compile:
javac MyProgram.java
- Run:
java MyProgram