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

Is main a function?

0 Answers  


Difference between an argument and a parameter?

10 Answers   Accounting, HCL, IBM, Quinnox,


Name the package that always imported by default?

4 Answers  


Define how does a try statement determine which catch clause should be used to handle an exception?

0 Answers  


Urgent requirement of JAVA/.NET/QA in a CMMI level3 MNC for Pune location.

0 Answers  






How to excute - Interface - Inner class- method can any one tell how to execute/ call this main method public interface abc { static int i=0; void dd(); class a1 { a1() { int j; System.out.println("inside"); }; public static void main(String a1[]) { System.out.println("in interfia"); } } }

1 Answers  


Is a method a procedure?

0 Answers  


What are the major advantages of internal iteration over external iteration?

0 Answers  


What is the use of Getters and Setters method ?

4 Answers  


Implement two stacks using a single array.

0 Answers   Global Logic,


What's the base class of all exception classes?

0 Answers  


What is null data type?

0 Answers  


Categories