Java is a high-level, object-oriented, platform-independent programming language.
"Hello, World!" program demonstrates Java basics.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Install JDK, set JAVA_HOME, and use javac and java commands.
Arithmetic operations using Java operators.
int result = 5 + 3; // Output: 8Manipulate and display strings.
String message = "Hello, Java!";
System.out.println(message.toUpperCase());Control program flow with if-else.
int number = 10;
if (number > 0) {
System.out.println("Positive");
} else {
System.out.println("Negative");
}Iterate with loops.
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}Work with collections of elements.
int[] numbers = {1, 2, 3, 4};
System.out.println(numbers[0]); // Output: 1Encapsulate code in reusable methods.
public static int add(int a, int b) {
return a + b;
}Handle runtime errors gracefully.
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}Run parallel threads.
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
new MyThread().start();Use fundamental Java classes.
String str = "123";
int num = Integer.parseInt(str);
System.out.println(num);Add metadata to code.
@Override
public String toString() {
return "Example";
}Simplify functional programming.
Runnable r = () -> System.out.println("Lambda Expression");
r.run();Read and write files.
import java.io.File;
File file = new File("example.txt");
System.out.println(file.exists());Work with dynamic data structures.
import java.util.ArrayList;
ArrayList<String> list = new ArrayList<>();
list.add("Java");
System.out.println(list.get(0));Handle networking tasks.
import java.net.InetAddress;
InetAddress ip = InetAddress.getLocalHost();
System.out.println(ip);Create GUI applications.
import java.awt.Frame;
Frame frame = new Frame("AWT Example");
frame.setSize(300, 300);
frame.setVisible(true);Enhance GUIs with Swing components.
import javax.swing.JButton;
import javax.swing.JFrame;
JFrame frame = new JFrame("Swing Example");
JButton button = new JButton("Click Me");
frame.add(button);
frame.setSize(200, 200);
frame.setVisible(true);