File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes simple   or complicated? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple   or complicated?" Watch "simple   or complicated?" New topic

simple or complicated?

devika cha

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
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.

SCJP 1.2
Greg Schultz

Joined: Oct 08, 2003
Posts: 16
See Java Language Specification:
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

[ January 06, 2004: Message edited by: Greg Schultz ]
I agree. Here's the link:
subject: simple or complicated?
jQuery in Action, 3rd edition