Java Basics
-
Java : Interesting and Cool Tricks
1) Java String : concat() vs + operator for concatenation intern() private String str1 = new String(“Hello”); private String str2 = “Hello”; System.out.println(str1 == str2); //false private String str3 =…
-
Java Concurrency : Atomic Wrapper
Java provides atomic classes such as AtomicInteger, AtomicLong, AtomicBoolean and AtomicReference. Objects of these classes represent the atomic variable of int, long, boolean, and object reference respectively which can be atomically updated. class Counter extends…
-
Java 8 Features
Java 8 provides the following features: Lambda Expression Method Reference Default Methods Functional Interface Optional Class String Joiner Stream API Concurrency Enhancements Lambda Expression: A new feature in java 8…
-
Singleton class in Java and how can we make a class as singleton?
Singleton class is a class whose only one instance can be created at any given time in one JVM. Make constructor as private, so that instance cannot be created outside…
-
How can you make a class Immutable?
Declare the class as a final so it can’t be extended. Make all fields private so that direct access will be avoided. Do not implement setter methods for a variables.…
-
What will happen if you store null key in HashMap?
In HashMap, only one null key is allowed. If key of the HashMap is null then it will always be present in the index 0. NullPointerException is thrown if you…
-
Can we use any class as a Map key in HashMap?
Yes. We can use any class as a Map key, however the following points should be considered before using them. If a class overrides an equals() method, it should also…
-
What is the importance of hashcode() and equals() methods?
HashMap uses the key object hashCode() and equals methods to determine the index to put the key-value pair. The methods are also used when we try to get the value…
-
How hashmap works in java?
Hashmap stores a key-value pair in Map.Entry static nested class implementation. HashMap works on hashing algorithm and uses hashcode() and equals() method in put() and get() methods. When we call…
-
Difference between enum and constant in java
Enum is a java keyword, we can use it for a variable to be set of predefined constant value. Before Java 1.5 we used public static final keywords to represent…











