• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusing me the two question?please help ~~

 
Tu Ran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First:
" In this statement that uses the ternary operator: int a=b?c what
tye fo variable should b be?
A: an integer
B:the same variable type as 'c'
c:the same variable type as 'd'
d: the boolean
(I think no one correct answer?but I am wrong? why can you tell me?)
Second:
class Test{
public static void main(String[] args){
float x=-214783650.6f;
System.out.println(Math.round(x));
}
}//Why x=-214783648??? but I think x should equal to -214783651
 
Po-yu Chien
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For ternary operator, I guess the boolean value
should be the answer.
It`s like:
if(b) {
a = c;
}
so b must be a boolean value.
However I cannot respond to your second question.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tu Ran,
The Math.round() method taking a float as argument returns an int whose minimal value is -2147483648.
Moreover, the javadoc for Math.round() says that "If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE."
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ternary operator is equivalent to the if/else,assignment constructor

So b must be a boolean value.
For your second question, you are converting a float to a int value using Math.round(), what you're seeing is called a "overflow" error.
Floating point values are stored differently then integer values, as such large values cannot truly have a one to one mapping between the two types!
[ February 22, 2002: Message edited by: Rajinder Yadav ]
 
Tu Ran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks my friends
Valentin Crettaz
Rajinder Yadav
Po-yu Chien
I will study hard in java~~thanks again!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic