Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Dan's Wrapper Exam Question

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • 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: 9000
19
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.

 
All of the following truths are shameless lies. But what about this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic