• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

int and long

 
Greenhorn
Posts: 18
Tomcat Server Java
  • 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
Tomcat Server Java
  • 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
Tomcat Server Java
  • 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
Tomcat Server Java
  • 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 Windows XP Java
  • 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
Posts: 23843
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
     
    Paper jam tastes about as you would expect. Try some on this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!