What is arithmetic promotion assignment and casting, JAVA Programming

Assignment Help:

What is Arithmetic promotion assignment and casting?

In an assignment statement, i.e. if there's an equals sign, Java compares the type of the left hand side to the at last type of the right hand side. It won't change the type of the left hand side, but it will check to make sure in which the value it has (double, float, int or long) on the right hand side can fit in the type on the left hand side. Anything can fit in a double. Anything except a double could fit in a float. Any integral type can fit in a long, but a float or a double can't, and ints, shorts, and bytes can fit inside ints. If the right hand side can fit inside the left hand side, the assignment takes place along with no additional ado.

Assigning long values to int variables or double values to float variables can be equally troublesome. In fact it's so troublesome the compiler won't let you do it unless you tell it you actually mean it with a cast. When it's essential to force a value within a particular type, use a cast. To cast a variable or a literal or an expression to a different data type just precede it with the kinds in parentheses. For example:

int i = (int) (9.0/4.0);

A cast lets the compiler know in which you're serious about the conversion you plan to make.

While a value is cast down before assignment, series of operations takes place to chop the right hand side down to size. For a conversion among a floating point number and an int or a long, the fractional part of the floating point number is truncated (rounded toward zero). This generates an integer. If the integer is small sufficient to fit in the left hand side, the assignment is completed. Instead if the number is too large, then the integer is set to the largest probable value of its type. If the floating point number is too little the integer is set to the smallest probable value of its type.

This can be a nasty bug in your code. It can also be hard to search since everything might work perfectly 99 times out of a hundred and only on rare occasions will the rounding become a problem. Therefore while it does there will be no warning or error message. You required being extremely careful when assigning floating point values to integer types.


Related Discussions:- What is arithmetic promotion assignment and casting

Write a xhtml document with java script, Write an XHTML document that uses ...

Write an XHTML document that uses JavaScript code to do the following.  The user is first prompted for a word and then for a possible prefix for that word.  If the prefix is not an

Program implement class which have main string method, Implement the Money ...

Implement the Money class discussed in class. This class should represent a dollar and cents amount with 0-99 cents and the cents being the same sign as the dollars. The class shou

Create an applet for grid layout, Take the Student Choice GUI you created i...

Take the Student Choice GUI you created in Assignment and convert it to an applet.  Because standalone GUI applications and applets are so similar, a lot of the code should be reus

Java error handling mechanism, Errors: When a dynamic linking failure or ...

Errors: When a dynamic linking failure or some other "hard" failure in the virtual machine happens, the virtual machine gives an Error. Typical Java programs could not find Error

Difference b/w deep cloning and shallow cloning of object, The default natu...

The default nature of an object's clone() function automatically provides a shallow copy. So to need a deep copy the classes have to be edited or adjusted.   Shallow co

Explain the term naming packages in details, Explain the term Naming Packag...

Explain the term Naming Packages in details? As you saw previously name space conflicts arise if two pieces of code declare the similar name. Java remains track of what belongs

How to retrieve warnings, SQLWarning objects are a subclass of SQLException...

SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the implementation of an application, as exceptions do; they easily

What is preferred organizational structure for organization, What is the pr...

What is the preferred organizational structure for the organization? Preferred Organizational Structure for organization is hierarchal. Where there is various departments inter

Describe validate() and reset() methods, Validate() : Used to validate prop...

Validate() : Used to validate properties after they have been populated; known as before FormBean is handed to Action. Returns a collection of ActionError as ActionErrors. Followin

What is action class, The Action Class is type of the Model and is a wrappe...

The Action Class is type of the Model and is a wrapper around the business logic. The purpose of Action Class is to change the HttpServletRequest to the business logic. To use the

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd