• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

datatype doubt

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 35
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thank you steve
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic