• Post Reply Bookmark Topic Watch Topic
  • New Topic

Primitives type  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I am preparing for java OCA examination. I need some advice from experts about primitives types in java. I have the following question

Does java assumes an int value for all integral data types?

byte a = 125; // code compiles . Is value of a 125 is assumed to be int.
byte a = 11111111;// code doesn't compile error (error!! incompatible type conversion from int to byte)

long a = 1111111; // code compiles with L as the number literal is assumed to be int.

but why in the case of byte it doesn't work when we add a value higher than a byte could take.

Now if all the integral values are assumed to be int then how the compiler will do implicit cast as int type is bigger then  byte type?




Could some one please help me make this understand.
 
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you cast a primitive, you are not saying, "Make this value fit even though it's not big enough," you're saying, "Compiler, I know this value will fit even though this primitive can hold values too high for the type I'm casting too."  So for instance, if you do this: ...you're not going to get what you think. 
 
riz shafiq
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thank you for the quick reply. I know we can do explicit casting to convert int to byte.
I would like to know the following
By default, the Java compiler treats all integer literals as of type int. Consider the declaration of a variable of type byte:

byte a = 100;
if the literal value 100 is treated as an int then how java is doing implicit casting from a bigger to smaller type. To my knowledge we need explicit casting (from bigger type to smaller type ) and java only does implicit casting that is (from smaller to bigger types )
then how does it works in the above scenario.
i hope that makes sense
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
riz shafiq wrote:
byte a = 100;
if the literal value 100 is treated as an int then how java is doing implicit casting from a bigger to smaller type. To my knowledge we need explicit casting (from bigger type to smaller type ) and java only does implicit casting that is (from smaller to bigger types )
then how does it works in the above scenario.


Section 5.2 of the Java Language Specification ...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2

Assignment conversions. Has a section for compile time constants (and for certain types).... basically, an integer value, that is a compile time constant, and is determined to fit into a range of a byte, may be assigned to a byte variable.

Henry

 
riz shafiq
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry

thank you for the quick reply. I read the section 5.2 and found the reason.
It means that java does narrowing conversion in the case of byte,short and char.
Is that true?

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.

 
riz shafiq
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
the question has been answered

thank you all
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!