Java Basics - Class Notes
Java Basics - Class Notes
1. Introduction to Java
• What is Java?
o Java is a high-level, object-oriented programming language developed by Sun
Microsystems (now owned by Oracle).
o It is platform-independent, meaning Java programs can run on any device or
operating system that has a Java Virtual Machine (JVM).
• Key Features of Java:
o Object-Oriented: Focuses on objects, classes, inheritance, and polymorphism.
o Platform Independent: "Write Once, Run Anywhere" (WORA) due to JVM.
o Multithreading: Allows multiple tasks to run concurrently.
o Automatic Memory Management: Uses garbage collection to manage memory.
o Rich API: Extensive set of libraries for networking, GUI, utilities, etc.
2. Setting Up Java
java
Copy
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
java
Copy
int age = 25;
double price = 10.99;
boolean isJavaFun = true;
• If-Else Statement:
java
Copy
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
• Switch Statement:
java
Copy
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");
}
• Loops:
o For Loop:
java
Copy
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
o While Loop:
java
Copy
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
6. Functions (Methods)
• Defining a Method:
java
Copy
public static int addNumbers(int a, int b) {
return a + b;
}
• Calling a Method:
java
Copy
int result = addNumbers(5, 3);
System.out.println(result); // Output: 8
java
Copy
class Car {
String model;
int year;
• Inheritance:
o Allows a class to inherit fields and methods from another class.
java
Copy
class Animal {
void makeSound() {
System.out.println("Some sound");
}
}
• Polymorphism:
o The ability for different classes to be treated as instances of the same class
through inheritance. Methods can have the same name but behave differently.
• Encapsulation:
o Bundles the data (variables) and methods into a single unit (class) and restricts
access to the inner workings of the class through access modifiers (public,
private).
• Abstraction:
o Hides the complex implementation details and shows only the necessary features
of the object.
8. Exception Handling
• Try-Catch Block:
o Used to handle exceptions (errors during program execution).
java
Copy
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
9. Collections Framework
Example of ArrayList:
java
Copy
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
• Common Libraries:
o java.util: Collections, date/time utilities, etc.
o java.io: File I/O operations.
o java.net: Networking utilities.
• Lambda Expressions:
o Allows writing anonymous methods in a more concise way.
java
Copy
(a, b) -> a + b;
• Streams API:
o Facilitates functional-style operations on collections of elements.
java
Copy
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
This is a high-level overview. If you want more detailed examples or explanations on any
specific topic, just let me know!
You said:
ChatGPT
I can't directly generate a PDF for you, but I can guide you through how to do it yourself!
This will give you a PDF version of your Java class notes!