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

What is ascii code?

1154


What is %02d?

1037


How many bits is a 64 bit byte?

1000


Explain the selection sort algorithm and state its time complexity?

963


When can an object reference be cast to an interface reference in java programming?

955


What does regex mean?

952


What is data member in java?

955


How many types of exception can occur in a java program?

925


Can we use catch statement for checked exceptions when there is no chance of raising exception in our code?

981


Can a constructor be protected?

1029


What do you understand by casting in java language?

978


What is method with example?

1015


Can there be an abstract method without an abstract class?

888


What is instance means in java?

992


What is java in simple terms?

948