How to create immutable class in Java?

How to create immutable class in Java? Immutable class is a class which once created, its contents cannot be modified. Immutable objects are the objects whose state cannot be modified once it is created. Best example to explain the difference between mutable and immutable is String and StringBuffer. String is immutable class (Refer String is…

Continue reading

String is immutable or final in Java

String is immutable or final in Java? One of the Interviewers favourite question, How String plays an important role in every program, similarly in every interview questions on String will be there 🙂 😉 . Let’s check the reasons behind why String is immutable of final. Security: String is used in many places like in…

Continue reading

String Pool

Almost in every program, String is being used. When we create a string, it is stored in String pool, as name indicates it is a pool of Strings stored in Java Heap Memory. We can create String in 2 ways By using new operator, String v1 = new String(“Test”); Directly assign the value using Double…

Continue reading

Generics

Generics Java Generics features were introduced from Java Se 5 onwards. Before Java 5, we can store any type of objects in collection i.e. non generic. With inclusion of Generics we need to store only specific type of objects only. Let’s look at the benefits of generics Compile time checking: Generics allows you to check…

Continue reading

Custom Annotation

Custom Annotation Similar to interface we can define Custom Annotations or user defined annotations simply by adding @interface in the declaration. For example: @interface CustomAnnotation{} Here, CustomAnnotationis the custom annotation name. While writing the custom annotations few points has to be considered. Method should not have any throws clauses, Similar to interface only declarations available…

Continue reading

Annotations Java se 5

Annotations in java Annotations in java, a form of metadata, provide data about a program.Java annotations do not directly affect the execution of your code. The main purposes of Java Annotations are Annotations provides information to compiler for detecting errors or suppress warnings etc.(Compiler Instructions) Annotations are used at build time and deployment time processing…

Continue reading

Java 5 features

Java 5 Features   Some of the important  Java  5 features  are Enumeration or enum Autoboxing Unboxing Static Import Var args Variable Argument Annotations Generics Click on the above links to view the detailed explanation.

Continue reading

Enumeration or Enum

Enumeration or Enum Enum or Enumeration is a type like a class or interface that contains set of fixed constants. Syntax: enum EnumName{ Constant1, Constant2, Constant3….} Example:   public enum directions{ NORTH, SOUTH, EAST, WEST } //Internally Enum will look like this public class Directions extends  java.lang.Enum { public static final Directions NORTH= new Directions (); public static…

Continue reading

Autoboxing and Unboxing

Autoboxing and Unboxing The automatic conversion or encapsulation (boxing) of primitive data type (Ex byte, char, int, double, Boolean etc.) into its equivalent wrapper class objects (Ex Byte, Character, Integer, Double, Boolean etc.) is known as Autoboxing and reverse process is known as Unboxing i.e. automatic conversion of Wrapper class objects to its corresponding primitive…

Continue reading

Static Import

Static Import The static import feature of Java 5 makes the programmer’s life a little bit simpler by accessing any static member of a class directly. No need of class name at all directly access by the name If you want to access any static member of a class often, by using this feature makes…

Continue reading