Monday, October 10, 2016

String Interview Questions and Answers In Java

 In this article we are going to discuss about String Interview Questions in java.These questions frequently asked from the String topic in java specially for beginners.You can visit String Tutorial to get full details regarding it.At the same  time Interviewer could ask some programmatic Interview questions on String for that you can check this link Top 10 java programs on strings. So, you should get confident after reading all these three articles on the String topic. Let's we start few String Interview Questions

1.  What is String in java? Is String is  a data type?

Ans: String is a class in java and defined in java.lang.package. String class represents character Strings. String is used almost in almost all the java applications and there are some interesting facts we should know about it.

  • String is immutable and final in java
  • JVM uses String pool to store all the String Objects
  • we can initiate String Object using double quotes

2.What are the different ways to create String Object?

Ans: We can create String Object using new operator just like any normal java class or create a String Object.

Ex:  String st=new String("abc");
         String st1="abc";

     When we create  a String using double quotes, JVM looks in the String Pool to find if any other String is stored with same value. If found,it just returns the reference to that String Object else it creates a new String object with given value and stores it in the String pool.

               When we use new operator, JVM creates the String object but don not store it into the String Pool. We can use use intern() method to store the String object into the String pool or return the reference if there is already a String with equal value present in the pool.




3. Why String is immutable in Java?

Ans: String is one of the most used classes in any programming language. As we know that String is immutable and final in java and java runtime maintains a String pool that makes it a special class. Immutable in the sense of memory. It creates string or assign a new String/change the value.
               If several references point to same String without even knowing it, it would be bad if one of the references modified that string value. That's why string objects are immutable. And what if someone  overrides the functionality of string class? That's the reason that the string class is marked final so that nobody can override the behavior of its methods.

4. What are the benefits of String immutability?

Ans: At a high level, benefits would be security, Thread Safety and memory efficiency.

  •  String pool is possible  only because String is immutable in java, this way java Runtime saves lot of java heap space because different String  variables in the pool. If String would not have been immutable , then String interning would not have been possible because if any variable would have changed that value, it would have been reflected to other variable also.
5. What is String  Pool in Java?

Ans:   String pool is a pool of Strings stored in java Heap Memory. We know that String is special class in java and we can create String Object using new operator as well as providing value in double quotes. String pool is possible  only because String is immutable in java and it's implementation of String interning concept.

                String pool is also example of Flyweight  design pattern. String pool helps in saving a lot of space for java Runtime although it takes more time to create the String. 

                When we use double quotes to create a String, it first looks for String with same value in the String Pool , if found it just returns the reference else it creates a new String in the pool and then returns the reference. However, using new operator, we force string class to create new String  object and then we can use intern()method to put it into the pool or refer to other string object from pool having same value. 

6. Why char array is preferred over String for storing password?

Ans:  String is immutable in java and stored in String pool. Once it is created it stays in the pool until unless garbage collected, so even though we done with password it is available in the memory for longer duration and there is no way to avoid it. It is a security risk because anyone having access to memory dump can find the password as clear text.
  
               If we use char array to store password,we can set it to blank once we are done with it. So we can control for how long it is available in memory that avoids the security threat with String.

7. How do you check if two Strings equal in java or not?

Ans :  There are two ways to check if two Strings are equal or not

using "==" operator or using equals() method. when we use "==" operator , it checks for value of string as well as reference  but in our programming,most of the time we are checking equality of string for value only. So we should use equals() method to check if two string are equal or not. 

Ex:   
String s1="abc"
String s2="abc";
String s3=new String("abc");
System.out.println("s1==s1?"+(s1==s2));//true
System.out.println("s1==s3?"+(s1==s3));//false
System.out.println("s1 equals s3?"+(s1.equals(s3));//true

8. Does String is thread-safe in java?
Ans:  String are immutable, so we can't change it's value in program. Hence it's thread-safe and can be safely used in multi-threaded environment.

9. Why String is popular HashMap key in java?

Ans:  Since String is immutable, it's hash code is cached at the time of creation and it doesn't need to be calculated again. This makes it a great candidate for key in a Map and it's processing is fast than other  HashMap Key objects. This is why String is mostly used Object as HashMap keys.

10) What is difference Between StringBuffer and String Builder?

Ans: 

  1. StringBuffer is synchronized and StringBuilder is not
  2. StringBuffer is thread-safe and StringBuilder is not
  3. StringBuilder is faster than StringBuffer because of no overhead of acquiring and releasing locks associated with synchronized methods.





No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...