Java Notes (Beginner to Intermediate)
1. Introduction to Java
Java is a high-level, object-oriented, class-based, and platform-independent programming
language.
Developed by James Gosling at Sun Microsystems in 1995.
Now maintained by Oracle Corporation.
2. Features of Java
Feature Description
Simple Easy to learn, clean syntax like C/C++
Object-Oriented Everything in Java is treated as an object
Platform Independent Code written once can run on any system using the JVM
Secure Provides runtime checking, no pointer access
Robust Strong memory management and exception handling
Multithreaded Supports multiple threads of execution
Portable Java bytecode runs on any machine
High Performance Just-In-Time (JIT) compiler improves speed
3. Java Program Structure
java
CopyEdit
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
Part Description
public class Main Declares a class named Main
public static void main Entry point of Java application
Part Description
String[] args Command-line arguments
System.out.println() Prints output to console
4. Java Compilation and Execution
Step Command
Compile javac FileName.java
Run java ClassName
5. Java Data Types
Primitive Data Types
Type Size Description
byte 1 byte Whole numbers (−128 to 127)
short 2 bytes Whole numbers (−32K to 32K)
int 4 bytes Whole numbers (default type)
long 8 bytes Large whole numbers
float 4 bytes Decimal numbers
double 8 bytes Double-precision decimal
char 2 bytes Single character
boolean 1 bit true or false
6. Java Operators
Type Examples
Arithmetic +, -, *, /, %
Relational ==, !=, >, <, >=, <=
Logical &&, `
Assignment =, +=, -=, etc.
Increment/Decrement ++, --
7. Control Statements
Conditional Statements
java
CopyEdit
if (condition) {
// code
} else {
// code
Switch Statement
java
CopyEdit
switch (value) {
case 1:
// code
break;
default:
// code
Looping Statements
java
CopyEdit
for (int i = 0; i < 10; i++) {
// code
while (condition) {
// code
do {
// code
} while (condition);
8. Java Classes and Objects
Class Definition
java
CopyEdit
class Car {
int speed = 100;
Object Creation
java
CopyEdit
Car myCar = new Car();
System.out.println(myCar.speed);
9. Constructors
A constructor is a special method used to initialize objects.
java
CopyEdit
class Car {
Car() {
System.out.println("Constructor called");
10. Inheritance
Java supports single inheritance using the extends keyword.
java
CopyEdit
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void bark() { System.out.println("Bark"); }
11. Method Overloading and Overriding
Overloading: Same method name, different parameters.
Overriding: Same method in parent and child class.
java
CopyEdit
class A {
void show(int x) { }
void show(int x, int y) { } // Overloaded
class B extends A {
void show(int x) { } // Overridden
12. Access Modifiers
Modifier Scope
public Accessible everywhere
private Accessible only within the class
protected Accessible within the package and subclasses
default Accessible only within the same package
13. Interfaces and Abstract Classes
Interface
java
CopyEdit
interface Animal {
void sound();
Abstract Class
java
CopyEdit
abstract class Shape {
abstract void draw();
14. Exception Handling
java
CopyEdit
try {
// risky code
} catch (Exception e) {
System.out.println("Error: " + e);
} finally {
// always executed
15. Java Packages
A package is a namespace that organizes classes and interfaces.
java
CopyEdit
package mypack;
public class MyClass {
// code
To use:
java
CopyEdit
import mypack.MyClass;
16. Java Input and Output
java
CopyEdit
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
17. Java Arrays
java
CopyEdit
int[] arr = {1, 2, 3, 4};
System.out.println(arr[0]);
18. Java Strings
java
CopyEdit
String s = "Hello";
System.out.println(s.length());
System.out.println(s.charAt(0));
System.out.println(s.toUpperCase());
19. Java Object-Oriented Principles
Principle Description
Encapsulation Wrapping data using classes and access control
Inheritance Reuse of existing class using extends
Polymorphism One interface, multiple implementations
Principle Description
Abstraction Hiding implementation details
20. Java Memory Management
Managed by Garbage Collector (GC)
Java automatically deallocates unused objects.
21. Java Virtual Machine (JVM)
JVM is a virtual machine that runs Java bytecode.
It provides platform independence and runtime security.