Total members 11890 |It is currently Thu Apr 18, 2024 5:02 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : using of String constructor
 Using a Constructor in jsp     -  
 Copy Constructor     -  
 Class with a Constructor in php     -  
 What Are Constructor Methods?     -  
 cannot find symbol constructor     -  
 class with constructor parameter     -  
 recursive string reversal- reverse string     -  
 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     -  



Topic Tags

Java Strings
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com