• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

String prob. ...help

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
c the following code: ..............................//1
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");

o/p : Not Equal

Now,c this code: ...............................//2
if( "String".endsWith(""))
Sytem.out.println("True");
else
System.out.println("False");
o/p : " True "
and , now this one: .............................//3

if( "String".startsWith(""))
Sytem.out.println("True");
else
System.out.println("False");
o/p The code produces : "True"
Now...my question is, if code 2 and 3 both produces "True" then how the output of the code 1 is "not Equal" !!
One can say ,it is because the code 1 is using String method and when a String method is used one new String is generated and return.
If u say this then explain me the following code :

if("String".concat("") == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
o/p : Equal

Hmmmm..I think my brain is not working properly...CAN SOME1 HELP ME ,,,

HELP !! HELP !! HELP !!

Regds.
<marquee> Ratul Banerjee </marquee>
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code 1:
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
o/p : Not Equal
If u say this then explain me the following code :
code 2:
if("String".concat("") == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
o/p : Equal
as with concat() it will not return new string if spaces are passed as parameter.
if you say
if("String".concat("pk") == "String")
then it will return a new string "Stringpk" which is now having other reference,so there r not equal
pawan
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pawan Kumar:
code 1:
if(" String ".trim() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
o/p : Not Equal
If u say this then explain me the following code :
code 2:
if("String".concat("") == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
o/p : Equal
as with concat() it will not return new string if spaces are passed as parameter.
if you say
if("String".concat("pk") == "String")
then it will return a new string "Stringpk" which is now having other reference,so there r not equal
pawan


This can be better explained w.r.t. to concat() as :
If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created
See the below code
Code //3
if("String".concat(" ") == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
o/p Not Equal
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ratul,
If the method completes without actually doing anything the original reference is returned.
In your first example <code>" String ".trim()</code>, there were whitespaces to be removed; the <code>trim()</code> method had to create a new String in order to return <code>"String"</code>, as promised by it's contract.
In your last example, <code>"String".concat("")</code>, there was nothing for <code>concat()</code> to do; the original String did not have to be altered so it's reference was returned.
For more information, read the String class documentation in the API
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all.
It is little bit clear now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic