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 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

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

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

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

OOPS Class Object

Class Object Object: Anything that really exists and can be distinguished from others in world is an “OBJECT“. For example a person, cat, dog, chair etc. Every Object has its own properties and can perform some actions.Consider a person whose name is ‘Rahul’. Rahul exists physically we can consider him as an object. He has properties…

Continue reading