• Post Reply Bookmark Topic Watch Topic
  • New Topic

int and long  RSS feed

 
Greenhorn
Posts: 18
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When line 6 is commented program prints 27,but when line 5 is commented it gives compile time error.Why?? both int and long are greater than byte.then why it occurs with long but not with int.
 
Ranch Hand
Posts: 209
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Compiler complains for loss of precision.You have to downcast it.
byte b=(byte)27L;
 
Shanu Pandey
Greenhorn
Posts: 18
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sagar Shroff wrote:The Compiler complains for loss of precision.You have to downcast it.
byte b=(byte)27L;


I know that i have to downcast it.But this is the thing which i want to know that why downcasting is not required in case of byte b=27 ,as required in case of byte b= 27l,if both int and long are greater than byte.(byte=8 bit, int=32 bit, long=64 bit).
 
Sagar Shroff
Ranch Hand
Posts: 209
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shanu Pandey wrote:

Sagar Shroff wrote:The Compiler complains for loss of precision.You have to downcast it.
byte b=(byte)27L;


I know that i have to downcast it.But this is the thing which i want to know that why downcasting is not required in case of byte b=27 ,as required in case of byte b= 27l,if both int and long are greater than byte.(byte=8 bit, int=32 bit, long=64 bit).



Because it's an implicit cast provided by the compiler and the same rule is available to byte,short,char For eg try this char c=9;
But when you apply something like this byte b=129;.So you have to take care whether it falls in the range in ma eg 129 was out of range of for Byte.
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from Java doc:


byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

 
author
Sheriff
Posts: 23484
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shanu Pandey wrote:

Sagar Shroff wrote:The Compiler complains for loss of precision.You have to downcast it.
byte b=(byte)27L;


I know that i have to downcast it.But this is the thing which i want to know that why downcasting is not required in case of byte b=27 ,as required in case of byte b= 27l,if both int and long are greater than byte.(byte=8 bit, int=32 bit, long=64 bit).



For the first, see section 5.2 of the Java Language Specification -- about 3 paragraphs down. The exact wording is....

In addition, if the expression is a constant expression (§15.28) of type byte, short, char or int :

  • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.


  • For the later, read that section again. Notice that long, float, or double is not listed.

    Henry
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!