• Post Reply Bookmark Topic Watch Topic
  • New Topic

datatype doubt  RSS feed

 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


byte a =4;
byte b =5;
bye c = a+b; ---> there is error in this line (type mismatch:cannot convert int into byte)



why i am getting that error??
I am just trying to place a byte variable within byte only know then what's the reason for this error?

please help me out
 
Rishi Dhar
Ranch Hand
Posts: 35
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because remember any arithmatic operation between a variable+variable or variable+literal results in an int literal and an byte variable cannot hold an int literal because of narrowing.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The + operator (and numerical operators in general) produce results of the type int unless the type of the operands prevents it (if one of the operands is a long, float, or double then the result will be long, float, or double respectively. So in your example, you have two ints being added, which produces and int. You then try to assign it to a byte, which you can't do safely. You need to cast the result so that it gets to the correct data type: byte c = (byte) (a + b); [the parens around a + b may be redundant, not sure but I put them there for clarity)]
 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys.


You said that casting the byte will produce the result in byte ,then
byte c =(byte)a+b; --->this statement is enough right but even though you am getting error in this line

I am only getting the output
if i cast like this

byte c = (byte)(a+b);

can you clear my doubt?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I said the parens around a + b may be redundant, but it looks like I was wrong; I wasn't sure about the order of operations. I guess (byte)a + b casts a to a byte then performs the addition between a byte and an int which produces an int. You need to ensure the cast happens after the addition, so you need the parenthesis.
 
Henry Wong
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
Karthikeyan Pandian wrote:
You said that casting the byte will produce the result in byte ,then
byte c =(byte)a+b; --->this statement is enough right but even though you am getting error in this line

I am only getting the output
if i cast like this

byte c = (byte)(a+b);

can you clear my doubt?



Casting has a higher precedence than the addition operator. The extra parenthesis is needed for the expression to end up as a byte (which you have already discovered).

[EDIT: and it looks like Steve beat me to the response... ]

Henry
 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thank you steve
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!