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..
Answers were Sorted based on User's Feedback
Answer / 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 |
What is "finally" keyword?
Why main method is called first in java?
How many threads can java run?
Can we sort array in java?
Why does it take so much time to access an applet having swing components the first time?
Can we modify the throws clause of the superclass method while overriding it in the subclass?
What is floor math?
What is the difference between method and constructor ?
What is difference between calling start() and run() method of thread?
What is the difference between error and exception and explain in simple words not whatever is given in the book.
What is a null check?
If an object reference is set to null, will the garbage collector immediately free the memory held by that object?