Results 1 to 6 of 6
  1. #1
    Nitika is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default Conditional operator

    int a=1,c=2;
    byte d = (a<c)?1:2; ------------------------1
    The above statement give compile time error and
    if we use
    byte d = (true)?1:2;
    it will work fine.

    I want to ask why statement 1 is given compile time error and required type cast into the byte.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Conditional operator

    The second expression can be evaluated by the compiler, i.e. it 'knows' the value of 'true' and it can deduce that 1 fits in a byte. Not so with the first expression: it doesn't 'know' the value of 'a < c' (without interpreting the code which it can't) so it gives up; the values 1 and 2 are integers as far as the compiler is concerned but the target is a byte, so a cast is needed.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Nitika is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default Re: Conditional operator

    Thank you

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Conditional operator

    Along similar lines to what Jos said, if you declare a and c as final, then the cast is not necessary. Since the compiler
    knows the values can't change, it evaluates them itself.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Nitika is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default Re: Conditional operator

    I want to read these things in details. Can you suggest me some websites.

    Thanks,
    Nitika

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Conditional operator

    Here you go: https://docs.oracle.com/javase/specs/

    kind regards,

    Jos

    ps. it's language lawyer stuff; not an easy read ...
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Conditional Formatting
    By mimidep in forum Apache POI
    Replies: 0
    Last Post: 06-12-2014, 12:27 AM
  2. Unary operator and binary operator
    By suhaas.mohandos@gmail.com in forum New To Java
    Replies: 10
    Last Post: 08-27-2013, 05:39 PM
  3. Replies: 5
    Last Post: 01-22-2012, 12:02 AM
  4. conditional statement
    By blindfolded916 in forum New To Java
    Replies: 12
    Last Post: 07-11-2010, 10:09 AM
  5. how to do conditional looping?
    By chennee72 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-09-2008, 01:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •