Java Programming Fundamentals
A Detailed Overview of Java Basics
Introduction to Java
• Java is a high-level, object-oriented
programming language.
• Developed by Sun Microsystems (now owned
by Oracle).
• Write Once, Run Anywhere (WORA) – Platform
Independent.
• Used for web, desktop, and mobile
applications.
Features of Java
• Simple and Easy to Learn
• Object-Oriented Programming (OOP)
• Platform Independent (JVM)
• Secure and Robust
• Multi-threading Support
• Rich API and Frameworks
Java Environment Components
• JDK (Java Development Kit) - Contains tools for
Java development.
• JRE (Java Runtime Environment) - Provides
libraries and JVM.
• JVM (Java Virtual Machine) - Executes Java
bytecode.
Basic Java Program Structure
• A Java program consists of a class and a main
method.
• Every statement ends with a semicolon (;).
• Curly braces `{}` are used for defining blocks.
Java Program Example
• public class HelloWorld {
• public static void main(String[] args) {
• System.out.println("Hello, World!");
• }
• }
Java Data Types
• Primitive Data Types: int, float, char, boolean,
double, long.
• Reference Data Types: Objects, Arrays, Strings.
• Example: int age = 25; boolean isJavaFun =
true;
Variables and Constants in Java
• Variables store data values.
• Constants are declared using 'final' keyword.
• Example: final int MAX_VALUE = 100;
Operators in Java
• Arithmetic Operators (+, -, *, /, %)
• Relational Operators (==, !=, <, >, <=, >=)
• Logical Operators (&&, ||, !)
• Assignment Operators (=, +=, -=, *=, /=)
Control Flow Statements
• Conditional Statements: if, if-else, switch.
• Looping Statements: for, while, do-while.
• Jump Statements: break, continue, return.
Example: if-else Statement
• int num = 10;
• if (num > 0) {
• System.out.println("Positive Number");
• } else {
• System.out.println("Negative Number");
• }
OOP Concepts in Java
• Encapsulation - Data hiding using private
fields.
• Inheritance - One class acquires properties of
another.
• Polymorphism - Method overloading &
overriding.
• Abstraction - Hiding implementation details.
Example: Creating a Class & Object
• class Car {
• String brand;
• void displayBrand() {
• System.out.println("Car Brand: " + brand);
• }
• }
• Car myCar = new Car();
• myCar.brand = "Toyota";
Exception Handling
• Used to handle runtime errors.
• Try-Catch Blocks.
• Finally Block for cleanup.
• Throw and Throws keywords.
Example: Try-Catch Block
• try {
• int divide = 10 / 0; // This will cause an
exception.
• } catch (ArithmeticException e) {
• System.out.println("Cannot divide by zero!");
• }
Collections Framework in Java
• List: ArrayList, LinkedList.
• Set: HashSet, TreeSet.
• Map: HashMap, TreeMap.
• Used for managing dynamic data structures.
Example: Using ArrayList
• import java.util.ArrayList;
• ArrayList<String> list = new ArrayList<>();
• list.add("Java");
• list.add("Python");
• System.out.println(list); // Output: [Java,
Python]
File Handling in Java
• Reading and Writing Files.
• Using FileReader, FileWriter.
• Handling Exceptions in File Operations.
Example: Writing to a File
• import java.io.FileWriter;
• FileWriter writer = new FileWriter("test.txt");
• writer.write("Hello, Java!");
• writer.close();
• System.out.println("File written successfully.");
Multithreading Concepts
• Running multiple tasks simultaneously.
• Creating Threads using Thread class.
• Synchronization for thread safety.
Example: Creating a Thread
• class MyThread extends Thread {
• public void run() {
• System.out.println("Thread is running.");
• }
• }
• MyThread thread = new MyThread();
• thread.start();
Conclusion
• Java is versatile and widely used.
• Understanding fundamentals is key to
mastering Java.
• Practice regularly to improve Java skills.
• Java is the foundation for advanced
technologies like Spring, Hibernate, and
Android Development.