Java in a Nutshell
Hello, welcome to this ‘panaromic’ Java guide. You can find easily digestable information on almost all the topics of Java.
When I first started my software engineering career, I was looking for an all-in-one Java guide but I couldn’t have found it. Then I prepared one which benefited me a lot.
A quick update, I’ve just published changes of Java 11 in a Nutshell. Check it out if you’re interested.
If you’re new to software development or looking for a resource to quickly review topics for interview prep, you’re in the right place.
Let’s go!
Topics We Will Cover
- Properties of Java
- Variable Types
- What are JVM, JRE and JDK?
- Object Creation Stages
- Constructor types
- Data Types
- Access Modifiers
- Passing By Value
- Inheritance
- Overloading
- Overriding
- Encapsulation
- Abstraction
- Interface
- Abstract class
- The Relation between Overriding, Upcasting, Overloading, Runtime Polymorphism and Interfaces?
- Association
- Aggregation
- Composition
- I/O
- Serialization & Deserialization
- Exceptions (Try/Catch/Finally)
- Inner Class
- Java 8 Features (Functional Interface, Lambda Expressions, Default methods, Streams, Optional, Collections, DateTime API)
You can find references at the end of the guide. Please let me know if the guide has any incorrect/missing information. Feedbacks are always welcomed.
Properties of Java
- Object oriented: Java is a true object oriented programming language. Except of the primitive data types, everything is a object.
- Platform independent: Java is compiled into a platform independent byte code (unlike C and C++). The byte code is interpreted by the Java Virtual Machine (JVM) on whichever platform it’s being run on.
- Simple: Java is a high-level programming language and is closer to the human language. Thus it’s easier to learn.
- Interpreted: Java byte code is translated (by JVM) on the fly to native machine instructions.
- High performance: With Just-In-Time (JIT) compilers, Java enables high performance.
Variable Types
- Local variables: Variables defined inside methods.
- Instance variables: Variables within a class, outside any method.
- Class variables: Static variables within a class, outside any method.
What are JVM, JRE and JDK?
- When we write Java code, java compiler (javac) converts it to byte code. Javac is included in Java Development Kit (JDK).
- The byte code is then converted to machine-specific instructions via Java Virtual Machine (JVM).
- Machine-specific instructions are then run on the computer by Java Runtime Environment (JRE).
- JVM = Execution of a Java program. Because Java is a platform-independent language, it needs a virtual machine environment (interpreter) to run, which is provided by JVM. So, JVM is used to run Java code. Whenever we run a Java program either using JRE or JDK, it goes to JVM to be executed.
- JRE = JVM + Library Classes. JRE provides environment to run Java programs on our machines.
- JDK = JRE + Development Tools. JDK is used to develop and run Java code. It is used by developers.
Object Creation Stages
Apple myApple = new Apple();
- Declaration: A variable is declared with an object type and a variable name.
Apple myApple;
- Instantiation: ‘new’ keyword is used to create object.
= new
- Initialization: The call to a constructor.
Apple();
Constructor Types
- Default constructor: A constructor without any argument. Also known as no argument constructor.
- Parametrized constructor: A constructor with parameters.
Data Types
- Primitive data types: There are 8 of them;
boolean
,char
,short
,byte
,int
,long
,float
,double
. - Non-primitive data types: The rest of the data types, i.e. reference/object data types.
Access Modifiers
default
is visible to the package.private
is visible to the class only.public
is visible to the world.protected
is visible to the package and sub-classes.
If no access modifier is denoted, default
is used as a default.
Non-access modifiers are static
, final
, abstract
, synchronized
and volatile
.
Passing By Value
Java is a pass-by-value language. Referencing here, check the below example.
Inheritance
- Definition: Inheritance is a mechanism where one class acquires all the properties of another class.
- Benefits: Inheritance promotes code reusability and enables method overriding so that run-timepolymorphism can be achieved. Run-time polymorphism is briefly, “call to a method is resolved at runtime” (whether it’s childs method or parents method).
- Extra details:
private
methods, variables and constructors are not inherited whilestatic
field/methods are inherited (public static
variables aren’t a good practice). Inheritance represents IS-A relationship, also known as parent-child relationship. One class can only extend only one other class. Multiple inheritance is not possible in Java (a response about all classes inheriting from Object, it’s briefly chain of inheritances). Reason for that is, say class A and B havetalk()
method and C inherits both. What happens when we callc.talk()
would be unresolved.
Overloading
- Definition: If a class has more than one methods with the same name but with different type or number of arguments, it’s called overloading.
- Benefits: It provides cleaner code. Also, it enables to call a similar method with different parameters.
- Extra details: We can’t only change the return type of a method as this may cause confusion. Ex.
sum()
returnsint
, but overloading only with return type asdouble
will cause confusion on which method to call.
Overriding
- Definition: If a subclass provides a specific implementation of a method that is already provided by its parent class, it’s called overriding.
- Overriding rules: The methods must have the same name and the same signature. Also, the two classes must have an IS-A relationship (i.e inheritance).
- Overriding benefits: It is useful to achieve runtime polymorphism and to implement interface methods.
- Extra details: We can’t override
static
methods because overriding is for instance methods (not statics, not field variables), statics are hidden if the subclass has the same signature. We can override overloaded methods.@Override
signature is not compulsory but it is a good practice to use as it provides additional checks. We can change the scope of overridden method in the subclass to a broader type like fromprotected
topublic
but not vice versa.
Encapsulation
- Definition: Encapsulation is data hiding. Methods or variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class.
- Benefits: We prevent direct access to instance & class variables thus the class will have more control. We can make the class read-only or write-only (by not providing getter or setter). Also, encapsulated classes are easy to (unit) test.
- Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction.
- Encapsulation rules: Declare the variables of a class as private. Provide public setter and getter methods to modify and view the variables’ values.
Abstraction
- Definition: Abstraction is hiding details and showing only the functionality.
- Another definition: Abstraction is providing user-level details while hiding the implementation details.
- Example: A good analogy is a coffee machine. As a coffee user all you want is to click a button or two to get your favorite fresh coffee. You don’t want to know how coarse to grind the coffee, the ideal water temperature and what’s the ideal coffee to water ratio. All you want is to get your coffee with your buttons. All the details are hidden and you only see the functionality with buttons, this is the abstraction.
- Example: A technical example can be user clicking “Order” button, without caring about what happens in the background when he clicks.
- Benefits: It helps to reduce programming complexity and effort.
There are two ways to achieve abstraction; interface (100% abstraction) and abstract class (0–100% abstraction).
Interface
- Interface is a blueprint of a class. It has static constants and abstract methods.
- Interface represents IS-A relationship.
- Since all methods are abstract, none of the methods can have a body.
- Just like the abstract class, it cannot be instantiated.
- Since Java 8, interfaces can have default and static methods.
- Since Java 9, interfaces can have private methods.
interface myInterface {
// declare constant fields
// declare methods (they are abstract by default)
}
- Multiple inheritance is not supported by Java because of ambiguity but it’s possible when the parents are interfaces.
Abstract class
A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
abstract class Bike{ // abstract class
abstract void use(); // no method body
} class Bianchi extends Bike{
void use(){ System.out.println("Bike is on the use."); }
public static void main(String args[]){
Bike obj = new Bianchi();
obj.use();
}
}
The Big Picture: What’s the relation between Overriding, Upcasting, Overloading, Runtime Polymorphism and Interfaces?
Relation Group 1: Overloading & Compile Time Polymorphism
It’s compile time polymorphism when a call to an overloaded method is resolved at compile time. Also known as static binding and early binding.
Relation Group 2: Overriding & Runtime Polymorphism
It’s run time polymorphism when a call to an overridden method is resolved at runtime. Also known as dynamic binding and late binding.
StaticType x = new DynamicType();
is the format we define Java objects. StaticType is determined at compile time, DynamicType is determined at runtime. For the exampleFruit f = new Apple()
the type of f isFruit
at compile time,Apple
at runtime. If apple overridescolor()
method, it’s a call to the parent’scolor()
method at compile time, but then changed as childs’color()
method at runtime (runtime polymorphism).
class A {}
class B extends A {}//Upcasting
A a = new B();
Upcasting is a cast from a derived class to a more general base class.
Dog dog = new Dog();
Animal animal = (Animal) dog;
If the reference variable of Parent class refers to the object of Child class, it is known as upcasting.
Extra Details
Downcasting is a cast from a base class to a more specific class.
Animal animal = new Dog();
Dog dog = (Dog) animal;
- Java permits an object of a subclass type to be treated as an object of any superclass type. This is called upcasting.
- Upcasting is done automatically, while downcasting must be manually done by the developer.
- Upcasting and downcasting are NOT like casting primitives from one to the other (a good resource).
Association
- Association refers to the relationship between multiple objects.
- Aggregation and composition are two types of association.
Aggregation
- If a class have an entity reference, it is known as aggregation.
- Aggregation represents HAS-A relationship.
- Aggregation benefits: Promotes code reusability. In the above example, we just simply put use of Address class just with a line of code.
Composition
- Definition: Composition is the strong type of association.
- Composition rule: An association is said to composition if an object owns another object and another object cannot exist without the owner object.
- Example: Think about lego homes. A lego home can’t exist without single lego(s), but legos can exist without a lego home.
I/O
Stream is a flow of data from an input (source) to an output (destination).
- InputStream: Read data from a source.
- OutputStream: Write data to a destination.
- Extra details:
stdin
andstdout
in C++,System.in
andSystem.out
in Java are examples of (standart) streams.byte
streams are 8-bit, character streams are 16-bit unicode format used to perform I/O operations.
Serialization & Deserialization
- Definition: Serialization is the conversion of an object to a series of bytes. The byte stream can then be deserialized — converted into a replica of the original object (source).
- Benefit: The object can be easily saved to persistent storage or streamed across a communication link.
- Extra details: In Java, the serialization mechanism is built into the platform, but you need to implement the Serializable interface to make an object serializable.
Exceptions
An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.
Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.
- Checked exceptions: A checked exception is an exception that is checked (notified) by the compiler at compilation-times, also known as compile time exceptions. These exceptions cannot simply be ignored, the programmer should take care of (handle) these exceptions.
- Unchecked exceptions: An unchecked exception is an exception that occurs at the time of execution, also known as runtime exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.
Try/Catch and Finally
- With the help of try/catch block we are able to catch exceptions.
- A try/catch block is placed around the code that might generate an exception.
- Code within a try/catch block is referred to as protected code.
try {
// Protected code
} catch (ExceptionName e1) {
// Catch block
}
- Definition: Finally block always executes, irrespective of occurrence of an Exception.
- Rule: It’s placed after try or catch block.
- Benefit: Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.
try{
// Protected code
}catch(IOException | SQLException e){//multiple exception syntax
// Catch block
}finally{
// The finally block always executes.
}
Inner Class
- Definition: Inner class is a class inside a class.
- Inner classes can be defined inside another class (nested inner class) or another classes’ method (method local inner class).
class OuterClass {
class NestedInnerClass {
}
}
- Anonymous inner class is a nameless inner class. An inner class declared without a class name is known as an anonymous inner class.
- In case of anonymous inner classes, we declare and instantiate them at the same time. Generally, they are used whenever you need to override the method of a class or an interface.
AnonymousInnerClass anonInner = new AnonymousInnerClass() {
public void my_method() {
// ...
}
};
- Nested Inner classes vs Anonymous Inner Classes: If we’re going to use a method only once, there’s no need to write it as a seperate class. Also we can prevent inner class to be used elsewhere. On the other hand, we can use nested inner class if we’re going to use it in other places (source).
Java 8 Features
Functional Interface
- Functional interfaces are also called Single Abstract Method interfaces (SAM Interfaces). As name suggest, they permit exactly one abstract method inside them. Java 8 comes with
@FunctionalInterface
annotation to ensure the restrictions of functional interfaces are applied. Also, sincedefault
methods are notabstract
you’re free to adddefault
methods to your functional interface as many as you like. Withdefault
methods we can definedefault
implementation of a method in an interface, so classes thatextend
the interface can directly use the method without providing any extra implementation information. Details are provided in the Default methods section.
Lambda Expressions
- Definition: A lambda expression (or function) is just an anonymous function, i.e., a function with no name and without being bounded to an identifier.
- Syntax:
(parameters) -> expression
,(parameters) -> { statements; }
,() -> expression
- Benefits: Lambdas provide more brief and readable code. It eliminates the need of creating anonymous classes. The comparison will be provided after the examples.
- Anonymous class vs Lambda expression: Lambdas eliminate the need of writing anonymous classes. Below is the comparison of the codes(source):
Default methods
Java 8 allows you to add non-abstract methods in interfaces. These methods must be declared default methods. Default methods were introduces in java 8 to enable the functionality of lambda expression. Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.
If a class willingly wants to customize the behavior of move()
method then it can provide it’s own custom implementation and override the method.
Streams API
A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
Intermediate Operations
- map: The map method is used to map the items in the collection to other objects according to the Function passed as argument.
- filter: The filter method is used to select elements as per the Predicate passed as argument.
- sorted: The sorted method is used to sort the stream.
Terminal Operations
- collect: The collect method is used to return the result of the intermediate operations performed on the stream.
- forEach: The forEach method is used to iterate through every element of the stream.
- reduce: The reduce method is used to reduce the elements of a stream to a single value.
The reduce method takes a BinaryOperator as a parameter.
Optional
- To prevent
NullPointerException
and too manynull
checks, Optional class is introduced. - Optional class has multiple methods to manage null’s.
empty()
method creates an empty optional.ofNullable(myVariable)
method returns non-empty optional if a value (myVariale) is present, empty optional if the value is absent.of()
converts a variable to an optional, does not accept null values.
Collections
A Java collection framework provides an architecture to store and manipulate a group of objects.
Iterable
- The Iterable interface is the root interface for all the collection classes.
- Contains only one abstract method:
Iterator<T> iterator()
Collection
- The Collection interface extends the Iterable interface and therefore all the subclasses of Collection interface also implement the Iterable interface.
List
- List is ordered.
- We can store duplicates.
ArrayList
- ArrayList is ordered and non-synchronized.
- Uses a dynamic array to store duplicate elements and different data types.
- Elements can be randomly accessed.
LinkedList
- LinkedList is ordered and non-synchronized.
- It uses a doubly linked list.
- Data manipulation is fast (no shifting required).
Vector
- Vector uses a dynamic array to store the data elements.
- It is similar to ArrayList. However, It is synchronized and contains many methods that are not the part of Collection framework.
Stack
- The stack is the subclass of Vector.
- It implements the LIFO (last-in-first-out) data structure, stack.
- The stack contains all of the methods of Vector class and also provides its methods like
push()
,pop()
andpeek()
which defines its properties.
Queue
- Queue interface maintains the FIFO (first-in-first-out) order.
- It is similar to an ordered list.
- The implementations are
PriorityQueue
,Deque
, andArrayDeque
.
PriorityQueue
- Processes elements with respect to their priorities
- PriorityQueue doesn’t allow null values to be stored in the queue.
- PriorityQueue iterator does not traverse in any particular order, for ordered purpose we should use poll() method (details).
ArrayDeque
- Implements the Deque(double ended queue) interface.
- Unlike queue, we can add or delete the elements from both ends.
- ArrayDeque is faster than ArrayList and Stack and has no capacity restrictions.
Set
- Unordered set of elements, duplicates not allowed
- We can store at most one null value in Set
- Set is implemented by HashSet, LinkedHashSet, and TreeSet.
HashSet
- Uses hash table for storage
- Hashing is used to store elements
- Each element is unique
LinkedHashSet
- Represents LinkedList implementation of Set Interface.
- Insertion order is maintained and null elements are permitted.
- Each element is unique.
SortedSet Interface
- Elements are sorted in the increasing (ascending) order.
- Can be instantiated as
SortedSet<datatype> set = new TreeSet();
TreeSet
- Java TreeSet class implements the Set interface that uses a tree for storage.
- Like HashSet, TreeSet also contains unique elements. However, the access and retrieval time of TreeSet is quite fast.
- The elements in TreeSet stored in ascending order.
DateTime API changes
A big part of developer community has been complaining about Date and Calendar classes. Reasons were many such as hard to understand, hard to use and not flexible. Date class has even become obsolete and java docs suggest to use Calendar
class instead of Date
class. And on top of all, Date comparison is buggy and I have also faced such issue in past.
Date
class has even become obsolete. The new classes intended to replace Date class are LocalDate
, LocalTime
and LocalDateTime
.
LocalDate
class represents a date. There is no representation of a time or time-zone.LocalTime
class represents a time. There is no representation of a date or time-zone.LocalDateTime
class represents a date-time. There is no representation of a time-zone.- For representing the specific timestamp ant any moment, the class needs to be used is
Instant
. TheInstant
class represents an instant in time to an accuracy of nanoseconds. Duration
class is a whole new concept brought first time in java language. It represents the time difference between two time stamps.
Source
- https://www.tutorialspoint.com/java/java_tutorial.pdf
- http://www.tutorialspoint.com/java/java_quick_guide.htm
- https://www.geeksforgeeks.org/differences-jdk-jre-jvm/
- https://www.tutorialspoint.com/java/java_encapsulation.htm
- https://www.guru99.com/java-data-abstraction.html
- https://www.javatpoint.com/aggregation-in-java
- https://www.tutorialspoint.com/Association-Composition-and-Aggregation-in-Java
- https://www.javatpoint.com/inheritance-in-java
- https://www.javatpoint.com/runtime-polymorphism-in-java
- http://www.cs.utexas.edu/~cannata/cs345/Class%20Notes/14%20Java%20Upcasting%20Downcasting.htm
- http://mindprod.com/jgloss/upcasting.html
- http://mindprod.com/jgloss/downcasting.html
- https://www.javatpoint.com/corejava-interview-questions
- https://www.tutorialspoint.com/java/java_files_io.htm
- https://stackoverflow.com/questions/447898/what-is-object-serialization
- https://www.tutorialspoint.com/java/java_exceptions.htm
- https://www.tutorialspoint.com/Flow-control-in-a-try-catch-finally-in-Java
- https://www.geeksforgeeks.org/inner-class-java/
- https://www.tutorialspoint.com/java/java_innerclasses.htm
- https://www.javatpoint.com/interface-in-java
- https://www.javatpoint.com/abstract-class-in-java
- https://www.geeksforgeeks.org/stream-in-java/
- https://kodedu.com/2014/10/java-8-optional-yeniligi/
- https://www.edureka.co/blog/java-collections/#framework
- https://www.javatpoint.com/collections-in-java
- https://howtodoinjava.com/java-8-tutorial/
- https://howtodoinjava.com/java8/date-and-time-api-changes-in-java-8-lambda/