Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

Top 25 Java String Interview questions

Author: Infocampus Bangalore
by Infocampus Bangalore
Posted: Nov 30, 2018

The String class and conception may be a vital class in Java. There's not one Java program out there that isn't using String objects, and that is why it's essential from interview purpose of considering well.

25 Java String concept Interview Questions and Answers

1. Why is String final in Java?

There are some reasons for this, e.g. String pool, Caching, Performance; however, Security is maybe the first necessary reason. Given String is used in sensitive places like specifying the host and port details, lockup it for modification stop from might Security connected risks. It's one among the popular Java interview questions. Therefore, you higher read additional regarding it in the given answer.

2. A way to convert String to date in Java?

Before Java eight, you'll use Date Format or easy Date Format class to convert a String thus far In Java or vice-versa. From Java eight ahead, once you use the new Date and Time API, you'll additionally use the Date Time Formatter class to convert String to native Date, Local Time, or Local Date Time category in Java.

3. Is String thread-safe in Java? Why?

Yes, String is thread-safe as a result of its immutable. All changeless objects are thread-safe as a result of once they're created, they cannot be changed, therefore no issue concerning multiple threads accessing them.

4. Can we use String as HashMap key in Java?

Yes, we will use String as a key in HashMap as a result of it implements equals () and hashcode () technique that may be a needed for an object to be used as a key in HashMap.

5. A way to check if a String is empty in Java?

There are some ways to see if a String is empty in Java, e.g. you'll check its length. If the length of the String is zero, then it's empty. Otherwise, you'll conjointly use is Empty() technique that returns true if String is empty. Though you wish to take care with needs, e.g. a String might contain whitespace which can look empty however length won't be zero. Therefore it depends upon your needs.

6. However will substring technique add Java?

This comes back a substring of such vary from the first String on that it's referred to as. It returns a replacement String object as a result of String is changeless and cannot be modified, but, before Java seven it also holds the reference of the first array which may stop it from Garbage collected and caused a memory leak, that isn't sanctioned.

7. What's String pool in Java? What's the distinction in String pool between Java 6 and 7?

It's a pool of cached String objects for minimizing the number of String instances and up performance by sharing a similar example with multiple purchasers and reducing pickup. Before Java seven, the Sring pool was set on meta-space wherever class data held on however from JDK seven ahead it's moved into heap house.

8. What's the distinction between "ABC".equals(str) and str? Equals ("ABC"), wherever str may be a String object?

Though each looks similar and come back an equivalent result if str is up to "ABC" however the important distinction comes once given String is null, i.e. str = null. Therein case, 1st code snip can come back false, whereas second code snippet can throw NullPointerException. {this is|this is often|this will be} one among the difficult Java queries, and if a candidate can answer this, then he has smart data of Java fundamentals.

9. The difference between StringBuffer and StringBuilder in Java?

Now, this is often attention-grabbing as a result of each StringBuffer and StringBuilder represents changeable String. This is often additionally asked a follow-up question of the gesture. Anyway, the important distinction is that ways of StringBuffer, e.g. append() ar synchronic, hence slow, whereas those of StringBuilder isn't synchronic, thus fast. Otherwise, StringBuilder merely is a replica of StringBuffer, and you'll see that within the Javadoc of the StringBuilder class similarly.

10. The way to format String in Java?

You can use the format () technique of java.lang.String category to format a given String in Java. If you wish to print the formatted String, you'll additionally use the System.out.print() technique that prints the formatted string to console.

11. A way to convert Enum to String in Java?

Similar to any Java object, you'll also use the toString() technique to convert associate Enum to String in Java. The Enum category provides a toString() method which may be overridden by your Enum implementations.

12. A way to convert String to int in Java?

There are some ways to convert the String to the int primitive in Java, however, the most effective way is by using the number.parseInt() technique. This technique parses the given String and returns a first int worth. If you wish a wrapper category number object, you'll use the number.valueOf() technique. Though it internally uses the parseInt() technique it caches frequently used number values, e.g., 128 to 127, which may scale back temporary objects.

13. What's the distinction between String in C and Java?

Even though each C and Java String is backed by character array, C String may be a NULL terminated character array whereas Java String is an object. Which suggests you'll decision ways on Java String, e.g. length, toUpperCase, toLowerCase, substring etc.

14. A way to convert String to double in Java?

Similar to the number.parseInt() technique that is employed to convert String to double, you'll use the Double.parseDouble() method to convert a String to increase primitive worth.

15. A way to convert String to long in Java?

It's almost like changing String to int or String to double in Java. All you wish to try and do is use the parseLong() technique of the Long class. This technique returns primitive long worth. even if you'll use the Long.valueOf() procedure which may be wont to convert String to Long however it returns a protracted object, therefore you'd got to use auto-boxing to turn wrapper object to primitive worth.

16. Difference between str1 == str2 and str1.equals(str2)?

The critical distinction is that 1st one is using == operator that will reference based mostly comparison whereas the second is using equals () technique that will provide a content-based correlation. Since String category overrides equals() to perform character-based comparison 1st can come back right on condition that each str1 and str2 points to a similar object, however, the second can go back correct if str1 and str2 have same content although they're a unique object. If you're interested, you'll scan additional regarding == vs equal in my post distinction between equals and == in Java.

17. Once you execute String str = new String ("abcd")? What are percentage String objects created?

This is another tough Java interview question as several Java developer can answer only one however that is not true. There are 2 String objects are created here, the primary String object is formed by String literal "abcd" and also the second is formed by new String(). If you're unsure about however

18. How does one split comma separated String in Java?

You can use either the StringTokenizer or the split () technique of java.lang.String category to separate a comma separated String. The division () procedure is best as a result of it additionally expects an everyday expression associated returns an array of String that you'll use or pass to a operate. See the solution link for a sample Java program to separate String in Java.

19. The difference between String and StringBuffer in Java?

The critical distinction between String and StringBuffer is that String is changeless whereas StringBuffer is changeable, which suggests you'll modification the content of a StringBuffer while not making separate String objects. If you're making string programmatically, think about Advanced Java Training in Bangalore using StringBuffer for higher performance because it places less pressure on the refuse collector by reducing the number of temporary objects.

20. Distinction between format () and printf () technique in Java?

Even though each way is often wont to format String and that they have same rules the critical distinction is that format () technique returns a formatted String whereas printf () technique print formatted String to console. So, if you wish a formatted String, use format technique and if you want to print, then use the printf () method.

21. How does one append leading zero to a numeric String?

You can use the format () technique of String to append leading zeros to a numeric String in Java. See the link for associate example.

22. A way to check if 2 Strings are Anagram in Java?

There are multiple ways that to resolve this drawback. Away is to kind each the string to compare. This fashion all characters can acquire the same place, and if Strings are the anagram, then they'll be up to one another.

23. Difference between format () and printf () technique in Java?

Even though each way may be wont to format String and that they have same rules the critical distinction is that format () technique returns a formatted String whereas printf () technique print formatted String to console. So, if you wish a formatted String, use format technique and if you want to print, then use the printf () method.

24. However, does one append leading zero to a numeric String?

You can use the format () technique of String to append leading zeros to a numeric String in Java. See the link for associate example.

25. A way to take away white house from String in Java?

You can use trim () technique to get rid of the white area from String in Java. It's almost like SQL Servers LTRIM () and RTRIM () ways.

Conclusion

You can use this list to refresh your data concerning String in Java moreover on harden your next Java interview. If you've got any smart String questions that don't seem to be on this list, be happy to drop a note, and I am going to embrace during this list.

Author:

Infocampus gives Java Training in Bangalore dependent on current industry principles that assistance Students to anchor situations in their fantasy occupations at MNCs.

Infocampus is a standout amongst the most trustworthy Java Courses in Bangalore offering hands-on experience with 100% Job Assistance Best Core Java Training in Bangalore and also Advanced Java Training in Bangalore.

Java Training in Bangalore has been planned according to the most recent industry patterns and remembering the propelled Java course substance and prospectus dependent on the expert prerequisite of the Student.

Visit: http://infocampus.co.in/java-training-bangalore.html

About the Author

Searching for center propelled java/j2EE training in Bangalore Marathahalli, a short time later your first plausibility to be an Infocampus.

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Infocampus Bangalore

Infocampus Bangalore

Member since: Dec 28, 2015
Published articles: 81

Related Articles