• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

what is this??

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friendz,
can anybody explain these mentioned line of code. where i m wrong...
1. String a="one";
String b=a.concat(a); //result is oneone
if (a=="one") //true ok i m satisfied coz same string pool
if (b=="oneone") //false what is that even both b and "oneone" same string pool

2.String c=" one ";
if (c.trim()=="one") //false ??? anybody explain
if (c.trim()==c.trim()) //still false what is that ???
plz give me detail both above questions.
thanx
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the first case the String b is created on runtime. and the Strings which are created on runtime are not placed in the pool, i-e a separate instance is created therefore == returns false.
Same thing is happening in the second case both of c.trim() are separate instances as they are being created on runtime therefore == returns false
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
every String methods returns a NEW String object when it's
necessary to change the contents
String c=" one ";
if (c.trim()=="one")//false bcos c.trim() return a new String but,
String c="one";
if (c.trim()=="one")//true bcos no modification happens to string literal "one" referenced c
hope this help
cristi
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The very important thing is that you have to understand the difference between "==" and object1.equal(object2),in u case object is the String class.
"==" compares two reference variables and string1.equal(string2) compares content of two string objects
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic