• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Dan's Wrapper Exam Question

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt in Dan's exam ...Consider the following piece of code :
class A {
public static void main (String args[]) {
byte primitiveByte = 1;
int primitiveInt = 1;
long primitiveLong = 1L;
float primitiveFloat = 1f;
String s = "1";
Long i1 = new Long(primitiveByte);
Long i2 = new Long(primitiveInt);
Long i3 = new Long(primitiveLong);
Long i4 = new Long(primitiveFloat);
Long i5 = new Long(s);
int i6 = i1.intValue() + i2.intValue() +
i3.intValue() + i4.intValue() +
i5.intValue();
System.out.print(i6);
}
}

What is the result of attempting to compile and run the program?
a. Prints: 5
b. Prints: 5.0
c. Compiler Error
d. Runtime Error
e. None of the Above
Answer : c
Can anyone explain how ? Also , his explanation says conversion from float to long a widening conversion and not narrowing conversion. Is that so ?
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Salim-
Even though a float is 32 bits and a long is 64 bits, a float has a bigger RANGE. A long has more precision, but a smaller range. Java does not allow implicit casting of any floating point numbers to any integer type numbers.
The Long constructor(s) take either a long or a String.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always remember this hierarchy,
byte-->short-->|
|-->int-->long-->float-->double
char-->|
You can always use implicit conversion for the direction of the arrow. For the other way you have to do explicit cast or else compiler will comply.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Salim Mohamed:
...his explanation says conversion from float to long a widening conversion and not narrowing conversion. Is that so ?


Salim,
The explanation on my web site for that question is as follows.


Long has two constructors. The first accepts a parameter of type primitive long. The second accepts a parameter of type String. The attempt to pass primitiveFloat to a constructor generates a compiler error because method invocation type conversions do not include implicit narrowing conversions such as the narrowing of a float to a long.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic