• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
Casting in Java
by (320 points)  
reshown by

1 Answer

0 votes
When you perform arithmetic withvariables or constants of the same type,the result of the operation retains thesame type.  For example, when you take twoints and multiplying together, the resultwill be an int.  When you subtract twodoubles, the result will be a double.However you might need to performmathematical operations on unliketypes.  The process of converting one datatype to another is type conversion.  Javaperform some conversions automaticallyalso called implicit conversion but someconversions must be requested explicitlyby the programmer.  When you performarithmetic with unlike operands ortypes, Java chooses a unifying typefor the result. When two unlike types areused to know expression, the unifying type isone that is higher in precision.  In otherwords, the number that is the lower type,the int or the long, would be convertedto be a higher type - the float or thedouble. Java has no problem adding theprecision in but it does have issueswith taking the precision away and thatwould require the explicit typeconversion.  In the example of the inttimes the double the result of themultiplication is a double. Java willconvert the int to a higher ranking datatype and will use that in themultiplication.However if I try to change gross pay tobe an int,Java will give you a type mismatchsaying that it cannot convert fromdouble to int.  There are some cases wherewe know that we're going to lose theprecision and we're okay with that.In this example, myMoney contains moredetails than an int of myDollars.However we just want the dollar so wecan use an explicit conversion. myMoneyis converted to an int before it is storedinto the integer variable named mydollars.  When the double value isconverted to an int, the decimal placesare lost.However the cast operator does notpermanently alter any variable's datatype so myMoney still contains 134.89and only myDollars would contain the134 at this point.Type casting is forcing one data type tobe used as a value for another.  Toperform type cast, you use a cast operatorwhich is created by placingthe desired result type in parentheses.In this example we have two integersbeing divided and the result should betreated as a double.  If we system out theresult inside of sampleMoney,we'll see that the result is 67.  Howeverif we up the number of kids to three,we should end up with a decimal pointhere because dividing 134 by 3 wouldresult in 44.66666.  Therefore, we needeither myDollars or numberOfKids tobe treated as the double so that we canget the precision that we need.  The waythat we cast to a double is to put thekeyword double inside parentheses.  NowmyDollars we treated as a double and theresult will be as a double as well.  Thecast operator is followed by thevariable or the constant that needs tobe cast.  Always be sure to check yourresults to verify that you're gettingthe data type that you need for yourprogram.