corejava
corejava
1. Basic Syntax
java
// Hello World Program
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Data Types
java
int age = 25;
double price = 10.5;
char grade = 'A';
boolean isJavaFun = true;
String name = "Praveen";
3. Operators
java
int sum = 10 + 5; // Addition
int diff = 10 - 5; // Subtraction
int product = 10 * 5; // Multiplication
int quotient = 10 / 5; // Division
int remainder = 10 % 5; // Modulus
4. Conditional Statements
java
if (age > 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
5. Loops
java
// For Loop
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
// While Loop
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
// Do-While Loop
int j = 1;
do {
System.out.println(j);
j++;
} while (j <= 5);
6. Arrays
java
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[0]); // Prints 1
7. Methods
java
public class Main {
static int add(int a, int b) {
return a + b;
}
Person(String name) {
this.name = name;
}
void display() {
System.out.println("Name: " + name);
}
}