0% found this document useful (0 votes)
44 views

Q25. What Is Java String Pool?: Static Method Non-Static Method

Static methods can be called using the class name without creating an instance of the class. They cannot access non-static instance variables or methods. Non-static methods require an instance of the class and can access static and non-static methods and variables. Constructor chaining allows constructors to call another constructor in the same class using this() or in the superclass using super(). This ensures constructors are called in the proper order. Strings are immutable in Java to enhance security, caching, synchronization, and performance as String objects are commonly shared between clients. Modifying a shared String could affect other clients.

Uploaded by

sachin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Q25. What Is Java String Pool?: Static Method Non-Static Method

Static methods can be called using the class name without creating an instance of the class. They cannot access non-static instance variables or methods. Non-static methods require an instance of the class and can access static and non-static methods and variables. Constructor chaining allows constructors to call another constructor in the same class using this() or in the superclass using super(). This ensures constructors are called in the proper order. Strings are immutable in Java to enhance security, caching, synchronization, and performance as String objects are commonly shared between clients. Modifying a shared String could affect other clients.

Uploaded by

sachin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q25. What is Java String Pool?

Java String pool refers to a collection of Strings which are stored in heap memory. In
this, whenever a new object is created, String pool first checks whether the object is
already present in the pool or not. If it is present, then the same reference is returned
to the variable else new object will be created in the String pool and the respective
reference will be returned.

Q26. Differentiate between static and non-static methods in Java.

Static Method Non-Static Method


1. The static keyword must be used before the 1. No need to use the static keyword before
method name the method name
2. It is called using the class 2. It is can be called like any general
(className.methodName)  method
3. It can access any static method and any
3. They can’t access any non-static instance
static variable without creating an instance
variables or methods
of the class
Q27. What is constructor chaining in Java?

In Java, constructor chaining is the process of calling one constructor from another
with respect to the current object. Constructor chaining is possible only through
legacy where a subclass constructor is responsible for invoking the superclass’
constructor first. There could be any number of classes in the constructor chain.
Constructor chaining can be achieved in two ways:

1. Within the same class using this()


2. From base class using super()

Q28. Difference between String, StringBuilder, and StringBuffer.

Factor String StringBuilder StringBuffer


Constant String
Storage Area Heap Area Heap Area
Pool
Mutability Immutable Mutable Mutable
Thread Safety Yes No Yes
Performance Fast More efficient Less efficient
Q29. What is a classloader in Java?

The Java ClassLoader is a subset of JVM (Java Virtual Machine) that is responsible


for loading the class files. Whenever a Java program is executed it is first loaded by
the classloader. Java provides three built-in classloaders:

1. Bootstrap ClassLoader
2. Extension ClassLoader
3. System/Application ClassLoader

Q30. Why Java Strings are immutable in nature?

In Java, string objects are immutable in nature which simply means once the String
object is created its state cannot be modified. Whenever you try to update the value
of that object instead of updating the values of that particular object, Java creates a
new string object. Java String objects are immutable as String objects are generally
cached in the String pool. Since String literals are usually shared between multiple
clients, action from one client might affect the rest. It enhances security, caching,
synchronization, and performance of the application. 

You might also like