• 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

Chapter 3 question 5 page 279

 
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Fizz {

int x = 5;

public static void main(String args[]) {
final Fizz f1 = new Fizz();
Fizz f2 = new Fizz();
Fizz f3 = FizzSwitch(f1, f2);
System.out.println((f1 == f3) + " " + (f1.x == f3.x));
}

static Fizz FizzSwitch(Fizz x, Fizz y) {
final Fizz z = x;
z.x = 6;
return z;

}
}



The outcome for the above code is

'true true'

The reason given in the book is:

"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 doesnt keep the objects state from changing."

I am having difficulty understanding this. I can see that f1.x == f3.x but how does

f1 == f3 ???
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
start from Here
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and Welcome to JavaRanch Glen . Use code tag while posting your code.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the first thing you need to ask is, what does 'f1 == f3' actually compare? And do these get passed around function calls?
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Glen,

What is returned by the method FizzSwitch?
 
Ranch Hand
Posts: 62
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Glen Iris wrote: class Fizz {

int x = 5;

public static void main(String args[]) {
final Fizz f1 = new Fizz();
Fizz f2 = new Fizz();
Fizz f3 = FizzSwitch(f1, f2);
System.out.println((f1 == f3) + " " + (f1.x == f3.x));
}

static Fizz FizzSwitch(Fizz x, Fizz y) {
final Fizz z = x;
z.x = 6;
return z;

}
}



The outcome for the above code is

'true true'

The reason given in the book is:

"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 doesnt keep the objects state from changing."

I am having difficulty understanding this. I can see that f1.x == f3.x but how does

f1 == f3 ???

In the code, in the method FizzSwitch(f1,f2) , you are passing f1 as first argument and when it reaches method definition, f1 is copied to x, then same object (f1=x) is getting assigned to z.Now here , z is an instance that is exactly same as f1 and when it is returned , it will be assigned to f3. So f1==f3 will return true..

I hope this clears your confusion.................
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Glen.
The reference variable f1,f3,z even thought they might be final. Here they are just copied to each other. so in the Start final Fizz f1 = new Fizz(); the value in f1 ie the location of the object is just copied to all the other variables. hence when f1==f3 we are actually comparing the location, which is the same.
thanks
 
Glen Iris
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys, especially Prashanth Patha, Seetharaman Venkatasamy and stanley hataria.

The part that was confusing me was z.x = 6; but I understad it now. Thanks again
reply
    Bookmark Topic Watch Topic
  • New Topic