• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about a Math.sqrt()

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you mean method arguments, yes they can have implicit cast as well.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it.......Thanks
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic