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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is dot operator?

792


How will you serialize a singleton class without violating singleton pattern?

1748


Does variable declaration allocate memory?

789


Can we have multiple classes in single file ?

819


Compare java and python.

762


How do you find the maximum number from an array without comparing and sorting?

872


What is the public field modifier?

765


What are three ways in which a thread can enter the waiting state in java programming?

908


What is the use of optional ?

793


Explain some best practices you would apply while using collection in java?

811


What are constants and how to create constants in java?

752


What is the disadvantage of java?

717


What is the difference between a method and a procedure?

759


When does an object becomes eligible for garbage collection in java?

814


How to find the index of the largest number in an arraylist java?

724