Java Interview Questions.

Dr. Rajeev Rathor
3 min readFeb 11, 2021

Why String is immutable or final in Java

There are several benefits of String because it’s immutable and final.

  • If String remain mutable then if any object have reference of mutable String, then string can be modified at any point of time and changes will be reflected everywhere that screwed up the entire features.
  • String is immutable that why it keep important data like configs, urls, certificates, secrete key , passphrase etc.
  • String Pool is possible because String is immutable in java.
  • Due to its immutable nature, String is used as safe Key for HashMap or Cache.
  • It increases security because any hacker can’t change its value and it’s used for storing sensitive information such as database username, password etc.
  • Since String is immutable, it’s safe to use in multi-threading and we don’t need any synchronization.
  • Strings are used in java classloader and immutability provides security that correct class is getting loaded by Classloader.

Different way of creating String.
Difference between String, StringBuffer and StringBuilder?
What is String Pool?
What does String intern() method do?
Diff between String pool, new String(), StringBuilder, StringBuffer

Does String is thread-safe in Java?: Because String is immutable. If N threads reading value can read same value. But Try to modify then new String will create and scope limited to thread that modify.

Why String is popular HashMap key in Java?: Because its immutable natures.

Static Access Modifier

  • When we declared any variable with static keyword is known as static variable. Static variable is a class level variable. When we want to refer common property to all the objects then we use static variable
  • Static method cannot override because overriding is happen at run time and on the basis of object.
  • static variable is possible to declare in interface.
  • Static variable and block initialize first on loading of class in JVM. If Static block or variable initialization fail or throw exception, cause NoClassDefFoundError exception will be encountered.
  • Synchronized static method or block can be accessed by only one thread at at a time. Even though 100s of object exist for same class those are assigned individually to all threads. Why because static method or block is class member not a object part.
  • Static method can be overloaded why method binding happened on compile time.
  • Static constructor can not be defined. because Constructor is used to instantiate Object. but static saying method is belong to class only. If object wants static method or variable, refer from Class.
  • this(), super() can not use static or Static variable can not be reference in this(), super().
  • Static method and variable can be defined in abstract and interface even both are not be initialize because Static is not associate with Objects.
  • Static method called by class name not by object. But no challenge if call by object.staticMethod()
    ClassName.staticMethod()

→Static Method of class can be called on null object of same class: YES
A a= null;
a.getMethod() will work perfectly. Where
getMethod is Static Method.
One class can have multiple static blocks in our code but looks weird.
→ abstract , Interface can have all static variable, method and block.

============= Java Threading Magic =============

Inheritance

--

--

Dr. Rajeev Rathor

PhD [IOT+AI], M.Tech [CSE] , B.Tech, 14 Years exp s from Product Development in Java, Backend, Data Engineering, IOT plaeform