Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

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
orchid is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 04-30-2007, 10:52 PM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
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
orchid is on a distinguished road
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: 136
goldhouse is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 +3. The time now is 03:23 AM.


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