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



solve this is my problem byte a=40,byte b=50 both add value is 90 this is with in range of byte... b..

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

solve this is my problem byte a=40,byte b=50 both add value is 90 this is with in range of byte... b..

Answer / siva

c=(byte)(a+b);

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

What are the differences between this and super keyword?

0 Answers  


Will minecraft java be discontinued?

0 Answers  


what is the use of StringBuffer?

2 Answers   Accenture,


How do you convert an int to a string in java?

0 Answers  


Default layout of Dialog object?

1 Answers  






What is the mapping mechanism used by java to identify IDL language?

0 Answers  


What is string value?

0 Answers  


What is lastindexof in java?

0 Answers  


Is it possible to cast an int value into a byte variable? What would happen if the value of int is larger than byte?

0 Answers  


What are the special characters?

0 Answers  


What is scope & storage allocation of static, local and register variables? Explain with an example.

0 Answers   IBS,


How do you sort arrays in java?

0 Answers  


Categories