aspose file tools*
The moose likes Beginning Java and the fly likes how return value is resolved Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how return value is resolved" Watch "how return value is resolved" New topic
Author

how return value is resolved

vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88


I am not able to understand how does the compiler knows the return type, as here is only assignment.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

An assignment has a value. And that value is the variable being assigned. With this, these expression...

c = new C();
c2 = c;

can be chained to this...

c2 = c = new C();


Originally posted by vijay shanker:


I am not able to understand how does the compiler knows the return type, as here is only assignment.


For your code, if c is equal to null, the variable is assigned, but the ternary operator then returns the value of the variable that has just been assigned.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brian Lang
Ranch Hand

Joined: Oct 21, 2008
Posts: 43
A bit OT here, and I'm probably taking a glib example and blowing it out of proportion...

I understand the ternary operator alright, and how the method returns either a new C reference or the original reference, but what if the c field isn't an instanceof C? Shouldn't there be a check for that?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Brian Lang:

I understand the ternary operator alright, and how the method returns either a new C reference or the original reference, but what if the c field isn't an instanceof C? Shouldn't there be a check for that?


Given that the return type is C, and "new C()" is assigned to c, this code will not compile unless c is declared as being of type "C". So there is such a check; it's done when this code is compiled. It would impossible for c to contain an object which wasn't type-compatible with C at runtime, so once it's compiled, there's no way the code can fail.


[Jess in Action][AskingGoodQuestions]
Brian Lang
Ranch Hand

Joined: Oct 21, 2008
Posts: 43
Originally posted by Ernest Friedman-Hill:
this code will not compile unless c is declared as being of type "C".


Thank you, that's just what I was looking for. I guess I was thinking it would be possible to change the reference at run time, but it's already set in bytecode and can't change.
vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
thanks, for your replies, i was not aware of that behavior of this operator.

actually c is an instance variable of type C in a class. So,.......
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
vijay,
the operator does not have that effect,

the variable c has to be defined somewhere,
and it has to be C because it's compiling,
so we're assuming it's a instance variable or global typed C,



there could be a superclass (B) or interface (Clike) and it would be a valid assignment statement


however, for the
return c;
to be valid, the variable must be defined as a C or subclass of C,


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how return value is resolved