• Post Reply Bookmark Topic Watch Topic
  • New Topic

about a Math.sqrt()  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why this class compile:
class Zero{
public static void main(String args[]){
int x = 10;
System.out.println(Math.sqrt(x)); // HERE , this work so...
}
}

If in the Documentation of java.lang.Math say:
static double sqrt(double a)
Returns the correctly rounded positive square root of a double value.
 
Mika Leino
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compilation succeeds because an int can be implicitly casted to double when needed. The JVM is able to widen the primitive type without the programmer explicitly casting it, but the opposite is not true. Hence the sqrt() method will handle that int as an double.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int 10 is promoted to double 10.0 and sq rt of 10.0 is returned. I am not sure what are you asking?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, i think so, but how to now when this implicit casted is made?
I now that the println() do it that with the "toString", i guess that is made with the basic class of java.lang or not all.
The Java is really great.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implicit cast is made for :
1. byte > short > int > long > float > double
2. char > int > long > float > double
Hope this helps.
Barkat
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK fried, but i mean about the methods, not all the methods make you an automatic cast.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you mean method arguments, yes they can have implicit cast as well.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barkat's right: widening conversion of primitives works both for assignment, and method arguments, and for return types as well. There are some rare cases when it performs strangely on converting int numbers to float (credit to Marlene and Thomas), but otherwise no problem.
 
Lalitha Chandran
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Here is a piece of code which doesn't compile :--- where a byte value is not implicitly cast to an int in method invocation.
public class Question18 {
public static void main(String[] args){
for(int i=0;i<10;i++){
System.out.println(getPrimitive(127));//line 1
}
}
public static int getPrimitive(byte b){//line 2
return (short)(Math.random()*b);//line 3
}
}
In that case how do we know where does the cast takes place and where it doesn't.
Regards,
Lalitha Chandran
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lalitha, it only looks like getPrimitive() is being passed a byte. In fact, any whole numeric literal is int by default unless it's postfixed with L or l or explicitly cast to narrow types. So, 127 does fit into byte, but it's int, therefore, compilation fails. getPrimitive((byte)127); should work.
 
kashif hameed
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear,
I think it is impilicity casted into the Double.bcz when every arithmatic operation is performed then the values are first casted into double then the arithmatic operation is performaed on it.[JLS].
Kashif Hameed!!!
 
Lalitha Chandran
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it.......Thanks
 
Gurukeerthi Gurunathan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Lalitha's example to work, we need an explicit cast, but at the same time an assignment like,
byte b = 127;
will perfectly compile. I think there is a difference between passing a value as a method argument and assigning. Even this will work,
byte b = (int)127;
Why is that so?
-GuruG.
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by gurug:
For Lalitha's example to work, we need an explicit cast, but at the same time an assignment like,
byte b = 127;
will perfectly compile. I think there is a difference between passing a value as a method argument and assigning. Even this will work,
byte b = (int)127;
Why is that so?
-GuruG.

Java performs implicit narrowing conversion for literals provided that they fall within the legal range of the primitive type. Thus, assigning 127 to a byte is allowed because a byte can accept that much. However, assigning a value of 128 to a byte would fail without an explicit conversion. But take note that this automatic narrowing does not apply to method invocation. It only applies to assignment. So Lalitha's example failed because automatic narrowing was not performed.
[ September 25, 2003: Message edited by: Alton Hernandez ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!