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
What are meta-annotations?
What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. Import java.net.* Versus import java.net.socket)?
What is a java object and java application?
Difference between class#getinstance() and new operator ?
Why heap memory is called heap?
What is class forname?
What is supplier in java?
Difference between collection, collection and collections in java?
what is the purpose of the runtime class?
What is the difference between the boolean & operator and the && operator in java programming?
Can we override private methods?
Difference between a class and an object?
enlist some features of jdk.
What are multiple inheritances?
Can you explain inner class.