aspose file tools*
The moose likes Java in General and the fly likes Explain this behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Explain this behaviour" Watch "Explain this behaviour" New topic
Author

Explain this behaviour

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

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


Ashwin Sridhar
SCJP | SCWCD | OCA
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18136
    
  39

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


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

    Joined: Oct 21, 2000
    Posts: 4339
        
        2

    i think they hired unemployed lawyers to write it


    SCJP
    Visit my download page
    Ashwin Sridhar
    Ranch Hand

    Joined: Jul 09, 2011
    Posts: 272

    Thank you all for helping me with this query
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    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

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    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
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18136
        
      39

    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Explain this behaviour
     
    Similar Threads
    Puzzled with the output of this programme
    Static????????????
    Can someone please explain this problem?
    switch case basic
    While loop - Head's First Java example