• 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

simple or complicated?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic