Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Puzzling String

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String one= "Hello";
String two= "Hell"+"o";
bolean answer = one==two;
Now u will think the answer should be false but no! if u check it out it will TRUE? whats the reason behind this
I Knon the answer! Do u?
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
There goes my guess .. During compilation since that string one and two are initiated with same literal ( after concatenation for two), the string pool is used and both point to same literal. Hence the result ( though the value of the object two can be changed down the line and the similar assignment would result in false). Someone pls correct if Im wrong...
Thanks
MKBala...
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Balamurugan
when we use operator '+' append() method of StringBuffer is invoked on a StringBuffer Object.
After the concatenation has been done compiler calls toString() to convert StringBuffer Object back into a String Object.
but in this kind of stuations resuting string belongs to a heap not to a pool.
I changed Sugat example
String one= "Hello";
String two= one+"o";
bolean answer = one==two;
Now answer gives false and whatever I said about concatenation above applies here .
but in the original example String two= "Hell"+"o"; it seems append method of Stringbuffer is not called also no String
object is created into the pool.Anyway it needs some explaination and i'm waiting for that....
Thanks

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Balamurugan
yes its correct..as compiler knows the value of string at compile time, so it picks up from String pool
prasad
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prasad
you mean during the compile time the value of
String two= "Hell"+"o";
is evaluated which is already in the pool so
append method is not called.
Hmm sounds reasonable..
 
Sugat Thulkar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends !
you are going near
Here is one more CLUE
In switch construct we can use only CONSTANTS in case statements
But any variable is allowed there? NO!
But you can use expressions involving only constants
Like 3+2 etc
TRY HARD!!
luv SUGAT
 
Sugat Thulkar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends !
you are going near
Here is one more CLUE
In switch construct we can use only CONSTANTS in case statements
But any variable is allowed there? NO!
But you can use expressions involving only constants
Like 3+2 etc
TRY HARD!!
luv SUGAT
 
pie. 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