Java Programming
Java Programming
Java Programming
Introduction to Java
Key Features:
o Object-Oriented: Uses concepts like classes, objects, inheritance, polymorphism,
abstraction, and encapsulation.
o Platform-Independent: Java code is compiled into bytecode that runs on any
device with a JVM.
o Secure: Offers features like bytecode verification, security manager, and
exception handling.
o Multi-threaded: Supports concurrent programming, allowing multiple threads to
run simultaneously.
Installation Steps:
1. Download and install the Java Development Kit (JDK).
2. Set the JAVA_HOME environment variable to point to the JDK directory.
3. Install an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse,
or NetBeans.
Basic Tools:
Basic Syntax:
java
Copy code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Data Types:
o Primitive: int, float, char, boolean, etc.
o Non-Primitive: Strings, Arrays, Classes, and Objects.
Control Structures:
o Conditional: if, else, switch.
o Loops: for, while, do-while.
java
Copy code
class Car {
String color;
void drive() {
System.out.println("The car is driving.");
}
}
public class Test {
public static void main(String[] args) {
Car myCar = new Car();
myCar.color = "Red";
myCar.drive();
}
}
Standard Libraries:
o java.lang: Core classes like String, Math, Object.
o java.util: Collections framework, utility classes like ArrayList, HashMap.
o java.io: Input/output classes for file handling.
o java.net: Classes for network programming.
Popular Frameworks:
o Spring: Framework for building enterprise applications.
o Hibernate: ORM (Object-Relational Mapping) framework for database
interaction.
o JavaFX: Framework for building desktop GUI applications.
Thread Basics:
o A thread is a lightweight process.
o Threads can be created by extending Thread class or implementing Runnable.
o Example:
java
Copy code
public class MyThread extends Thread {
public void run() {
System.out.println("Thread is running.");
}
}
public class TestThread {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
java
Copy code
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream().filter(n -> n % 2 ==
0).forEach(System.out::println);
Java is widely used for building scalable and secure applications across various domains,
including web development, enterprise systems, mobile apps, and embedded systems. Mastering
Java involves continuous practice, exploring frameworks, and staying updated with the latest
versions and features.