• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

doubt in Expressions

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we add 2 byte variable and assigned it to another byte variable means it gives error. but if we add two numbers(the added value is less than 127)
and assigned to one byte variable means it doesnt give error why?

example code
byte a=2;
byte b=5;
byte c= a+b;


here it gives the error (type mismatch)

if we give

byte c=2+5;

this line doest give error. I dont know the reason. Pls help me

Thanks in advance
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sankar,
Adding two bytes automatically promotes them to ints because the virtual machine doesn't support these operations on any type narrower than an int.
Integers are for arithmetic and bytes are for memory manipulation.
We might choose to ban arithmetic operations like addition and subtraction on bytes.
You can check Here also

[ October 03, 2007: Message edited by: Sheikh Sadiruddin ]
[ October 03, 2007: Message edited by: Sheikh Sadiruddin ]
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sankar,
The result of any expression which involves either an int-size or smaller always results into an int. That implies they will be converted to an int. So as of here when you are adding as byte c= a+b;. The result of a+b gives an int where as c is a byte.So it will give a type mismatch error.
Whereas in second case where you are giving byte c=2+5;.
The value of 2+5 is again an int but compiler automatically narrows down it into a byte by implicitly casting (This is true for any integer literal i.e any int value like 5,7,23...)
Thus byte c= 2+5; is equivalent to byte c= byte (2+5);

This is true for char and short too as both are shorter than int, though here it was for byte
[ October 03, 2007: Message edited by: pranav bhatt ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sankar wrote:

... but if we add two numbers(the added value is less than 127)
and assigned to one byte variable means it doesnt give error why?



The answer is hidden in the part of your question in brackets.
Because "numbers" (int literals) are always compile time constants and the compiler can check if the result fits into your byte.


Bu.
[ October 03, 2007: Message edited by: Burkhard Hassel ]
 
It was the best of times. It was the worst of times. It was a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic