Notes

Working notes on Notes: Join two lists, 2. ListUtils.union(list1,list2);, Get class’s details, ArrayList : capacity & size, that does not equate to size, and == operator checks if both sides reference same object.

UjnotesCC0 1.0

Working notes on Notes: Join two lists, 2. ListUtils.union(list1,list2);, Get class’s details, ArrayList : capacity & size, that does not equate to size, and == operator checks if both sides reference same object.

Join two lists

1. List<String> newList = new ArrayList<String>(listOne);

NewList.addAll(listTwo);.

2. ListUtils.union(list1,list2);

Requires apache commons.

Get class’s details

Get class’s details JavaP -verbose <class-file>

________________.

ArrayList : capacity & size

Initial size is capacity.

that does not equate to size

Actual elements need to be added for size to grow.

Same with Array[].

Java is a computer programming language.

Developed by James Gosling in 1982.

Not purely Object oriented as it supports primitive data types like boolean, byte, int etc.

(this terminology is however subjective).

Both interpreted and compiled.

Machine independent code - as the source code is compiled into bytecode which is then interpreted during runtime.

JRE - Java runtime environment specific to the machine is required to run the code.

JIT - just in time compiler, an optional component that again compiles bytecode into machine code. It optimizes procedure calls that are prioritized. JVM can reuse optimized code instead of interpreting again.

Stack memory holds variables and procedures whereas the heap holds the dynamic objects.

Pointers are Not supported in Java, it uses it internally. Only references are supported.

References are initialized to null.

Numeric variables are initialized to zero and likewise, boolean to false.

Variables are otherwise not initialized by default and usage will result in “variable might not be initialized” during compile time.

Difference between C++ & Java.

  • Compiled v/s interpreted.
  • Machine independent.
  • Pointers.
  • Multiple inheritance.

== operator checks if both sides reference same object

Cannot be overridden.

Compares hashcode.

.equals

Checks if the attributes are equal individually.

Can be overridden.

Usually has to as per the required business logic.

Copy constructor

Constructor with class as parameter.

Sets values separately.

Single try block - multiple catch

As per type of exception thrown.

Finally

Last block of code to be executed after exception has been handled.

May not execute as the try block can crash - by stack overflow or terminate by system.exit().

Final variable

Once assigned - cannot be changed.

If not initialized - can only be set by the constructor.

Final class

Cannot be inherited.

Super

Used to access parent’s variables and methods.

Super.method_1();.

Super.var_1;.

Static

Belong to the class not to the object, hence single copy.

Can be overloaded - differing in params.

Class

Only inner (member) class can be static.

A static nested class can be instantiated without instantiating its outer class.

Inner class can access both static and non-static members of its outer class.

Static inner class can access only static members of its outer class.

OuterClass.InnerClass inner = outer.new InnerClass();

OuterClass.InnerClass innerObject = new OuterClass().new InnerClass();.

Garbage collector frees memory from the heap.

System.gc();.

ClassLoader load class belongs to JRE.

Shallow copy - only creates a new reference.

Deep copy - actually creates a separate copy, properties are set separately.

–.

Strings are immutable, i.e. cannot be altered.

Optimization for String pool in heap.

Multithreading. Simple code as no synchronization is required. Required for concurrency.

Collections. Hashtables and hashmaps have String keys requiring that they must not change.

Singleton class

Private constructor.

Initialized through a static function

That checks if an object exists.

Else creates and returns reference to it.

Singletons written with double-checked locking can be thread-safe.

https://www.baeldung.com/java-singleton-double-checked-locking

Best way. Java enums.

Array initialization

<type> <var>[<n>]

N should be a valid integer - for successful compilation.

-ve is thus allowed

N should be a valid positive integer - for successful run.

Abstract class

Abstract class Syntax: class abstract <class_name>

Cannot be instantiated, only subclassed (i.e. inherited).

As such “incomplete class”.

Can have both abstract and not abstract members.

Interface

Blueprint of a class.

Cannot contain any instances.

Can only have public members.

Methods cannot have any definition.

More ‘abstract’.

Syntax: interface <class_name>

Comparator

Custom comparison method implemented via interface.

Updated: 2026 Aug 19