Java Basics
Java
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.
JVM
Short for Java Virtual Memory. The virtual machine, on top of the operating system, which runs the compiled Java code.
JDK
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).
Object overhead
In a 32-bit system, int
type is stored using 4 bytes. But Integer
object
will require 16 bytes of memory. The reason for this is, to accomodate for the
object metadata (such as class pointer, flags, lock, size, etc.)
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.
Iterable
interface represents an object that can be used in aforEach
statement.Collection
interface adds common functionliaty such as add, remove toIterable
interface. Provides a common interface for all the implementations of different data structures.List
,Queue
,Set
interfaces extendsCollection
to specific data structures- ArrayList is a dynamic array implementation
PriorityQueue
is an implementation ofQueue
HashSet
is an implementation ofSet
Map
is a similar interface toCollection
. Allows access to items by reference to contents that are used as a keyHashMap
is 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
Originally, JDK provided the ability to group objects through:
Array
which is a native language featureVector
which can grow in size but not shrinkHashtable
which 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.
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.
Interfaces
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.
Abstract classes
A special type of class, which cannot be used to instantiate objects from.
Defined by abstract
keyword. Can only be extended from.
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
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
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
Static members (aka. class members) are used to store data, specific to 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.