Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt

 
Rajan Narayanan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Ex2 {
public static void main (String args[]) {
int a = 4;
System.out.println("Value is "+
((a>4) ?99.99:9));
}
}
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajan Narayanan:
public class Ex2 {
public static void main (String args[]) {
int a = 4;
System.out.println("Value is "+
((a>4) ?99.99:9));
}
}

__________________________________________________
The o/p is : Value is 9.0

 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajan Narayanan:
public class Ex2 {
public static void main (String args[]) {
int a = 4;
System.out.println("Value is "+
((a>4) ?99.99:9));
}
}

Already discussed here http://www.javaranch.com/ubb/Forum24/HTML/003095.html
Savithri
 
Rajan Narayanan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Thnx for ur reply, but when i compiled and run it shows only 9 not 9.0 , can u explain out the reason.
Thnx
Rajan
 
daryl olson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajan,
Are you sure it returned 9?
I tried it and it returned 9.0 as expected (under 1.2.2).
 
Rajan Narayanan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm very much sure, Please help me, this is the code which compiled and executed...
public class Ex2 {
public static void main (String args[]) {
int x = 4;
System.out.println("value is :"+((x>4) ?99.99:9));

}
}
the output is value is 9
i donno the reason..whether it varies from compiler to compiler..
Help me out...
thanks
Rajan
 
daryl olson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried again and got 9.0.
As this is what is expected based I JLS, I don't know how you are getting 9
Anyone else wish to confirm this?
 
mehrar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by daryl o:
I tried again and got 9.0.
As this is what is expected based I JLS, I don't know how you are getting 9
Anyone else wish to confirm this?

Me 2 got 9.0. I am using JDK 1.2.1
------------------
Raj
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajan Narayanan,
Can you tell us the platform and jdk version you are using?. Because as per JLS the 9 will be promoted to 9.0 double (binary promotion) and since the test condition is 'false', 9.0 will be printed.
regds
maha anna
 
thomas
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getting 9.0 (like maha anna explained)
JDK 1.2 on WINDOWS 98
 
thejus/mahesh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes answer is 9.0 only we use win-nt and jdk1.2.1
 
Tushar Kansara
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure why is 9.0 being printed.
Are there any rules for Integer to be promoted to Float while being printed.
Regards
Tushar kansara
------------------
 
Rajan Narayanan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Thanx for all ur help !!! i guess i'm 'ving JDK 1.1, that's y i got 9 as output.
lemme try in higher version.
thanx
rajan.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic