Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-29-2007, 10:45 PM
orchid's Avatar
Member
 
Join Date: Apr 2007
Location: Midwest
Posts: 60
Rep Power: 0
orchid is on a distinguished road
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
Code:
char x = 'X';
int i = 0;
System.out.print(true  ? x : 0);
System.out.print(false ? i : x);
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-30-2007, 10:52 PM
Senior Member
 
Join Date: Mar 2007
Posts: 135
Rep Power: 0
goldhouse is on a distinguished road
Thumbs down Ternary Operators
This is an interesting operator in 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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-30-2007, 11:16 PM
orchid's Avatar
Member
 
Join Date: Apr 2007
Location: Midwest
Posts: 60
Rep Power: 0
orchid is on a distinguished road
Default
It is not terribly readable. Why woulc anyone use it.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-30-2007, 11:37 PM
Senior Member
 
Join Date: Mar 2007
Posts: 135
Rep Power: 0
goldhouse is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-12-2008, 09:08 AM
miss.meli's Avatar
Member
 
Join Date: Nov 2008
Location: Austin, TX
Posts: 17
Rep Power: 0
miss.meli is on a distinguished road
Default
So this ? : operator is similar to an if else statement?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-25-2008, 01:38 PM
Member
 
Join Date: Nov 2008
Posts: 16
Rep Power: 0
dharmender378 is on a distinguished road
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 01:40 PM. Reason: alignment
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-25-2008, 01:42 PM
Member
 
Join Date: Nov 2008
Posts: 16
Rep Power: 0
dharmender378 is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-26-2008, 10:26 AM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 752
Rep Power: 2
Darryl.Burke is on a distinguished road
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.
Code:
System.out.println(true ? 1.0 : 3);
System.out.println(true ? 1 : 3.0);
db
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-27-2008, 07:36 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Talking handy-dandy code candy
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:
Code:
( variable1.equals(variable2)? 5: 4  );
to
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:
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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP Question maheshkumarjava JavaServer Pages (JSP) and JSTL 1 03-29-2008 11:51 AM
Need help on this question Deon New To Java 3 01-27-2008 04:58 PM
JNI question javaplus New To Java 0 12-24-2007 11:18 AM
Need help with a question please sonal New To Java 1 11-29-2007 10:17 PM
question about rmi leonard New To Java 1 08-06-2007 05:19 AM


All times are GMT +2. The time now is 12:58 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org