• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Fundamentals  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please give me a reason for these things

byte b1 = 10, b2 = 20 ;
short s = b1 + b2 ; //line 2

final int x = 100;
byte b3 = x; // line 4

final long l = 100;
byte b5 = l ; //line 6

Why does line 2 gives an error?
Also, Why does line 6 gives an error while line 4 does not give any ?
 
Marshal
Posts: 56606
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always tell us what the question is about.


Look at the return type of the + operator, which you will find in the Java Language Specification.
Work out why the first assignment to a byte worked; the same explanation can be applied to line 6 where your assignment made it past the compiler.
 
Campbell Ritchie
Marshal
Posts: 56606
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but I would appear to have totally misunderstood the second part of the question.
 
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lahiru Chandrarathna wrote:Please give me a reason for these things

byte b1 = 10, b2 = 20 ;
short s = b1 + b2 ; //line 2

final int x = 100;
byte b3 = x; // line 4

final long l = 100;
byte b5 = l ; //line 6

Why does line 2 gives an error?
Also, Why does line 6 gives an error while line 4 does not give any ?

Consider the following analogy;
byte is the smallest box
short is another box bigger than a byte but smaller than an integer or long boxes
long is another box bigger than short and byte.

Now the lines that are giving errors would be saying "Possible loss of Precision" You can learn a lot from these errors itself provided if you look.

Now there are two types of conversion
First is called widening conversion- in which you place the contents of smaller box (byte) into a bigger box(like int). This type of conversion is implicit ie it happens automatically and you dont have to write the code for it.
Now you would be wondering why Line 4 does not give an error?
See, a byte range is from 0 to 127, so if
its ok no worries, but if you will get a compile time error because its an issue of overflow.
Also you have to remember, an expression involving anything int-sized or smaller is always an int.
So based on this concept if we look at the expression final int x=100 and then assign this value to a byte the compiler will not throw any errors because a byte can accomodate the value 100 but god forbid if you have an expression like final int x=128 and then try assigning it to a byte, the compiler would be red faced and throw error "Possible loss of Precision"
The second type is called narrowing conversion- in which you put the contents of a bigger box say long into a smaller box say byte. This is also known as explicit conversion, why explicit because if you dont want the JVM compiler cry foul (read throw compile time error) than you must cast it therefore line 6 is an example of explicit conversion. With this said, i leave this on you to figure out how to solve the problem. You would find your solace in reading about "Primitive Casting". Hope this helps.
Cheers
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!