• 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

Explicit cast of primitive - some strange things

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All, This one may look easy for some. But at first sight it is confuses. Try the following.
int i=10;
// Following need explicit cast in order to get compiled.
char c=i; byte b=i; short s=i;
//But Following works fine - compiles well.
char c=10; byte b=10; short s=10;
My school of thought would say : As long as the value of RHS is in range of the LHS type, it should not need explicit cast no matter RHS is a variable or value.
Would someone please justify this behaviour of JVM!!!
Another thing - in a mockexam (http://valiveru.tripod.com/java/jvaltest.html) the following declaration is said valid. (which I tested does NOT compile)
int i=10; char c=i;
Are these site authenticated? This site has very tough Qs and added to that couple of wrong answers like above. Testing yourself at these site might be frustrating. Just a breather to concerned ones.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//But Following works fine - compiles well.
char c=10; byte b=10; short s=10;
The compiler is allowed to make decisions like this
to simplify the life of the programmer.
Don't confuse what happens at compile-time with what happens at runtime. If the runtime JVM had to do a range check every time an int value was assigned to a byte, it would slow things down considerably.
Bill
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
int i=10;
// Following need explicit cast in order to get compiled.
char c=i; byte b=i; short s=i;
//But Following works fine - compiles well.
char c=10; byte b=10; short s=10;
Well, try this
final int i=10;
char c=i;byte=i;short=i;
All these assignments work well coz some optimization done by the compilier( Im not very sure what they mean by optimization, but u should be aware abt this behaviour). So it seems that if u assign a final value or a final variable it will work fine.

Another thing - in a mockexam (http://valiveru.tripod.com/java/jvaltest.html) the following declaration is said valid. (which I tested does NOT compile)
int i=10; char c=i;
It will work but with a cast.
Anything wrong?
Thanks
Sanjeet

[This message has been edited by Sanjeet Karamchandani (edited August 19, 2000).]
 
Ranjan Sarangi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill and Sanjeet for drawing line between the compile time and run time checks.
Bill, Currently I'm reading your cram book. It's a great resource. I'll appear the exam shortly. It's a pleasure that you responded to my questoin.
Thanks.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic