public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");
// This is a comment
System.out.println("Hello World");
System.out.println("Hello World"); // This is a comment
/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");
int x = 100 + 50;
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 20) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Outputs "Good evening."
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
// Outputs "Thursday" (day 4)
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
// Now outputs Opel instead of Volvo
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
// Outputs 4
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int x = myNumbers[1][2];
System.out.println(x); // Outputs 7