Java String Interview Questions

Java String Interview Question 1

Is String a primitive type in java?

In java, String is an object.

It is not a primitive data type.

What are the ways of creating String in java?

In java, we can create String in two ways:

  1. String literals
  2. using new keyword

Both ways of creating String in java is clearly explained with example here, click to read

Why String is immutable in java?

String is immutable in nature. It means that once String object is created then its state will never change. Object created is permanent.


There is an important reason behind making String immutable in java. 

The reason is to improve the security of the java application. String objects are generally used as key in Maps, as database connections, as passwords, etc. . If String were mutable then these keys, connections, etc. could have been easily modified thus could result in breach of security. And it would result in severe impact on our java application.


Thus String were made immutable in nature.


To read more about immutable nature of String, click here

What are the interfaces implemented by String class?

String class implements:

  • Comparable interface
  • Serializable interface
  • CharSequence interface

Why String class is final in java?

When a class is declared final then it cannot be sub-classed or inherited.

In order to prevent java.lang.String class from being inherited and its properties modified by is sub-classes it is declared final.

Marking String final also helps making String immutable in java.



Please Share this page
Views : 114
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!