Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

I don’t want my class to be inherited by any other class. What should I do?

1015


What is the use of toarray () in java?

1071


How do you control extraneous variables?

916


What is a final class in java?

1020


How many bytes is string in java?

1253


Is oracle charging for java?

1034


What are the different types of java?

1067


Can a lock be acquired on a class in java programming?

991


What is fail first in java?

1205


What happens if we don’t define serial version uid?

1028


You're given a Boolean 2D matrix, can you find the number of islands?

1111


what is optional in java 8?

1062


Give the difference between the println method and sqrt method?

1086


What is data string?

973


Describe what happens when an object is created in java ?

1010