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

Java se 8 Repeating Annotations

Repeating Annotations ( Java SE 8) In some cases we need to use Repeated Annotations as per the requirement @Retention(RetentionPolicy.RUNTIME) @interface CustomAnnotation { String city() ; int pin() default 123; } @CustomAnnotation(city = “Vijayawda”) @CustomAnnotation(city = “Chennai”) public static void testAnnotation() { System.out.println(“Custom annotation test”); } If you use repeated Annotations like this compiler will…

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

VarArgs (Variable Arguments)

VarArgs Variable Arguments VarArgs feature introduced in Java se 5. Before introducing this feature if user don’t know how many arguments (similar data type) need to pass we will go for overloading concept or array. Consider this example before java se 5 public class VarargsTest { public static void multiSelect(String one, String two, String three){…

Continue reading

Java 7 new features

Java 7 new Features Some of the important Java 7 features are … String in Switch Statement: Before Java se 7, int or char only allowed for switch case, now in JDK 7 we can use String in switch case statement. String phone = “work”; switch (phone) { case”mobile”:System.out.println(“User selected mobile number”);break; case”work”:System.out.println(“User selected landline…

Continue reading

String in Switch Statement with example

String in Switch Statement with example Before JDK 7, int or char only allowed for switch case, now in JDK 7 we can use String in switch case statement. How this will be helpful in programmer’s perspective? Let’s see with a simple example. Using String in switch case Statement public static void phoneOptions(String phone) {…

Continue reading