• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

return value from assignment

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All:
I guess I learned in this forum that any assignment within an expression will return the assigned value to the next context level so:
boolean a, b;
a = (b=true);
then a will also be assigned true. In the following code, the bold assignments should return nulls and hence most specific classes (B,B) should be invoked. But assignments return (a,b) and (A,B) method is invoked. Why?

Thanks
Barkat
[ September 19, 2002: Message edited by: Barkat Mardhani ]
[ September 19, 2002: Message edited by: Barkat Mardhani ]
[ September 19, 2002: Message edited by: Barkat Mardhani ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, bold tags do not appear within the CODE tags. What Barkat means by "bold assignment" is the line
m(a=null,b=null);
The reason why the output is BBABBA is because the most specific method is invoked when multiple method signature match the invocation expression. In this case, the most specific method (as per JLS 15.12.2.2 Choose the Most Specific Method) is the method taking two parameters of type B. B is more specific than A.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Val:
Thanks for your reply. I understand first and third call for m method. It is the second call that is confusing:
m(a=null,b=null)
should result in:
m(null,null)
as I explained in previous post. Hence m(null,null) should call m(B,B) not m(A,B)... but it is calling m(A,B) why...
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, because in
m(a=null,b=null)
we have an indication of which method to invoke since a and b are of type A, respectively B, even though their value is null.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Valentin. The only one where the compiler has to think a bit is the m(null,null) case. In the others the type of the passed arguments is well defined.
-Barry
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me ask in another way:
boolean a, b;
a = (b=true);
Will you agree that (b=true) expression will yield true and that will be assigned to a?
[ September 20, 2002: Message edited by: Barkat Mardhani ]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes I agree. And?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't change the types of a and b, they're still
booleans
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
therefore (a=null) should result in null
and (b=null) should result in null
therefore (a=null,b=null) should result in (null,null)
therefore m(a=null,b=null) should result in m(null, null)
therefore m(null, null) should call most specific method m(B,B) not m(A,B)...Right? :roll:
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are back to the first example with A a; B b; ?
When the compiler sees m(a=null, b=null) it sees
a call to m(A, B) because a is of type A and b is of type B. It has one of those to call so it does that.
The fact that a value is being assigned to a and b at the time is of no matter. Instead of assigning nulls you could be doing m(a=new A(), b= new B()).
-Barry
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry and Val...
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If java was a true object oriented language (I have my head on offer saying this) we could do things like System.out.println((a=null).type)). That would help a lot in clarifying these cases.
Cheers
-Barry
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Output is:
class A
[ September 20, 2002: Message edited by: Valentin Crettaz ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin, here it is ---> on a plate.
Hey Hang on! I take that back...
<------
You made that up. Boo! Unfair!
I want Java to tell me the type of result of an expression. Like built in...
On the other hand, it is rather neat.
-------->
[ September 20, 2002: Message edited by: Barry Gaunt ]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You made that up. Boo! Unfair!
Well, I did it with what Java provides me
I want Java to tell me the type of result of an expression. Like built in...
Sure it would be nice
You want built-ins, there, get some:

Actually all wrapper classes provide a constant called TYPE which resolves to the primitive type represented by the class type. Not quite what we wanted, but they are built-ins
On the other hand, it is rather neat.
isn't it? At least it helps clear Barkat's doubts
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys. That was enlightening. I guess I was thinking along the line that m(a=null,b=null) statement will try to resolve the VALUE of 'a=null' assignment. But there is another twist to it i.e. what is TYPE of 'a=null' assignment, and m(a=null,b=null) is more worried about TYPE than VALUE for correct method invocation.
Please tell me that I got it, because I hate to waste your time...
[ September 20, 2002: Message edited by: Barkat Mardhani ]
[ September 20, 2002: Message edited by: Barkat Mardhani ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barkat, yes that's about it (I think )
-Barry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic