• 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

Explain this behaviour

 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Consider the following code



The output that i get is X88. it is converting false statement value of X into ascii. If i change int i to double i, the output changes to X88.0.

Can someone explain, why it changes based on datatype
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashwin Sridhar wrote:The output that i get is X88. it is converting false statement value of X into ascii. If i change int i to double i, the output changes to X88.0.


It all has to do with Widening Primitive Conversions. A char can be widened to either an int or a double, so the compiler assumes that that is, in fact, what you wanted.

Winston
 
Ashwin Sridhar
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you winston. Still i find it strange, why should it widen actually. its more like a IF ELSE block.
Why is the widenig happening depending on the ELSE block datatype
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashwin Sridhar wrote:Thank you winston. Still i find it strange, why should it widen actually. its more like a IF ELSE block.
Why is the widenig happening depending on the ELSE block datatype



Deep in the bowels of Section 15.25 of the Java Language Specification (portion of the section that explains what type the ternary expression is) ...

Otherwise, if the second and third operands have numeric type, then there are several cases:

  • 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. Note that binary numeric promotion performs value set conversion (§5.1.8).



  • This, of course, explains what you are seeing. What it doesn't explain, is why the JLS is written that way ... ?!?

    Henry

     
    Ranch Hand
    Posts: 4716
    9
    Scala Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i think they hired unemployed lawyers to write it
     
    Ashwin Sridhar
    Ranch Hand
    Posts: 277
    Oracle Spring Flex
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you all for helping me with this query
     
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ashwin Sridhar wrote:its more like a IF ELSE block.



    Incorrect. That's an assumption that you are making, but it has nothing to do with the actual definition of that operator. That simplification, like many, is fine in certain contexts, but you have to know its limits and not try to use it beyond those limits.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Henry Wong wrote:
    This, of course, explains what you are seeing. What it doesn't explain, is why the JLS is written that way ... ?!?

    Henry



    I suppose it's so that the expression could have one consistent type, rather than having its type depend on truth of the condition.
     
    Henry Wong
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jeff Verdegan wrote:
    I suppose it's so that the expression could have one consistent type, rather than having its type depend on truth of the condition.




    Sorry, I kinda misread the quote (which is bad since I quoted it).... I thought one of them had mentioned a case where it depended on the order (whether 2nd or 3rd operand).

    Never mind. We are all good.... and to elaborate the answer, just in case it isn't obvious.



    In the first case, zero is a literal, and hence, a compile time constant. It also is representable as a char (the other operand), so, following the second point of the quote, the expression is a char type.

    In the second case, following the third point of quote. To get a char and int to be the same type, the char is promoted to an int. So, the expression is an int type.

    Henry
    reply
      Bookmark Topic Watch Topic
    • New Topic