Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...

Can we add two byte variables and assign the result to a
byte variable ?
b=b1+b2 where b,b1,b2 are byte types

Answers were Sorted based on User's Feedback

Can we add two byte variables and assign the result to a byte variable ? b=b1+b2 where b,b1,b2 ..

Answer / rajesh

Yes,the following is the right way to do so
byte b1=21;byte b2=11;
byte b;
b=(byte)(b1+b2);

 Is This Answer Correct ? 38 Yes 5 No

Can we add two byte variables and assign the result to a byte variable ? b=b1+b2 where b,b1,b2 ..

Answer / m. abhishek

yes, but u require type casting because the the binary
operation in java should atleast returns Integer values

example :
byte a=12;
byte b=23;
// byte c=a+b; //Not OK,bcoz a+b is an int value
int c=(byte)a+b; // OK

 Is This Answer Correct ? 34 Yes 7 No

Can we add two byte variables and assign the result to a byte variable ? b=b1+b2 where b,b1,b2 ..

Answer / ashish ranjan

No We cannot add two byte variables directly. The reason behind is the operator '+' is used to perform integer operation. so the variables are automatically promoted to the integer when we perform any arithmetic operation.

so we need to typecast the variables

b= (byte)(b1 + b2);

 Is This Answer Correct ? 15 Yes 2 No

Can we add two byte variables and assign the result to a byte variable ? b=b1+b2 where b,b1,b2 ..

Answer / guest

yes.But you have to cast that variable before adding.

eg:
byte b=(byte)b1+(byte)b2

 Is This Answer Correct ? 7 Yes 35 No

More Core Java Interview Questions

Why do we need data structure in java?

what is webservices?

What do you mean by exception handling in Java?

Differentiate between class and structure.

Can we convert stringbuilder to string in java?

What is an exception? difference between Checked and Unchecked exception in Java

Why transient variables wont participate in serialization?

Which sorting is best in java?

There are three interfaces A,B & C. A extends B, B extends C, and C extends A.Is it multiple Inheritance? please anybody help me.....

What is indexof in java?

What is difference between this and super keyword?

Why are parameters used in functions?

Categories
• Core Java (6466)
• Advanced Java (552)
• Swing (223)
• EJB (402)
• Servlets (804)
• Struts (701)
• JDBC (771)
• JMS (324)
• SunOne (10)
• J2EE (485)
• Weblogic (694)
• Websphere (573)
• Java Networking (49)
• Hibernate (1009)
• Spring Framework (1486)
• Java J2EE AllOther (116)