Powerful object-oriented programming for desktop, web, and mobile applications!
← Back to HomeClick on a topic to expand and learn in detail.
Java is a high-level, object-oriented programming language used for desktop, web, and mobile applications.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
int age = 25;
double pi = 3.14;
char grade = 'A';
boolean flag = true;
String name = "Alice";
// Arithmetic: +, -, *, /, %
int sum = 5 + 3;
// Comparison: ==, !=, >, <
boolean result = (5 > 3);
// Logical: &&, ||, !
boolean flag = true && false;
int age = 18;
if(age >= 18){
System.out.println("Adult");
} else {
System.out.println("Minor");
}
switch(age){
case 18:
System.out.println("Exactly 18");
break;
default:
System.out.println("Other age");
}
// for loop
for(int i=0;i<5;i++){
System.out.println(i);
}
// while loop
int i=0;
while(i<5){
System.out.println(i);
i++;
}
// do-while loop
int j=0;
do{
System.out.println(j);
j++;
}while(j<5);
public static int add(int a, int b){
return a + b;
}
public static void main(String[] args){
System.out.println(add(5,3));
}
int[] numbers = {1,2,3,4,5};
System.out.println(numbers[0]); // 1
String name = "Alice";
System.out.println(name);
class Person {
String name;
int age;
void greet(){
System.out.println("Hello " + name);
}
}
public class Main {
public static void main(String[] args){
Person p = new Person();
p.name = "Alice";
p.age = 25;
p.greet();
}
}
try{
int result = 10 / 0;
}catch(ArithmeticException e){
System.out.println("Cannot divide by zero");
}finally{
System.out.println("Execution finished");
}