Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

using of String constructor

Tue Nov 13, 2007 10:24 pm

You to take when you use the String class constructor.Like the following
Code:
  String str1=new String("Hello");//Using the constructor

Constructor creates a new string means it does not intern the String. Interned String object reference can be obtained by using intern() method of the String class

this is different to the following
Code:
String str2="Hello";


If you want to compare the str1,str2 you can use str1.equals(str2) or use intern() function .
Code:
   
        String str1
="Hello";
        String str2="Hello";
        String str3=new String("Hello");
        String str4=str3.intern();
        if(str1==str2)
            System.out.println("Equal1");
        if(str1==str4)
            System.out.println("Equal2");
        if(str3.equals(str1))
            System.out.println("Equal3");
        
        if
(str3==str2)
        {
            System.out.println("Equal4");
        }else {
            System.out.println("Not Equal4");
        }
 


The output of this code is
Code:
Equal1
Equal2
Equal3
Not Equal4




Post a reply
  Related Posts  to : using of String constructor
 Using a Constructor in jsp     -  
 What Are Constructor Methods?     -  
 Copy Constructor     -  
 Class with a Constructor in php     -  
 class with constructor parameter     -  
 cannot find symbol constructor     -  
 check if string ends with specific sub-string in php     -  
 Splitting a String Based on a Found String     -  
 check if string start with a specific sub-string in PHP     -  
 recursive string reversal- reverse string     -  

Topic Tags

Java Strings