Knowledge / Computer Science and Engineering / Java essentials

Java essentials

Computer Science and Engineering (B.E./B.Tech) · Programming · free study notes for exams, placements and interviews

In short

Java is a class-based, statically typed, object-oriented language whose source compiles to platform-neutral bytecode run by the Java Virtual Machine — "write once, run anywhere". It is the default teaching and placement language in most Indian universities after C, and the production language of Indian banking, telecom and services IT: TCS, Infosys, Wipro, Cognizant, Accenture, HCL, Capgemini, Zoho, Flipkart, PhonePe, Paytm and almost every Spring Boot backend hiring in India. University papers examine OOP in Java, exception handling, multithreading, collections and JDBC; GATE CS does not test Java — its programming section is set in C — but TCS NQT, Infosys InfyTQ, Wipro NTH and all product-company rounds test it directly. Interviews concentrate on the JVM, String behaviour, collections internals, equals/hashCode, concurrency and Java 8 streams.

Explained

Platform, JVM and program structure

Data types, operators and control flow

Classes, objects and OOP as Java implements it

Strings, memory and garbage collection

Exceptions, collections and generics

Threads, functional features and common libraries

Key points

Formulas and examples

Primitive range (signed, n bits): −2n−1 to 2n−1 − 1 — so byte (8 bits): −128 to 127; int (32 bits): −2,147,483,648 to 2,147,483,647
HashMap resize threshold = capacity × load factor (default 16 × 0.75 = 12 entries)
ArrayList growth (Java 8+): newCapacity = oldCapacity + (oldCapacity >> 1) ≈ 1.5 × old
Bucket index in HashMap = (n − 1) & hash, where n = table length (a power of 2)
Default hashCode of a String: s[0]×31n−1 + s[1]×31n−2 + … + s[n−1]
Amortised cost: ArrayList add = O(1) amortised, get = O(1); LinkedList add at ends = O(1), get(i) = O(n)

Example 1 — String hashCode, computed fully. For "Ab": n = 2, 'A' = 65, 'b' = 98. hash = 65 × 31¹ + 98 × 31⁰ = 2015 + 98 = 2113. For "BC": 'B' = 66, 'C' = 67 → 66 × 31 + 67 = 2046 + 67 = 2113. Same hash, different strings — a genuine collision, which is exactly why HashMap also calls equals() after matching the hash.

Example 2 — HashMap resize count. A HashMap is created with the defaults and 30 distinct keys are inserted.

**Example 3 — Integer caching and ==.**

Integer a = 127, b = 127;        // from cache
Integer c = 128, d = 128;        // new objects
System.out.println(a == b);      // true
System.out.println(c == d);      // false
System.out.println(c.equals(d)); // true
int e = 128;
System.out.println(c == e);      // true  (c is unboxed, value compared)

The cache covers −128 to 127 by default; the upper bound can be raised with -XX:AutoBoxCacheMax.

Example 4 — Pass by value with an object.

static void change(StringBuilder sb, String s) {
    sb.append(" World");   // mutates the shared object
    s = s + " World";      // rebinds the local copy only
}
public static void main(String[] a) {
    StringBuilder sb = new StringBuilder("Hello");
    String s = "Hello";
    change(sb, s);
    System.out.println(sb + " | " + s);   // Hello World | Hello
}

Example 5 — Streams over a payroll list. Employees with salaries ₹35,000, ₹52,000, ₹78,000, ₹41,000 and ₹95,000 per month; find the total of those above ₹50,000.

List<Integer> sal = List.of(35000, 52000, 78000, 41000, 95000);
int total = sal.stream().filter(x -> x > 50000).mapToInt(Integer::intValue).sum();
double avg  = sal.stream().mapToInt(Integer::intValue).average().orElse(0);

Filtered: 52,000 + 78,000 + 95,000 = ₹2,25,000. Average of all five = 301,000 / 5 = ₹60,200.

FeatureC++Java
CompilationTo native machine codeTo bytecode, run by the JVM
MemoryManual new/deleteAutomatic garbage collection
PointersFull pointer arithmeticReferences only, no arithmetic
Multiple inheritanceSupported (classes)Interfaces only
Operator overloadingSupportedNot supported (except + for String)
Global functionsAllowedEverything inside a class
CollectionOrderDuplicatesNullAccess / insert
ArrayListInsertionYesYesget O(1), insert mid O(n)
LinkedListInsertionYesYesget O(n), ends O(1)
HashSetNoneNoOne nullO(1) average
TreeSetSortedNoNo nullO(log n)
HashMapNoneKeys unique1 null keyO(1) average
TreeMapSorted by keyKeys uniqueNo null keyO(log n)

Where it is used

Interview questions

Common mistakes

Related topics

Object-oriented programming concepts · Exception handling and debugging · C++ essentials · Functional programming basics · Compilation, linking and memory layout · Python essentials

Open in the Knowledge reader →Find jobs →