java fundamentals

24 Jul 2026 ยท Stone Liu

Given that I have an upcoming Java interview, I think it would be a nice time to catergorize and index some notes about this programming language. Java provides a bunch of features including its strong emphasis on Object Oriented Programming paradigm. Its also secure in the fact that you can share java programs without given the underlying source code.

1. What is the JVM

The Java Virtual Machine enables java source code to be run on all platforms/OSes given that they have a compatiable JVM installed. It acts as an intermediate interpreter that takes bytecode files, often referred to as .class files from the javac compiler. It then turns the bytecode files into machine code that can be directly ran on the underlying hardware. It uses techniques such as (JIT) Just in time compliation to make the execution of specific methods faster.

2. Strings in Java

Strings are immutable objects that store a sequence of characters. Java provides access to a shared String pool to optimize memory usage across an application by storing the literals of the string for reuse. Due to their immutable nature, they are sometimes less memory efficient than StringBuilders which act as mutable representations of strings in the case of very extensive writing of data. Strings are obviously thread-safe since they are immutable. The same cannot be said about StringBuilders since two threads can call the same method at the same time, leading to race conditions. For the case of multi-threaded environments, use StringBuffer since the methods are thread safe (synchronized).

3. Class Variables

Class variables are static variables that are stored within the Class itself. That is, it can be referenced without instantiating an object of a particular class. Instance variables require instantiation. Static variables thus have only one single reference and is shared among all objects instead of instance variables.

4. Stack Vs. Heap

Stack memory is typically allocated when a function is called. It pushes things like the arguments of the function, local variables, and return addresses. Once the function returns, everything on the stack is popped off meaning memory management is automatic. Heap memory is different since it can be accessed globally across the entire program. This is typically done when the size of something is unknown at compile-time, for example a resize-able array list.

Trivia

Explain the difference between == and .equals() for objects, and why this matters for String and custom classes.

`==` tests for aliasing equality, as in do the two objects in question share the same memory address? `.equals` method is a method that is usually meant to be overriding objects with your own equality tests.

Override equals() and hashCode() for a Point class with x and y fields. Explain why you must override both together.

You must override both if you want to use it in any hash data structure such as a hashset or hashmap. Two objects that are equal according to the `equals` method must also produce the same hash as obtained by the `hashCode` function.

What's the difference between abstract classes and interfaces? When would you use one over the other?

Abstract classes are classes that cannot be instantiated but are defined to be a class in which other sub classes can inherit methods, properties/fields, and overrride/overload methods. Interfaces are contracts that provide a set of methods must be implemented by a concrete class. Abstract classes are primarily used for IS-A relations on objects. Like a Cat IS-AN animal. Interfaces should be used when you want to abstract away the implementations of classes to just their public methods.

Explain method overloading vs overriding, and what happens with overloading when autoboxing/varargs are involved

Overloading means to have a method on a class with the same name but with different arguments in the method signature. Overriding means to override a specific method either as a subclass or on the interface with a new implementation. Overriding does not change the method signature. You cannot overload methods with its object counterpart. You can change varargs to its array counterpart though.