Forums Register Login

SCJP Brainteaser (10)

1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
This returns true.
+Pie Number of slices to send: Send
 

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



Need to try a bit some more....
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
false: "aninaml..."!="length..."
+Pie Number of slices to send: Send
 

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 ]
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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???
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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???
+Pie Number of slices to send: Send
Good Catch Jothi Shankar Kumar Sankararaj !!!

+Pie Number of slices to send: Send
It was a good one Sharma Ji. Worth the discussion. Thanks for bringing this question.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Ok, I'm a little confused. What is the difference between

and

Two of them print different values. Please help.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Oooooh, I see. Wow, I knew it's the precedence, but didn't quite get it. Thank you Yogesh!
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1487 times.
Similar Threads
final And static
Polymorphism question
tricky question about HashMap + equals
setEditable(true) function of jcombobox not working
collections- MAP USE
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:33:19.