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 is the difference between HashMap and Hashtable

17 Answers   Value Labs, Virtusa,


Define canvas?

0 Answers  


How many bits are used to represent unicode, ascii, utf-16, and utf-8 characters in java programming?

0 Answers  


Define linked list and its features with signature?

0 Answers  


What is procedure writing?

0 Answers  






Java violates few objected oriented concepts. Can you explain in what way?

3 Answers   Ness Technologies,


What is the full name of java?

0 Answers  


For which statements we use a label?

3 Answers   Wipro,


What are 4 pillers of object orinted programming?

0 Answers  


What is generics in java interview questions?

0 Answers  


What is java algorithm?

0 Answers  


What is a "pure virtual" member function?

0 Answers   Amazon,


Categories