Results 1 to 10 of 10
  1. #1
    orchid's Avatar
    orchid is offline Member
    Join Date
    Apr 2007
    Location
    Midwest
    Posts
    60
    Rep Power
    0

    Default Question mark colon operator question

    I inherited some code that I simply cannot sort out. Can anyone tell me why this prints off X88. It makes no sense to me.
    Thanks
    Java Code:
    char x = 'X';
    int i = 0;
    System.out.print(true  ? x : 0);
    System.out.print(false ? i : x);

  2. #2
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Thumbs down Ternary Operators

    This is an interesting operator in Java.
    Java Code:
    class TernaryOperator{
    
         public static void main(String[] args){
              int value1 = 1;
              int value2 = 2;
              int result;
              boolean someCondition = true;
              result = someCondition ? value1 : value2;
              System.out.println("First Result " +result);
    
              someCondition = false;
              result = someCondition ? value1 : value2;
    
    
              System.out.println("Second  Result " +result);
    
         }
    }
    Output:
    First Result 1
    Second Result 2

    The code is self explanatory

  3. #3
    orchid's Avatar
    orchid is offline Member
    Join Date
    Apr 2007
    Location
    Midwest
    Posts
    60
    Rep Power
    0

    Default

    It is not terribly readable. Why woulc anyone use it.

  4. #4
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    I forgot the answer the question ,BTW In java the expressions are evaluated from left to right
    hence first one will evaluated as character
    and second one will be an int type. The ascii value of 'X' is 88

    Say what will happen


    System.out.println( 2 + 2 + "2") // print 4
    System.out.println( "" +2 + 2 ) // print 22

  5. #5
    miss.meli's Avatar
    miss.meli is offline Member
    Join Date
    Nov 2008
    Location
    Austin, TX
    Posts
    17
    Rep Power
    0

    Default

    So this ? : operator is similar to an if else statement?

  6. #6
    dharmender378 is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    [QUOTE=goldhouse;285]I forgot the answer the question ,BTW In java the expressions are evaluated from left to right
    hence first one will evaluated as character
    and second one will be an int type. The ascii value of 'X' is 88

    Say what will happen


    System.out.println( 2 + 2 + "2") // print 4
    System.out.println( "" +2 + 2 ) // print 22[/QUOTE
    Ans:
    System.out.println( 2 + 2 + "2") --------> Prints 42
    System.out.println( "" +2 + 2 )----------->Prints 22
    Last edited by dharmender378; 11-25-2008 at 12:40 PM. Reason: alignment

  7. #7
    dharmender378 is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by miss.meli View Post
    So this ? : operator is similar to an if else statement?
    Yes, if the condition evaluates to be true then exp before the colon is executed otherwise the second exp post the colon is executed.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    > In java the expressions are evaluated from left to right
    hence first one will evaluated as character
    No.

    The type returned by a ternary is the narrowest type that can accommodate both values. Try this code and clear up any misconception.
    Java Code:
    System.out.println(true ? 1.0 : 3);
    System.out.println(true ? 1 : 3.0);
    db

  9. #9
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Talking handy-dandy code candy

    Quote Originally Posted by orchid View Post
    It is not terribly readable. Why woulc anyone use it.
    Most people consider ternary operator to be difficult to read, we can expand thus wherever found:
    Java Code:
    ( variable1.equals(variable2)? 5: 4  );
    to
    Java Code:
    if(variable1.equals(variable2))return 5;
    else return 4;
    I use ternary operator once in maybe 500 lines of code, that's when I am in a mood to use it.

    Especially challenging, if you want, is to code:
    Java Code:
    if(variable1.equals(variable2))
    {
        if(variable2.equals(variable3))
        {
            System.out.println("proceed");
        }
        else
       {
           System.out.println("not available");
       }
    }
    else
    {
         processData();
    }
    In ternary operators.

    Looks convoluted in ternary operators, and is especially tangled to sort out when one does not have a great deal of code already written. What evenutally evolves is something called Trees, which remarkably can be written: ( ( ( ( ) ( ) ) ( ) )( ) )
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Mashamskw View Post
    Вы собираетесь посетить Москву, но не знаете, где остановиться? Ищете [b]недорогую гостиницу, отель дешево, проживание в командировке

    spam post, spammer!

Similar Threads

  1. JSP Question
    By maheshkumarjava in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-29-2008, 10:51 AM
  2. Need help on this question
    By Deon in forum New To Java
    Replies: 3
    Last Post: 01-27-2008, 03:58 PM
  3. JNI question
    By javaplus in forum New To Java
    Replies: 0
    Last Post: 12-24-2007, 10:18 AM
  4. Need help with a question please
    By sonal in forum New To Java
    Replies: 1
    Last Post: 11-29-2007, 09:17 PM
  5. question about rmi
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:19 AM

Posting Permissions

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