tech-notes-and-questions

Basic Java

Topics

Comparisons


</br>

Topics

1. Inheritance

The property that allows a subclass to inherit the properties (fields, methods, constructors) of its super class. Benefits:

2. Polymorphism

The property of allowing us to perform a single action in different ways. Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):

Method overloading VS Method overriding

3. Encapsulation

The property of bundling the data (fields, methods) inside the object & exposing only the required components outside the object. Its done by “private” keyword.
Benefits:

4. Dependency Injection

5. Inversion of Control (IoC)

6. Garbage Collectors

Garbage collectors (GC) manage memory by automatically deleting unused java objects.

1. Serial GC - single thread does the garbage collection, useful in applications with small data sets where simplicity is more important than performance JVM Option: -XX:+UseSerialGC

2. Parallel GC - multiple threads do the garbage collection, focusing on maximizing application throughput by minimizing the time spent in garbage collection. Useful for applications with higher throughput as priority, and short pause times are less critical JVM Option: -XX:+UseParallelGC

3. Concurrent Mark Sweep GC - uses multiple threads to collect garbage while the application is running, minimizing the pause times. Suitable for applications that require low-latency and can afford to trade some throughput for shorter pause times JVM Option: -XX:+UseConcMarkSweepGC

4. G1 GC - designed to replace the CMS GC, offering more predictable garbage collection pauses. It divides the heap into regions and performs garbage collection incrementally, allowing it to prioritize regions with the most garbage. Suitable for applications requiring a balance between throughput and low-latency, with a focus on predictable pause times JVM Option: -XX:+UseG1GC

5. Z GC - scalable, low-latency garbage collector designed to handle huge heaps with minimal pause times. It performs most of its work concurrently, ensuring that pause times remain below 10ms, even for heaps of several terabytes. Suitable for applications that require extremely low-latency and can operate with huge heaps JVM Option: -XX:+UseZGC

6. Shenandoah GC - concurrent, low-latency with concurrent compaction JVM Option: -XX:+UseShenandoahGC

7. Epsilon GC (No-op GC) - The Epsilon GC is a no-op garbage collector that does not reclaim memory. It is designed for testing and benchmarking purposes where you want to see the impact of GC on performance without any actual garbage collection. Suitable for performance testing where garbage collection is not required or for very short-lived applications where memory is not a constraint JVM Option: -XX:+UseEpsilonGC


</br>

Comparisons

1. Abstract Classes Vs Interface

Abstract Classes Vs Interface

2. STATIC vs FINAL

3. HashMap Vs ConcurrentHashMap

| Hashmap | ConcurrentHashMap | |————————————————————————————————–|———————————————————————————————————–| | Allows one null key, multiple null values | Doesn’t allow null key or null values | | Not thread-safe for concurrent data modifications via multiple threads | Thread safe. Lock striping mechanism in segments (map is divided into segments and each has its own lock) | | Faster in single threaded applications | Slower in single threaded applications because of the additional lock mechanisms in the map segments | | fail-fast iterators (throws exception if data is tried to modified after declaring the iterator) | fail-safe iterators (concurrent modification is allowed after the iterator is declared) |

4. JDK vs JRE vs JVM

JDK = Java Development Kit
JRE = Java Runtime Environment
JVM = Java Virtual Machine

5. String vs StringBuilder vs StringBuffer

6. CallByValue vs CallByReference

1. Call by Value (Primitive Data Types): When a method is called with a primitive data type (e.g., int, char, double), Java passes a copy of the value. Any changes made to the parameters inside the method will not affect the original variables.

2. Call by Value (Object References): When passing objects to methods, Java passes the reference by value. This means that while the reference to the object is copied, both the original and copied references point to the same object in memory. Therefore, if you modify the object via the reference inside the method, the changes will reflect in the original object. However, reassigning the reference inside the method does not affect the original reference.

7. Static Classes vs Static Methods vs Static fields

The static keyword in Java and many other object-oriented programming languages has specific applications when used with classes, methods, and fields. Here’s how it works in each context:

Static Classes (nested)

OuterClass.StaticNestedClass nestedObj = new OuterClass.StaticNestedClass();


#### Static Methods
- A `static` method belongs to the class rather than instances (objects) of the class. You can call a static method directly using the class name without creating an object of the class.
- Static methods cannot access instance variables or methods directly; they can only access other static members (methods or fields) of the class. This is because static methods do not have access to the instance (`this` reference) of the class.
- This is the first block of code that's executed when a class is loaded.
- We cannot override static methods.
```java
class ExampleClass {
    static void staticMethod() {
        System.out.println("This is a static method.");
    }
}

ExampleClass.staticMethod(); // Call static method without creating an object

Static Fields (variables)

ExampleClass obj1 = new ExampleClass(); ExampleClass obj2 = new ExampleClass(); System.out.println(ExampleClass.staticCounter); // Output will be 2



### 8. Final Classes vs Final Methods vs Final Fields
The `final` keyword in Java can be applied to classes, methods, and fields, with specific behaviors associated with each:

#### Final Classes
- When a class is declared as `final`, it **cannot be subclassed** (i.e., no other class can extend it).
- This is often used for security or design reasons to prevent modification through inheritance.
```java
public final class Math {
    // Class implementation
    /* 
    Since Math is a final class, 
    you cannot create a subclass like 
        class MyMath extends Math.
     */
}

Final Methods

public class ChildClass extends ParentClass { // The following would cause a compile-time error: // public void display() { // System.out.println(“Attempting to override final method.”); // } }


#### Final Fields
- A field declared as `final` cannot be modified once it has been initialized. In other words, it makes the field a constant.
- For primitive types, the value is set once and cannot change.
- For reference types (objects), the reference to the object cannot be changed, but the object itself can still be modified (unless it is immutable).
```java
public class Example {
    public static final int MAX_VALUE = 100;  // Constant
    public final String name;

    public Example(String name) {
        this.name = name;  // Initialized in the constructor
    }
}
/*
    In the example above, MAX_VALUE is a constant and cannot be changed
    While name is a final instance variable that must be initialized either when declared or in the constructor, 
        and once assigned, it cannot be reassigned.
 */