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

Primitive data types

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);

Ans :
a. 9
b. 9.0
c. 99.9
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b. 9.0.
As RH&E say, Java determines the result type of the conditional, or ternary, operator when it compiles. Java sees it will be choosing between an int and a double, so it picks the wider of the two (double) for the ternary's result type.
See the second bullet point's third sub-bullet here in the Java Language Specification for more details.
HTH
Art
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic