| Author | Topic: Primitive conversions | 
| 
	Shreesha Mahishi unregistered  | 
 
posted March 24, 2000 10:33 AM    
         
 Hi, I have two questions concerning primitives conversions. 1. If you have  2.Consider the following code : int i = 0; int j = 20; long l = 22; 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 
  | 
| 
	Howard Stern ranch hand  | 
 
posted March 24, 2000 11:26 AM    
 
             
 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).] 
  | 
| 
	Howard Stern ranch hand  | 
 
posted March 24, 2000 11:31 AM    
 
             
 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).] 
  | 
| 
	Howard Stern ranch hand  | 
 
posted March 24, 2000 11:32 AM    
 
             
 I dont know what is wrong with the reply system it doesn't seem to send everything. 
  | 
| 
	maha anna bartender  | 
 
posted March 24, 2000 11:55 AM    
 
             
 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  
 I am sure you meant this only. I am adding just to make it vivid. regds maha anna 
  | 
| 
	Jim Yingst sheriff  | 
 
posted March 24, 2000 12:04 PM    
 
             
 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. 
  | 
| 
	Howard Stern ranch hand  | 
 
posted March 24, 2000 08:06 PM    
 
             
 Thanks Maha for the point. Thanks Jim for the correction. I was indeed stumped there!! 
  | 
| 
	Shreesha Mahishi unregistered  | 
 
posted March 24, 2000 09:00 PM    
         
 Howard and Maha Anna, thanks for the information. -Shreesha Mahishi 
  | 
  | | |