A programming language that is built around
object-oriented paradigm.
Everything is an object in Java. Objects can be instiated from a class using
the unary operator: new. All classes are an extension of the Object class.
Short for Java Virtual Memory. The virtual machine, on top of the operating system, which runs the compiled Java code.
Short for Java Development Kit. The set of tools to compile the Java programs.
Memory is allocated implicitly (variable declaration or new operator) and
deallocated implicitly (by the runtime).
Terminology
Section titled “Terminology”A template for a object, giving information about state and actions. State of an object is represented by variables and behavior is represented by methods.
The class definition contains a constructor method which is a special static
method used to instantiate objects. Inside methods this keyword is used to refer to the current object
instance.
Interface
Section titled “Interface”Defines the structure of a class. Can include state variable definitions but not values. Can include method signatures. After Java 8, can also include method body.
Child class
Section titled “Child class”Aka. sub class, derived class. A class extending another class (all public and
protected members), which is called its super class. Members can be overridden
by defining a member again in the sub class. New members can be declared.
super keyword can be used to access methods of super class.
Abstract class
Section titled “Abstract class”A special type of class, which cannot be used to instantiate objects from.
Defined by abstract keyword. Can only be extended from.
Abstract methods
Section titled “Abstract methods”Can only be defined inside abstract classes. Defined by abstract keyword. Any
child class must either override the abstract method or declare itself abstract
Access modifiers
Section titled “Access modifiers”In Java, access modifiers control visibility and accessibility of classes, methods, and variables:
For classes:
public: Class is visible everywheredefault(no modifier): Class is only visible within same package
For methods:
public: Accessible from any other classprotected: Accessible within same package and by subclassesdefault: Only accessible within same packageprivate: Only accessible within declaring class
For attributes:
public: Accessible from any classprotected: Accessible in same package and subclassesdefault: Only accessible in same packageprivate: Only accessible within declaring class
Non-access modifiers
Section titled “Non-access modifiers”For classes:
final: Class cannot be inheritedabstract: Class cannot be instantiated and may have abstract methodsstrictfp: Class uses strict floating-point calculations
For methods:
final: Method cannot be overriddenabstract: Method has no implementation and must be implemented by subclassessynchronized: Method can only be accessed by one thread at a timenative: Method is implemented in platform-dependent codestrictfp: Method uses strict floating-point calculations
For attributes:
final: Attribute cannot be changed after initializationstatic: Attribute belongs to class rather than instancetransient: Attribute will not be serializedvolatile: Attribute value may be changed by multiple threads
Static members
Section titled “Static members”Aka. class members. Used to store data on a class
instead of an instance. There can be static variables and static methods. Static
methods cannot access non-static variables and cannot call non-static methods.
this keyword is not available inside static methods.
Type Casting
Section titled “Type Casting”The process of converting one data type to another. Can be implicit or explicit.
If a wrong cast operation is performed on a primitive type, it will cause a compile error or loss of accuracy. If a wrong cast is done on a reference type, it will cause a runtime error ClassCastException.
Upcasting
Section titled “Upcasting”Casting a subclass to a superclass. Implicit and safe. Used in polymorphism to call overridden methods.
Example:
Animal animal = new Dog(); // Upcastinganimal.makeSound(); // Calls Dog's implementationDowncasting
Section titled “Downcasting”Casting a superclass to a subclass. Explicit and requires a cast operator. Used to access subclass-specific methods or properties.
Example:
Animal animal = new Dog();Dog dog = (Dog) animal; // Downcastingdog.fetch(); // Access subclass-specific methodUpcasting is common in method arguments and collections. Downcasting is used when subclass-specific behavior is needed.
Object overhead
Section titled “Object overhead”In Java, objects have additional memory overhead due to metadata stored alongside the actual data.
For example, in a 32-bit system, a primitive int requires 4 bytes of memory; An Integer object, requires 16 bytes. The extra memory is used to store object metadata:
- class pointer: A reference to the class metadata that defines the object’s structure and behavior.
- flags: Indicators used to store object-specific state information, such as garbage collection status.
- synchronization locks: Mechanisms to manage thread-safe access to the object. More on this here.
- object size information: Data about the memory footprint of the object.
These metadata are essential for the Java Virtual Machine (JVM) to manage objects effectively.
| Object Type | Memory Overhead (Bytes) | Notes |
|---|---|---|
| String | 44 | Includes length, hash code, and character array overhead. |
| HashMap | 48 | Includes hash table structure and bucket overhead. |
| Array | 16 | Includes array length and element type information. |
| LinkedList | 24 | Additionally, each item has 24 bytes overhead. |
| ArrayList | 32 | Includes capacity overhead for resizing and internal array storage. |
Collections framework
Section titled “Collections framework”A Java framework that includes production-grade implementations for commonly used data structures. The implementation is done using interfaces, abstract classes and (regular) classes.
Iterableinterface represents an object that can be used in aforEachstatement.Collectioninterface adds common functionliaty such as add, remove toIterableinterface. Provides a common interface for all the implementations of different data structures.List,Queue,Setinterfaces extendsCollectionto specific data structures- ArrayList is a dynamic array implementation
PriorityQueueis an implementation ofQueueHashSetis an implementation ofSetMapis a similar interface toCollection. Allows access to items by reference to contents that are used as a keyHashMapis an implementation of Map
All these interfaces and classes are implemented around generics. Generics allow them to be created with compile-time types.
Pre-Collection
Section titled “Pre-Collection”Originally, JDK provided the ability to group objects through:
Arraywhich is a native language featureVectorwhich can grow in size but not shrinkHashtablewhich is an array with 2 parts: a hashcode and the actual data. Hashcode is generated from the content of the data. Data is accessed using the hashcode.
But they didn’t have a common interface.