• 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

Doubt in Numeric conversion(Khalid Mughal)

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the example in khalid mughal(Pg47 Old book)
//Narrowing primitive conversions involving int variables
int i=-20;
final int j=20;
final int k=i;
byte b1=j;//final value of j in range. no cast required
byte b2=(byte)i; //Value of i not determinable.Cast Required
byte b3=(byte)k; //final value of k not determinable.cast required

My doubt is why in the world is i and k not determinable, so that a cast is required
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thomas,
According to my knowledge: we will do primitive casting under following situations:
_______________________________________________________________________
Implicit conversion occurs in the following scenarion:
1] byte --> short --> int --> long --> float --> double.
Means for exazmple we can assign byte to short or byte to int like that.
Implicit conversion takes place when you follow above sequence.
_______________________________________________________________________

******************************************************************
Explict convertion(i.e. casting)is required for below scenario:
double-->float-->long-->int-->short-->byte(i.e. in opposite direction)
If you are trying to assign in the opposite direction i.e. assigning int to short or int --> byte then explicit casting is required.
*******************************************************************
Bye.
Krish.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thomas

int i=-20;
final int j=20;
final int k=i;

here,
j is a compile time constant, because we specified it as final.
but though k is final, it is getting the value from another variable i(i.e not a compile time constant). k gets the value only at runtime.

byte b1=j;//final value of j in range. no cast required
byte b2=(byte)i; //Value of i not determinable.Cast Required
byte b3=(byte)k; //final value of k not determinable.cast required

In b1 u are assigning the compile time constant. So no problem.
In b2 u are assining int value, u can't assign the int value directly to the byte, it needs casting.
In b3 u are assigning runtime value so u need casting.

Hope u understood.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jacob,

By Jacob:

My doubt is B]why in the world is i and k not determinable[/B], so that a cast is required



see the reason is dam simple....

1)its undterminable as its not final....
2)if a variable is decalred as final, its resolved at compiler at compile-time only, so if its in range of the type to which assigned, then we dont require a explicit cast (this is true only for byte, short, char, int)

hope this helps....

if not feel free to get back

thanx
amit
 
thomas jacob
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody that clears my doubt
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so is this conculsion true ?

casting is not required if you are casting compile time int constant to short/byte etc
compile time constant means that it should be declared final with assining litteral value..
and only complitime integer constants are allowed to cast implicitly to norrow primitive i.e byte,short,char ?
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any comments ?? pls

regards
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. I have a question on this one. Suppose if I declare
final int j = 20000; // instead 20

then the compiler gives error message as possible loss of precision. Now the compiler knows the value of final variable j, so it gives the error.

Actually, final variable can be assigned before the constructor finishes. So, if I assign large value in the constructor, will I get compiler error?
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic