This is one of the common question to be asked in interview to judge your basic level of knowledge. We all know that String is immutable which means that once object created cannot be modified or if try to modify, it will create another String literal in the String pool. So this proves that String is immutable, but this is we are proving theoretically. Now if interviewer ask you to prove the same by writing programs then you will be doing like this,

class A{

  public static void main(String[] args){

     String s="java";  // Create a object in String pool.

     s = s + "java" ; // Create another object with "javajava" as 
                         String literal in the String pool

  }

Related Posts:

  • Search a Word in String Array ? Requirement is to search a particular String in the Array of String value. To achieve this we need to have some set string values in a String array and secondly we need to have a String value to be searched. Tips to Pr… Read More
  • Calculate the length of the String without using lenght() ? This question might be asked in as interview to judge logic on how you are going to achieve without providing you the length() method to calculate the String length easily. Tips to Think .... 1. As we know that in J… Read More
  • Find the sum of all the numbers present in a String? This is a interview question can be asked in an experienced or fresher's interview to write a program which will add up all the integer value present in a string and print the added number. Tips to think how to do it...… Read More
  • Singleton class in Java This is one of the important and mostly asked question in fresher and experienced interview. Singleton class means a class with only one object. It will have only one object which will be returned always when ever we try… Read More
  • How to perform some task before main() method execution? The above question means that we need to execute some line of code which perform something before the main() method execution. As we know that when we execute our program JVM will call main() at very first , so it seems th… Read More

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic