Skip to main content

Java String Interview Questions


String




1.What is the meaning of Immutable in the context of String class in Java?

An Immutable object cannot be modified or changed in Java. String is an Immutable class in Java. Once a String object is created, it cannot be changed. When we assign the String to a new value, a new object is created.

2.Why a String object is considered immutable in java?

Java language uses String for a variety of purposes. For this it has marked String Immutable. There is a concept of String literal in Java. Let say there are 2 String variables A and B that reference to a String object “TestData”. All these variables refer to same String literal. If one reference variable A changes the value of the String literal from “TestData” to “RealData”, then it will affect the other variable as well. Due to which String is considered Immutable. In this case, if one variable A changes the value to “RealData”, then a new String literal with “RealData” is created and A will point to new String literal. While B will keep pointing to “TestData”

3.How many objects does following code create?

Code:
String s1="HelloWorld";
String s2=" HelloWorld ";
String s3=" HelloWorld ";
The above code creates only one object. Since there is only one String Literal “HelloWorld” created, all the references point to same object.

4. How many ways are there in Java to create a String object?

Java provides two ways to create a String object. One is by using String Literal, the other is by using new operator.

5.How many objects does following code create?

Code:
String s = new String("HelloWorld");
The above code creates two objects. One object is created in String constant pool and the other is created on the heap in non-pool area.

6.What is String interning?

String interning refers to the concept of using only one copy of a distinct String value that is Immutable. It provides the advantage of making String processing efficient in Time as well as Space complexity. But it introduces extra time in creation of String.

7.Why Java uses String literal concept?

Java uses String literal concept to make Java more efficient in memory. If same String already exists in String constant pool, it can be reused. This saves memory usage.

8. What is the basic difference between a String and StringBuffer object?

String is an immutable object. Its value cannot change after creation. StringBuffer is a mutable object. We can keep appending or modifying the contents of a StringBuffer in Java.

9.How will you create an immutable class in Java?

In Java, we can declare a class final to make it immutable. There are following detailed steps to make it Immutable:
1.     Add final modifier to class to prevent it from getting extended
2.     Add private modifier to all the fields to prevent direct access
3.     Do not provide any setter methods for member variables
4.     Add final modifier to all the mutable fields to assign value only once
5.     Use Deep Copy to initialize all the fields by a constructor
6.     In clone method, return a copy of object instead of the actual object reference

10.What is the use of toString() method in java ?

In Java, Object class has toString() method. This method can be used to return the String representation of an Object. When we print an object, Java implicitly calls toString() method. Java provides a default implementation for toString() method. But we can override this method to return the format that we want to print.

11. Arrange the three classes String, StringBuffer and StringBuilder in the order of efficiency for String processing operations?

StringBuilder is the most efficient class. It does not have the overhead of Synchronization. StringBuffer is a Synchronized class. It has better performance than String but it is slower than StringBuilder. String is the slowest for any String processing operations, since it is leads to creation of new String literal with each modification. So the decreasing order of efficiency is: StringBuilder, StringBuffer, String







Comments

Popular posts from this blog

DOCKER Interview questions

DOCKER 1. What is Docker? Docker is Open Source software. It provides the automation of Linux application deployment in a software container. We can do operating system level virtualization on Linux with Docker. Docker can package software in a complete file system that contains software code, runtime environment, system tools, & libraries that are required to install and run the software on a server. 2. What is the difference between Docker image and Docker container? Docker container is simply an instance of Docker image. A Docker image is an immutable file, which is a snapshot of container. We create an image with build command. When we use run command, an Image will produce a container. In programming language, an Image is a Class and a Container is an instance of the class. 3. How will you remove an image from Docker? We can use docker rmi command to delete an image from our local system. Exact command is: % docker rmi <Image Id> If we want to fin...

Microservices Interview Questions

Microservices Interview Questions 1. What is a Microservice in Java? A Microservice is a small and autonomous piece of code that does one thing very well. It is focused on doing well one specific task in a big system. It is also an autonomous entity that can be designed, developed and deployed independently. Generally, it is implemented as a REST service on HTTP protocol, with technology-agnostic APIs. Ideally, it does not share database with any other service. 2. What are the benefits of Microservices architecture? Microservices provide many benefits. Some of the key benefits are: 1.      Scaling : Since there are multiple Microservices instead of one monolith, it is easier to scale up the service that is being used more. Eg. Let say, you have a Product Lookup service and Product Buy service. The frequency of Product Lookup is much higher than Product Buy service. In this case, you can just scale up the Product Lookup service to run on powerful ha...

Method Overloading and Overriding Interview Questions

Method Overloading and Overriding 1. What is the another name of Method Overloading? Method Overloading is also known as Static Polymorphism. 2.In java, How will we implement method overloading? We known, a class can have multiple method with same name but different arguments. It is called Method Overloading. To implement method overloading we have to create two methods with same name in a class and do one/more of the following: 1.      Different number of parameters 2.      Different data type of parameters 3.      Different sequence of data type of parameters 3. What kinds of argument variation are allowed in Method Overloading? Method Overloading only allows two methods with same name to differ in: 1.      Number of parameters 2.      Data type of parameters 3.      Sequence of data type of parameters 4. It is not p...