• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Ternary operators in System.out.println()

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is based on code from enthuware:



I get the following compiler errors:

type.java:4: <identifier> expected
System.out.println(i<20 ? out1() : out2());
^
type.java:4: illegal start of type
System.out.println(i<20 ? out1() : out2());
^
type.java:4: <identifier> expected
System.out.println(i<20 ? out1() : out2());
^
type.java:4: ')' expected
System.out.println(i<20 ? out1() : out2());
^
type.java:4: ';' expected
System.out.println(i<20 ? out1() : out2());
^
type.java:4: illegal start of type
System.out.println(i<20 ? out1() : out2());
^
type.java:4: <identifier> expected
System.out.println(i<20 ? out1() : out2());
^
type.java:4: ';' expected
System.out.println(i<20 ? out1() : out2());
^
type.java:4: illegal start of type
System.out.println(i<20 ? out1() : out2());
^
type.java:4: <identifier> expected
System.out.println(i<20 ? out1() : out2());
^
type.java:4: ';' expected
System.out.println(i<20 ? out1() : out2());



Enthuware gives the following explanation:

Note that it is not permitted for either the second or the third operand expression to be an invocation of a void method.
If one of the operands is of type byte and the other is of type short, then the type of the conditional expression is short.
If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T.

Otherwise, binary numeric promotion (5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.
If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.
If the second and third operands are of different reference types, then it must be possible to convert one of the types to the other type (call this latter type T) by assignment conversion (5.2); the type of the conditional expression is T. It is a compile-time error if neither type is assignment compatible with the other type.




I really do not understand this. Can you explain in a simple-to-understand way?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last two operands in a ternary expression must result in some kind of value. It's that simple.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sandra Bachan wrote:Following is based on code from enthuware:





Although your methods are returning 0, both methods have a void return type meaning they return nothing. System.out.println is expecting the method to return something to print. Also, you are trying to print outside of a method. Try changing the code to the following (added main method, changed methods out1 and out2 to static with int return type):



Changing the code above to return a void type now results in errors "'void' type not allowed here" and "cannot return a value from method whose result type is void".


 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I understand, thank you!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!