solve this is my problem byte a=40,byte b=50 both add value is 90 this is with in range of byte... byte range is -128to 127....
why this pgm gives error like type mismatch....
package javapgms;
public class byte1 {
public static void main(String args[])
{
byte a=40,b=50;
byte c=a+b;
System.out.println(c);
}
}
note : dont use int k...
a,b,c are in byte range... mind it..
Answer Posted / chaya k
There is a complile time error occurs while adding byte variables a and b together. Because we have to know that when we add byte numbers we get int as a resultant datatype.
Here,both a and b are in byte, then resultant datatype after addition is in int datatype but not in byte.We can't store value from int datatype to value in byte datatype so we do down-typecasting as:
byte c=(byte) a+b;
If you don't want to downcaste you should store resultant value in int datatype only as,
int c=a+b;
| Is This Answer Correct ? | 7 Yes | 1 No |
Post New Answer View All Answers
I don’t want my class to be inherited by any other class. What should I do?
What is the use of toarray () in java?
How do you control extraneous variables?
What is a final class in java?
How many bytes is string in java?
Is oracle charging for java?
What are the different types of java?
Can a lock be acquired on a class in java programming?
What is fail first in java?
What happens if we don’t define serial version uid?
You're given a Boolean 2D matrix, can you find the number of islands?
what is optional in java 8?
Give the difference between the println method and sqrt method?
What is data string?
Describe what happens when an object is created in java ?