• 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

Primitive conversions

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have two questions concerning primitives conversions.
1. If you have
float f = 3.4;
the compiler will complain since 3.4 will default to a double and you cannot assign a double to a float without an explicit cast.However, if you have
byte b = 3;
I cant see why the compiler does not issue an error - I was given to understand that 3 (or any integer literal) would default to an int - in that case, how can it be assigned to a byte without an explicit cast ?
2.Consider the following code :
int i = 0; int j = 20; long l = 22;
i = j << l;
I cant understand how the compiler forgives this. I thought j would be promoted to a long and the expression would be evaluated to a long. In that case, how can it be assigned to an int ? And why does the compiler complain when the + is used instead of << - after all, they are both binary operands !!!
- Shreesha Mahishi
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Regarding your first question
1. When an integer is assigned to a byte, char or short the compiler does an implicit narrowing conversion from int.
So byte b=1; will take 1 as a default int and convert to byte during the assignment
Your second question:
2. Refer the JLS section 15.18 Shift Operators. The compiler does not perform any binary promotion for shift operators but the unary promotion is performed on each operator individually. Thus i << l will not convert i to long if it is an int but it will convert i to int separately had it been a byte instead of an int.
[This message has been edited by Jim Yingst (edited March 24, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the last portio of the reply.
In case of i << l the i is not promoted to a long (Refer to section 15.18 Shift Operators in the JLS, the binary promotion is not perform with the shift operators).
However had it ("i") been a byte it would have promoted "i" to an int separately.
[This message has been edited by Jim Yingst (edited March 24, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont know what is wrong with the reply system it doesn't seem to send everything.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howard,
Your reply needs a small correction .( )
When an integer is assigned to a byte, char or short the compiler does an implicit narrowing conversion from int.
It is not integer. It is compile time integer constant
Because
<pre>
byte b = 1; //is ok
int i=1;
byte b = i; // is not ok
</pre>
I am sure you meant this only. I am adding just to make it vivid.
regds
maha anna
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howard- I fixed the problem with your posts. Whenever you type < or << in this forum, you need to put a space immediately afterwards so that everyone's web browsers will be able to tell that you are not making an HTML tag (which is not displayed but interpreted instead) - rather, you're writing a symbol which should be written literally.
Anyway, it's good style to put spaces around binary operators. Paul and Sun say so.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maha for the point.
Thanks Jim for the correction. I was indeed stumped there!!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howard and Maha Anna, thanks for the information.
-Shreesha Mahishi
reply
    Bookmark Topic Watch Topic
  • New Topic