Master Java programming with our comprehensive tutorial. Learn object-oriented programming through examples and hands-on practice.
Java is a class-based, object-oriented programming language designed to be platform-independent and secure.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
A simple Hello World program in Java
Learn fundamental Java concepts including variables, data types, and basic operations.
// Data types and variables
int number = 42;
double pi = 3.14159;
char grade = 'A';
boolean isValid = true;
String name = "John";
Common data types and variable declarations in Java
// Basic array declaration
int[] numbers = {1, 2, 3, 4, 5};
String[] names = new String[3];
// ArrayList example
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Programming");
Arrays and ArrayList examples
Java is built around OOP principles: encapsulation, inheritance, polymorphism, and abstraction.
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void study() {
System.out.println(name + " is studying.");
}
}
A simple class demonstrating OOP concepts
Learn how to control program flow using conditional statements and loops in Java.
// If-else statement
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else {
grade = 'C';
}
// For loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
// While loop
int count = 0;
while (count < 3) {
System.out.println("Count: " + count);
count++;
}
Examples of control flow statements and loops in Java