0% found this document useful (0 votes)
7 views7 pages

Core Java Notes

The document is a comprehensive guide on Core Java, covering essential topics such as syntax, object-oriented programming, data types, collections, exceptions, multithreading, and Java 8 features. It includes examples, explanations of key concepts, and a section on interview questions to prepare for Java-related job interviews. Additionally, it outlines a suggested learning order for mastering Core Java.

Uploaded by

clashking64ak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Core Java Notes

The document is a comprehensive guide on Core Java, covering essential topics such as syntax, object-oriented programming, data types, collections, exceptions, multithreading, and Java 8 features. It includes examples, explanations of key concepts, and a section on interview questions to prepare for Java-related job interviews. Additionally, it outlines a suggested learning order for mastering Core Java.

Uploaded by

clashking64ak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Core Java Notes

Beginner to Interview Level


A structured Core Java guide covering syntax, OOP, Strings, Collections, Exceptions, Multithreading, JVM, Java 8
features and interview concepts.

1. Java Introduction
• Java is a high-level, class-based, object-oriented language.

• Java source code is compiled into bytecode.

• Bytecode runs on the JVM, making Java largely platform-independent.

• Java uses automatic garbage collection.

2. JDK, JRE & JVM


• JDK: development kit containing tools and runtime components.

• JRE: runtime environment containing JVM and libraries.

• JVM: executes Java bytecode.


Java source (.java) → javac → Bytecode (.class) → JVM → Machine Code

3. First Program
public class Main { public static void main(String[] args) { [Link]("Hello
Java"); } }

javac [Link] java Main

4. Variables & Data Types


• Primitive: byte, short, int, long, float, double, char, boolean.

• Reference types: classes, arrays, interfaces, enums, etc.

• Local variables must be initialized before use.

• final prevents reassignment of a variable.


int age = 25; double salary = 45000.50; char grade = 'A'; boolean active = true; final int
MAX = 100;

5. Type Casting
int x = 10; double d = x; // widening double price = 10.5; int p = (int) price; // narrowing

6. Operators
• Arithmetic: +, -, *, /, %

• Relational: ==, !=, >, <, >=, <=

• Logical: &&, ||, !

• Assignment: =, +=, -=, *=, /=

• Unary: ++, --

• Ternary: condition ? value1 : value2

Core Java Notes Page 1


7. Input
import [Link]; Scanner sc = new Scanner([Link]); int age = [Link](); String
name = [Link](); [Link](age + " " + name);

8. Conditions & Loops


if (age >= 18) { [Link]("Adult"); } else { [Link]("Minor"); } for
(int i=0; i<5; i++) [Link](i); while (age < 30) age++;

9. Methods
A method is a block of code that performs a task and may accept parameters and return a value.
static int add(int a, int b) { return a + b; }

10. Arrays
int[] nums = {10, 20, 30}; [Link](nums[0]); for (int n : nums)
[Link](n);

• Arrays have fixed size after creation.

• Indexes start at 0.

11. Strings
• String objects are immutable.

• String literals are commonly stored in the String Pool.

• equals() compares content when properly implemented; == compares primitive values or object references.
String a = "Java"; String b = new String("Java"); [Link]([Link](b)); // true
[Link](a == b); // false

12. StringBuilder & StringBuffer


• StringBuilder is mutable and generally preferred for single-threaded string manipulation.

• StringBuffer is synchronized and can be useful when synchronized string operations are required.
StringBuilder sb = new StringBuilder("Hello"); [Link](" Java"); [Link]();

13. OOP — Four Pillars


• Encapsulation: bundle data and methods and control access.

• Inheritance: derive a class from another class.

• Polymorphism: one interface/reference can represent different implementations.

• Abstraction: expose essential behavior while hiding implementation details.

14. Class & Object


class Car { String color; void drive() { [Link]("Driving"); } } Car c = new
Car(); [Link] = "Red"; [Link]();

15. Constructors, this & super


• Constructor initializes an object and has the same name as the class.

• this refers to the current object.

• super refers to the immediate parent-class members.

Core Java Notes Page 2


class User { String name; User(String name) { [Link] = name; } }

16. Access Modifiers


Modifier Access

private Same class only

default Same package

protected Same package + subclasses (with Java access rules)

public Everywhere

17. Static & Final


• static belongs to the class rather than an individual object.

• static methods cannot directly access instance members without an object.

• final variable cannot be reassigned.

• final method cannot be overridden.

• final class cannot be extended.

18. Inheritance
class Animal { void sound() { [Link]("Sound"); } } class Dog extends Animal {
void bark() { [Link]("Bark"); } }

19. Overloading vs Overriding


• Overloading: same method name with different parameter lists; compile-time resolution.

• Overriding: subclass replaces an inherited instance-method implementation; runtime dispatch.

20. Abstract Class & Interface


• Abstract class can contain abstract/concrete methods, constructors and state.

• Interface defines a contract; modern interfaces can have default and static methods.

• A class can implement multiple interfaces.


interface Payment { void pay(); } class UPI implements Payment { public void pay() {
[Link]("Paid"); } }

21. Encapsulation Example


class Account { private double balance; public double getBalance() { return balance; } public
void deposit(double amount) { if (amount > 0) balance += amount; } }

22. Exception Handling


• try contains risky code.

• catch handles an exception.

• finally is used for cleanup and normally executes whether an exception occurs or not.

• throw explicitly throws an exception.

• throws declares exceptions a method may propagate.


try { int x = 10 / 0; } catch (ArithmeticException e) { [Link]([Link]()); }
finally { [Link]("Done"); }

Core Java Notes Page 3


23. Checked vs Unchecked Exceptions
• Checked exceptions are checked by the compiler, e.g. IOException.

• Unchecked exceptions extend RuntimeException, e.g. NullPointerException and ArithmeticException.

• Error represents serious JVM/system problems and is generally not meant for normal recovery.

24. Collections Framework


• List: ordered collection; duplicates allowed.

• Set: unique elements.

• Map: key-value associations.

• Queue/Deque: ordered processing.


List list = new ArrayList<>(); [Link]("Java"); Set set = new HashSet<>(); [Link](10); Map
map = new HashMap<>(); [Link]("Ayush", 25);

25. Collection Implementations


• ArrayList: fast random access; common List choice.

• LinkedList: linked structure; useful for certain insertion/removal patterns.

• HashSet: unique elements, no guaranteed iteration order.

• LinkedHashSet: preserves insertion order.

• TreeSet: sorted set.

• HashMap: common key-value map.

• LinkedHashMap: predictable ordering.

• TreeMap: sorted by key.

• ConcurrentHashMap: designed for concurrent access.

26. equals() & hashCode()


If two objects are equal according to equals(), they must return the same hashCode(). This matters for HashMap and
HashSet.
@Override public boolean equals(Object o) { ... } @Override public int hashCode() { ... }

27. Generics
List names = new ArrayList<>(); [Link]("Java"); String name = [Link](0);

Generics provide compile-time type safety and reduce explicit casting.

28. Wrapper Classes


• Integer, Long, Double, Character and Boolean are wrapper classes.

• Autoboxing converts primitive to wrapper automatically.

• Unboxing converts wrapper to primitive.


Integer x = 10; int y = x;

29. Java 8 Features


• Lambda expressions.

Core Java Notes Page 4


• Functional interfaces.

• Stream API.

• Method references.

• Default/static interface methods.

• Optional.
List nums = [Link](1,2,3,4,5); [Link]() .filter(n -> n % 2 == 0)
.forEach([Link]::println);

30. Streams
• filter selects elements.

• map transforms elements.

• sorted orders elements.

• distinct removes duplicates.

• collect gathers results.

• forEach performs an action.


List result = [Link]() .filter(n -> [Link]() > 3) .map(String::toUpperCase)
.toList();

31. Multithreading
• Thread enables concurrent execution.

• Runnable represents a task.

• ExecutorService manages thread pools.

• Synchronization protects shared mutable state.

• Race condition occurs when result depends on timing of concurrent access.


Thread t = new Thread(() -> { [Link]("Running"); }); [Link]();

32. ExecutorService
ExecutorService pool = [Link](3); [Link](() ->
[Link]("Task")); [Link]();

33. JVM Memory


• Heap: objects and arrays are generally allocated here.

• Stack: each thread has stack frames for method calls/local variables.

• Metaspace: class metadata in modern HotSpot JVMs.

• Garbage Collector reclaims heap memory that is no longer reachable.

34. Garbage Collection


Java automatically manages heap memory. Understand object reachability, GC pressure, memory leaks caused by
unintended references, and OutOfMemoryError.

35. Important Keywords


• class, object, new

Core Java Notes Page 5


• this, super

• static, final

• extends, implements

• public, private, protected

• abstract, interface

• try, catch, finally, throw, throws

• synchronized, volatile

• instanceof

Core Java Notes Page 6


36. Core Java Interview Questions
• JDK vs JRE vs JVM?

• Why is Java platform-independent?

• == vs equals()?

• Why is String immutable?

• String vs StringBuilder?

• Overloading vs overriding?

• What are the four OOP pillars?

• Abstract class vs interface?

• ArrayList vs LinkedList?

• HashMap vs Hashtable?

• HashSet vs TreeSet?

• Why override hashCode() with equals()?

• Checked vs unchecked exception?

• throw vs throws?

• final vs finally?

• What is garbage collection?

• Heap vs stack?

• What is a thread?

• Runnable vs Thread?

• What is ExecutorService?

• What are lambda expressions and streams?

37. Revision Cheat Sheet


JDK = development tools + runtime JRE = runtime + JVM JVM = executes bytecode OOP =
Encapsulation + Inheritance + Polymorphism + Abstraction List = ordered / duplicates Set =
unique Map = key/value == = primitive value OR reference identity .equals() = logical/content
equality Checked exception = compiler checked Unchecked = RuntimeException family Heap =
objects Stack = method frames per thread Lambda = concise function expression Stream =
declarative data processing

38. Learning Order


Syntax → Data Types → Conditions/Loops → Methods → Arrays/Strings → OOP → Exceptions → Collections →
Generics → Lambda/Streams → Multithreading → JVM/GC → Practice problems.

Core Java Notes Page 7

You might also like