Reference Types in Java

Reference datatypes in java are those which contains reference/address of dynamically created objects. These are not predefined like primitive data types. Different Reference Types java.lang.ref package provides reference-object classes, which support a limited degree of interaction with the garbage collector. Java has four main different reference types. They are: Strong Reference Weak Reference Soft Reference Phantom … Read more

Local Inner Classe in Java

Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. … These classes have access to the fields of the class enclosing it. Local inner classes are must be instantiated in the block they are defined in. Basic Object Construction and Use in Java Objects come Basic Object Construction and Use in Java in … Read more

Classes and Objects in Java

There are many differences between method overloading and method overriding in java. A list of differences between method overloading and method overriding are given below: Objects have states and behaviors. Example: A dog has states – color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of … Read more

Generics in Java

Generics are a facility of generic programming that extend Java’s type system to allow a type or method to operate on objects of various types while providing compile-time type safety. In particular, the Java collections framework supports generics to specify the type of objects stored in a collection instance. Related Article: Visibility (controlling access to … Read more

Immutable Objects in Java

An object is considered immutable if its state cannot change after it is constructed. Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code Creating an immutable version of a type using defensive copying Some basic types and classes in Java are fundamentally mutable. For example, all array types are mutable, and so are … Read more

Immutable Class in Java

Immutable objects are instances whose state doesn’t change after it has been initialized. For example, String is an immutable class, and once instantiated its value never changes. Example without mutable refs public final class Color { final private int red; final private int green; final private int blue; private void check(int red, int green, int … Read more

Compile time processing using annotation processor

This article is an intro to Java source-level annotation processor and provides examples of using this technique for generating additional source files during compilation. This example demonstrates how to do compile time checking of an annotated element. The annotation The @Setter annotation is a marker can be applied to methods. The annotation will be discarded during compilation … Read more

Annotations in Java

In Java, an annotation is a form of syntactic metadata that can be added to Java source code. It provides data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate. Classes, methods, variables, parameters and packages are allowed to be … Read more

Constructors in Java

While not required, constructors in Java are methods recognized by the compiler to instantiate specific values for the class which may be essential to the role of the object. This topic demonstrates proper usage of Java class constructors. Default Constructor The “default” for constructors is that they do not have any arguments. In case you … Read more

Operators in Java

Operators in Java programming language are special symbols that perform specific operations on one, two, or three operands, and then return a result. The Increment/Decrement Operators (++/–) Variables can be incremented or decremented by 1 using the ++ and — operators, respectively. When the ++ and — operators follow variables, they are called post-increment and … Read more

Iterating through the contents of a Map

Maps provide methods which let you access the keys, values, or key-value pairs of the map as collections. Iterating through the contents of a Map. You can iterate through these collections. Given the following map for example: Map repMap = new HashMap<>();repMap.put(“Jon Skeet”, 927_654);repMap.put(“BalusC”, 708_826);repMap.put(“Darin Dimitrov”, 715_567); Iterating through map keys: for (String key : … Read more

Maps in Java

The java.util.Map interface represents a mapping between keys and their values. A maps cannot contain duplicate keys; and each key can maps to at most one value. Since Maps is an interface, then you need to instantiate a concrete implementation of that interface in order to use it; there are several Maps implementations, and mostly … Read more