| Author |
simple or complicated?
|
devika cha
Greenhorn
Joined: Dec 17, 2003
Posts: 23
|
|
Look at the code below: public class floor { static String m(float i) {return "float";} static String m(double i) {return "double";} public static void main (String[] args) { int a1 = 1; long b1 = 2; System.out.print(m(a1)+","+ m(b1)); }} it prints: float float My question is whatever is passed as the parameter to the methods but it returns string type and so it should print --> float double can anybody explain.
|
 |
Anurag Mishra
Ranch Hand
Joined: Jun 16, 2003
Posts: 121
|
|
Hi, In java there is something called Widening conversion for Primitive data types, Widening conversions change a value to a type that accomodates a wider range of values than theoriginal type can accomodate.In most cases the new type has more bitsthan the original. Widining Conversion follows this path byte-->short(or char)-->int-->long-->float-->double so here in ur case when ur doing this m(a1)--> here ur argument is of type int but u don't have any method which takes int as argument so it will follow WIdening conversion and call next higher type argument method here its FLOAT so it prints float. m(b1)--> here simlarly ur passing b1 as long which again follows widining path n calls method with FLOAT n print float. I think you just read any Java book it will give u more clear idea. thanks Anurag
|
SCJP 1.2
|
 |
Greg Schultz
Greenhorn
Joined: Oct 08, 2003
Posts: 16
|
|
See Java Language Specification: http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#189955 5.1.2 Widening Primitive Conversion The following 19 specific conversions on primitive types are called the widening primitive conversions: byte to short, int, long, float, or double short to int, long, float, or double char to int, long, float, or double int to long, float, or double long to float or double float to double Greg [ January 06, 2004: Message edited by: Greg Schultz ]
|
 |
 |
|
|
subject: simple or complicated?
|
|
|