1.
Data Types
Java supports various data types:
- Primitive: int, float, double, char, boolean, etc.
- Reference: arrays, classes, interfaces, etc.
Program 1:
class Example1 {
int a = 10;
float b = 5.5f;
void show() {
System.out.println("a = " + a + ", b = " + b);
}
public static void main(String[] args) {
new Example1().show();
}
}
Program 2:
class Example2 {
char letter = 'A';
boolean isValid = true;
void show() {
System.out.println("Letter: " + letter + ", Valid: " + isValid);
}
public static void main(String[] args) {
new Example2().show();
}
}
Program 3:
class Example3 {
String name = "Java";
void greet() {
System.out.println("Welcome to " + name);
}
public static void main(String[] args) {
new Example3().greet();
}
}
Program 4:
class Example4 {
int[] numbers = {1, 2, 3};
void display() {
for (int n : numbers) {
System.out.println(n);
}
}
public static void main(String[] args) {
new Example4().display();
}
}
2. Operators
Operators in Java include:
- Arithmetic (+, -, *, /, %)
- Relational (==, !=, >, <, >=, <=)
- Logical (&&, ||, !)
- Assignment (=, +=, -=, etc.)
Program 1:
class Add {
public static void main(String[] args) {
int a = 5, b = 3;
System.out.println("Sum: " + (a + b));
}
}
Program 2:
class RelationalCheck {
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println("a > b: " + (a > b));
}
}
Program 3:
class LogicalOp {
public static void main(String[] args) {
boolean x = true, y = false;
System.out.println("x && y: " + (x && y));
}
}
Program 4:
class AssignmentOp {
public static void main(String[] args) {
int a = 5;
a += 3;
System.out.println("a = " + a);
}
}
3. Type Casting
Type casting in Java:
- Implicit (automatic): int to float
- Explicit (manual): float to int
Used to convert one data type to another.
Program 1:
class ImplicitCast {
public static void main(String[] args) {
int a = 10;
float b = a;
System.out.println("Float value: " + b);
}
}
Program 2:
class ExplicitCast {
public static void main(String[] args) {
double d = 9.78;
int i = (int) d;
System.out.println("Int value: " + i);
}
}
Program 3:
class CharToInt {
public static void main(String[] args) {
char c = 'A';
int i = c;
System.out.println("ASCII of A: " + i);
}
}
Program 4:
class IntToChar {
public static void main(String[] args) {
int i = 66;
char c = (char) i;
System.out.println("Char of 66: " + c);
}
}
4. Conditional Statements
Conditional statements include:
- if, if-else, else-if ladder
- switch-case
Used to execute code based on conditions.
Program 1:
class IfExample {
public static void main(String[] args) {
int a = 10;
if (a > 5) {
System.out.println("a is greater than 5");
}
}
}
Program 2:
class IfElseExample {
public static void main(String[] args) {
int a = 3;
if (a % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
}
}
Program 3:
class ElseIfExample {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90)
System.out.println("Grade A");
else if (marks >= 75)
System.out.println("Grade B");
else
System.out.println("Grade C");
}
}
Program 4:
class SwitchExample {
public static void main(String[] args) {
int day = 3;
switch(day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Invalid day");
}
}
}
5. Looping Statements
Loops in Java:
- for, while, do-while
Used to execute a block of code repeatedly.
Program 1:
class ForLoop {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++)
System.out.println(i);
}
}
Program 2:
class WhileLoop {
public static void main(String[] args) {
int i = 1;
while(i <= 5) {
System.out.println(i);
i++;
}
}
}
Program 3:
class DoWhileLoop {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while(i <= 5);
}
}
Program 4:
class NestedLoop {
public static void main(String[] args) {
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 2; j++) {
System.out.println("i=" + i + ", j=" + j);
}
}
}
}
6. Static & Dynamic Initialization
- Static Initialization: variables assigned during declaration.
- Dynamic Initialization: variables assigned at runtime using expressions or input.
Program 1:
class StaticInit {
int a = 10;
void show() {
System.out.println("Static a = " + a);
}
public static void main(String[] args) {
new StaticInit().show();
}
}
Program 2:
class DynamicInit {
public static void main(String[] args) {
int a = 5;
int b = a + 3;
System.out.println("b = " + b);
}
}
Program 3:
class InitBlock {
int x;
{ x = 20; }
public static void main(String[] args) {
InitBlock i = new InitBlock();
System.out.println("x = " + i.x);
}
}
Program 4:
class ConstructorInit {
int x;
ConstructorInit(int val) {
x = val;
}
public static void main(String[] args) {
ConstructorInit obj = new ConstructorInit(50);
System.out.println("x = " + obj.x);
}
}