• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SCJP Brainteaser (10)

 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another one..

But it's relatevly easy from other ones.



What will be the output and please note that give the exact output , what will be on the screen, if we run this program on Screen.

It would be great if you don't use and JAVA IDE or JVm to check the output of program.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sharma Ji,

It would print false.

Reason is when we concatenate Strings using a return from a method, it creates a new String in the memory. But saying,

final String dog = "length: " + "10" would return true when checked for pig == dog.
[ November 10, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Continuing to the same teaser.

Just give the output if the question would be like.



Give the exact output what we will be see on the Black Screen.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This returns true.
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
This returns true.



Need to try a bit some more....
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried compiling and it showed me the result as below,

First the program,



Now the result,
compile-single:
run-single:
Animals are equal: true
BUILD SUCCESSFUL (total time: 1 second)

Still you feel it to return false???

Both the objects are of type String and the content in the object is same (length: 10), which is why it returns true.
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you cheated the rules.

The rule was we cann't use PC. It was just brainteaser.


Anyways,

If you run both programs you will see the difference
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
false: "aninaml..."!="length..."
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by java benice:
false: "aninaml..."!="length..."



Howdy Mr Java benice...

Welcome to JAVA Ranch.

You will enjoy here while learning so many concepts.

But unfortunately we have a naming policy. You can change it from.here.

Hope you may find it suitable for you.

PS: Moderators take a look on this issue
[ November 10, 2006: Message edited by: Sharma Ji ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sharma Ji,

I didnt cheat the rules. You asked what it would print, I told true and you told that I have to try it again. so I compiled it and showed the O/P.

Even I like to compile it mentally first before opening my IDE.
[ November 10, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Sharma Ji,

I didnt cheat the rules. You asked what it would print, I told true and you told that I have to try it again. so I compiled it and showed the O/P.



So have you tried both programs, and what are different Outputs for the different Programs
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sharma Ji,



Above one prints false

Reason for the above one to be false...the two objects have different references and they are two different objects. Also the line System.out.println("Animals are equal: " + pig == dog); is seen by the compiler as System.out.println(("Animals are equal: " + pig) == dog); which will anyways return false. Now to prove my first statement "the two objects have different references and they are two different objects" try System.out.println("Animals are equal: " + (pig == dog)); which would still return false. Hope it is clear....

The below one prints true



I think I dont have to give reason here as it is blatant that the code above returns true.
You will disagree??
[ November 10, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Offcourse I will still.

Because still there is a important concept left.

are you (Confused).

Then I would tell you once again, try it again.
[ November 10, 2006: Message edited by: Sharma Ji ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sharma Ji,




Above one prints false

Reason for the above one to be false...the two objects have different references and they are two different objects. Also the line System.out.println("Animals are equal: " + pig == dog); is seen by the compiler as System.out.println(("Animals are equal: " + pig) == dog); which will anyways return false. Now to prove my first statement "the two objects have different references and they are two different objects" try System.out.println("Animals are equal: " + (pig == dog)); which would still return false. Hope it is clear....

Operator precedence comes to play with the System.out.println("Animals are equal: " + pig == dog); option.

Satisfied sharma Ji....or still you wanna argue???
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sharma Ji.

Hi have you recently applied for Bartender/Sheriff or TrailBoss at JavaRanch.
And they (Moderators) have given you assignments to try out some of their activities.

Or you are just Enjoying your friday.


[ November 10, 2006: Message edited by: Harshad Khasnis ]
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Jothi

Reason is when we concatenate Strings using a return from a method, it creates a new String in the memory.


Does not soulds universal.Even if you will concatenate two strings it will create a new object.


(3) and (4) will also print flase when checked with reference eqality check.
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harshad Khasnis:
Dear Sharma Ji.

Hi have you recently applied for Bartender/Sheriff or TrailBoss at JavaRanch.
And they (Moderators) have given you assignments to try out some of their activities.

Or you are just Enjoying your friday.



Dear Harshad,

Well If I say yes. You are right.
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harshad Khasnis:
Dear Sharma Ji.

Hi have you recently applied for Bartender/Sheriff or TrailBoss at JavaRanch.
And they (Moderators) have given you assignments to try out some of their activities.

Or you are just Enjoying your friday.



Dear Harshad,

Well If I say yes. You are right.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sanjeev,

What happened...Did you try to sopt the flaw in your code???

You said,

(3)final String dog = "length: " + 10;
(4)final String dog = "length: " + "10"; will return false...

First it won't compile as you are trying to declare dog twice...

Changing your code to the one like below,

(3)final String dog1 = "length: " + 10;
(4)final String dog2 = "length: " + "10";

and the statement....

System.out.println("Animals are equal: " + dog1 == dog2); //returs false

but wheras the statement...

System.out.println("Animals are equal: " + (dog1 == dog2)); //returns true

The reason why the above statement returns true is that both dog1 and dog2 references refer to the same single object "length: 10" in the memory.

Got it???or Still confused???
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Catch Jothi Shankar Kumar Sankararaj !!!

 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was a good one Sharma Ji. Worth the discussion. Thanks for bringing this question.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
I got the point how we gets the false when we concatenate the strings with a return string from a method and how to take care for printing the stmts in SOPs..

Thanks a lot.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'm a little confused. What is the difference between

and

Two of them print different values. Please help.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The both references will point two different objects in the string constant pool.The reason being for that is whenever we concatenate the string with a method returned string(dog1),it creates a new object rather than pointing to a same named string constant(dog2) in the pool.

so when we apply reference check equality on both means (dog1==dog2),it returns false.Now change the pig.length() to "10",it must print true.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,
Yes you are right. It does print different values and here's is the reason why.
First some stuff to remember: Java uses something called as the string pool to manage string objects ( you dont need to know the details abt the string pool for the SCJP. but you should know that there is something calledas the string pool). Whatever string objects are created are stoed in the string pool so that the string can be reused
For ex suppose you say String str="Hello".
The first time the program runs the str object is placed in the string pool
Now next time say you create another object called str1 as follows:
String str1="Hello";
What happens at this point is that the Java compiler knows that the string object with the value "Hello" is already present in the string pool. So instead of creating a new String object what the compiler does to optimize memory usage is to make str1 also point to the same string object as str.

Hence the equality test for the string objects str and str1 i.e (str==str1) returns true as the two references are the same.

Now when a method returns a String object it creates a new String object hence if we were to declare another string object say str2 as follows
String str2=getString();
where let's assume that getString returns "Hello"; here when the method returns a string a new string object is created in the string pool and the reference variale str2 points to that
Hence the test str==str2 fails as the two objects refer to different objects on the pool albeit the objects they are referring to have the same value "Hello".
Hope this explaination solves your confusion

Regards
Prahalad
 
John Johnson
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thank you for explaining that to me. Now I understand that. I have the last question to ask though. What is the diference between this code:

and

This too, print different values. I'm lost!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,
The difference in the following two statement is nothing but the operator precedence.
1) System.out.println("Animals are equal: " + dog == pig);
2) System.out.println("Animals are equal: " + (dog == pig));

On the 1) Statement + operator take the precedence and comparison will be between ("Animals are equal: " + dog) and (pig) whereas on the statement 2) the comparison is between (dog) and (pig). Hope this will clarify your doubt.
 
John Johnson
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oooooh, I see. Wow, I knew it's the precedence, but didn't quite get it. Thank you Yogesh!
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic