This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

about K&B book (SCJP 6)

 
Ranch Hand
Posts: 74
Netbeans IDE Tomcat Server Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in chapter 3,page 280 it says answer A ("true true") is correct for question 5

and my answer was D,which is "false false"


I tried to run this program on my machine,and it gave me false false as well


can somebody verify my answer ?


thanks in advance
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the question...

Welcome to JavaRanch!
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the code and question, so it makes easy for the reader to reply back, because many of the
people are not having books at their hands at the same instant.

Best Regards
 
Mahmoud Hossam
Ranch Hand
Posts: 74
Netbeans IDE Tomcat Server Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is as follows :

Given:



what is the result?

A. true true
B. false true
C. true false
D. false false
E. Compilation fails
F. An exception is thrown at runtime


Answer

✓ A is correct. The references f1, z, and f3 all refer to the same instance of Fizz. The final
modifier assures that a reference variable cannot be referred to a different object, but final
doesn’t keep the object’s state from changing.

B, C, D, E, and F are incorrect based on the above.
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have any clue how you get "false false" as output on your machine. The answer is "true true", f1 and f3 do indeed point to the same object. Read this tutorial for more info on calling methods with objects...
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Mahmoud,

I have run the program over my machine and it is giving me true true, that is answer A, which is basically correct.
How come you are getting false false? The flow of the program is like this

1- First the first final object is created which is f1 and having value of x as 5.
2- Second a non-final instance is created called f2 and having value of x as 5.
3- On the third step, a method call to fizzSwitch() is made in which instances f1 and f2 are passed and their return instance has to be assigned to instance three which is f3.
4- If you see the method signature


It has two parameters that are instances x and y.

5- When the method call is made to fizzSwitch, this state is made

x and f1 referrring to instance 1
y and f2 referring to instance 2

Inside the method, first line, the final reference z also starts pointing towards instance 1. I

6- It changes the state of object 1 and now instead of 5 x is having the value 6.
7- Remember that, the state of final objects can be changed and that is what happened to object 1.
8- Then the reference is returned and the method call completes and at this point reference f3 is also referring towards object of fizztest.


When this statement is encountered




Basically the equals method checks whether two references are referring to the same object or not. Remember object class equals method using = operator to check for equality
unless and until you override the implementation. In this case case both are referring to the same object, so first condition answer is true.

When you say



So both references are referring to the same object and the answer will be true as the value of x is 6.

Hope this helps,



 
Mahmoud Hossam
Ranch Hand
Posts: 74
Netbeans IDE Tomcat Server Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh,I wrote FizzSwitch(f2,f2) instead of (f1,f2),that's why I got "false false"

I'm so sorry for the inconvenience

btw,where can I find the errata for this book,I want a reliable source on this one
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this

Hope this helps,



 
Mahmoud Hossam
Ranch Hand
Posts: 74
Netbeans IDE Tomcat Server Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:Check this

Hope this helps,



isn't there a "confirmed" errata for SCJP 6 ?
 
Ankit Garg
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mahmoud Hossam wrote:isn't there a "confirmed" errata for SCJP 6 ?


Right now there isn't...
 
Mahmoud Hossam
Ranch Hand
Posts: 74
Netbeans IDE Tomcat Server Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay,thanks everybody !
 
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