• 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

Conditional Operator with different return types

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A question I encountered on a practice exam:
QUESTION : 13
What is the output of the following piece of code
1. int x = 6;
2. double d = 7.7;
3. System.out.println((x>d) ? 99.9 ; 9);

(semicolon is a typo...it should be a colon)
The answer here is 9.0
When I played around with this on the compiler, I noticed some interesting things. When I used a char in place of the 9, I don't get an output. When I switch around, and use 99.9 : 'd', the output is 100, which is obviously the unicode designation for 'd'. What rules does it use to assign type to the return value? Does it automatically convert the types to the largest type in the answer, in this example double or is there some other rule.
Thanks
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are several type conversions going on here. In the logic statement x > d - x is converted to double.
The type conversion in the logic has nothing to do with the conversion that System.out.println performs - apparently it has decided that you want to do print( int n )
If you want it forced to print a String conversion, I think
System.out.println("" + (x>d) ? 99.9 ; 9);
will work.
Bill
 
Diesel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill,
Thanks for the quick reply once again. Unfortunatly, I am still confused about the generic rule for this conditional statement. When the runtime environement encounters this statment ((test)?x:y), if x and y are different types, what will the output be. I understand that both x and y will be converted to the same type, but what is that type.
Thanks
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Diesel,
The rules applied for the conditional operator is slightly complicated. But once you get a grasp of it, it is very easy. I am explaing these rule with the following notaion.
(test)? : 2ndOperand :3rdOperand
The rules are the
1. (test) MUST evaluate to boolean.
2. 2nd and 3rd operand expressions can take one of the following 6 cases explained in the example below. Please go through ease case carefully. Please come back if you have anything to ask.
You can get the JLS rules for conditional operator here. Since just reading JLS may look DRY at first, I wrote a small example program for you to visualize easily. Also note that the 2nd and 3rd operands postion can be interchanged. 2nd operand can come in 3rd'd position and vice versa.
regds
maha anna

<pre>class Base {
public String toString() {
return "Base";
}
}
class Sub extends Base {
public String toString() {
return "Sub";
}
}
class Test {
final static int INT_CONSTANT = 10;
public static void main(String[] args) {
int x=6;
double d=7.7d;
byte b1 = 1;
byte b2 = 2;
char c1='A';
char c2='B';
short s1=3;
short s2=4;
int i1=5;
int i2=6;
long l1=7;
long l2=8;
float f1 =9;
float f2 =10;
double d1 =11;
double d2 =12;
Boolean bln1 = new Boolean(true);
Integer intr1 = new Integer(10);
Base base = new Base();
Sub sub = new Sub();
//(test)? : 2ndOperand :3rdOperand
//case 1. 2nd and 3rd SAME type RESULT = SAME type
System.out.println( (x>d) ? b1 :b2); //prints 2 ( type 'byte')
System.out.println( (x>d) ? c1 :c2); //prints 'B' ( type 'char')
System.out.println( (x>d) ? s1 :s2); //prints 4 ( type 'short')
System.out.println( (x>d) ? i1 :i2); //prints 6 ( type 'int')
System.out.println( (x>d) ? f1 :f2); //prints 10.0 ( type 'float')
System.out.println( (x>d) ? d1 :d2); //prints 12.0 ( type 'double')
System.out.println( ""+ ( (x>d) ? null :null)); //prints null
System.out.println( ""+ ( (x>d) ? base :base)); //prints "Base"
System.out.println( ""+ ( (x>d) ? sub :sub)); //prints "Sub"

//case 2. 2nd = byte (AND) 3rd = short RESULT = short
//byte b3 = (x>d) ? b1 : s1; //compiler error. Can't convert short to byte

//case 3. 2nd = byte (AND) 3rd = char RESULT = int
//byte b3 = (x>d) ? b1 : s1; //compiler error. Can't convert 'int' to byte
//case 4. 2nd = byte/char/short (AND) 3rd = constant 'int' literal
//which is whichin the rangeof 2nd type. RESULT = 2nd operand's type

System.out.println( (x>d) ? b1 : 10); //prints 10 ( type 'byte')
System.out.println( (x>d) ? c1 : 100); //prints 'd' ( type 'char')
System.out.println( (x>d) ? s1 : INT_CONSTANT); //prints 10 ( type 'short')

//case 5 (All other cases except cases 1,2,3)
// RESULT = Binary Numeric promotion occurs
System.out.println( (x>d) ? b1 : i1); //prints 5 ( type 'int')
System.out.println( (x>d) ? c1 : f1); //prints 9.0 ( type 'float')
System.out.println( (x>d) ? s1 : d1); //prints 11.0( type 'double')
//case 6 2nd is reference type 3rd is also reference type
//Note here one ref MUST be convertible to another reference.

System.out.println( (x>d) ? base:sub); //prints 'Sub'
//case 6.1 2nd is reference type 3rd is also reference type
But Not convertible

//System.out.println( (x>d) ? intr1:bln1); //Compiler error.
//Can't convert Integer to Boolean
}
}
</pre>

[This message has been edited by maha anna (edited August 14, 2000).]
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maha
THat was a pretty impressive and thorough example.
Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic