Home > Articles > Programming > Java

This chapter is from the book

Numeric Operators

In the last section, we created and initialized some variables. Now let's look at Java's facilities for numeric operations. They will look very familiar to C programmers, but there are some differences. Operators that perform arithmetic or numeric comparison are shown in Table 3.2. The precedence gives the order in which the compiler performs operations, with 1 being the first. You can always use parentheses to control the order in which operations are performed.

Table 3.2 Numeric Operators in Java

Precedence

Operator

Description

1

++

Increment by 1 (or 1.0)

1

--

Decrement by 1 (or 1.0)

1

+

Unary plus

1

-

Unary minus

2

*

Multiplication

2

/

Division

2

%

Modulo

3

+

Addition

3

-

Subtraction

5

<

Less than test

5

>

Greater than test

5

<=

Less than/equal test

5

>=

Greater than/equal test

6

==

Equals test (identical values)

6

!=

Not equals to test

13

op=

op with assignment (+=, -=, *=, and so on)


Order of Evaluation of Operands

When evaluating an expression, Java always evaluates the operand on the left first. This rule can be important if the left operand is a method call or an expression that modifies a variable that appears on the right.

Increment and Decrement

Java follows the C convention with the increment and decrement operators, which directly modify the value in a primitive variable by adding or subtracting 1. When this operator appears in a larger expression, the order in which the modification occurs depends on the position of the operator, as shown in the following code fragment:

1. int x = 5 ;
2. int y = x++ ; // y gets the value 5, before incrementing x
3. int y2 = ++x ; // y2 gets the value 7, after incrementing

When evaluating expressions that involve increment and decrement, keep in mind that expression evaluation is always "left first." For example, consider the following sequence:

1. int a = 2 ;
2. a += ++a ;
3. System.out.println( "value of a= " + a );

This code prints value of a= 5 because the Java first evaluates the left side of += as 2, and then evaluates ++a as 3, and finally carries out the addition and stores the result in a, replacing the value created by ++a. Remember that ++ or -- before the variable indicates "pre" evaluation of the variable and when the operator is after the variable, it indicates "post" evaluation.

CAUTION

It would not be at all unusual for you to have one or more questions in which the order of increment or decrement operations is critical.

Unary + and - Operators

Distinct from the arithmetic add and subtract operators, the unary + and - operators affect a single operand. Unary - changes the sign of a numeric expression to the right of the operator. Unary + has no effect on an expression; it is included for completeness and because some programmers like to use it to emphasize that a number is positive.

Arithmetic Operators

In general, the arithmetic operators +, -, /, and * work as you would expect, but you will need to know the conventions that the compiler uses to convert various primitives before performing operations. As with C, the operator appears between its two operands.

Arithmetic Operators with Assignment

The operators that combine an arithmetic operator with the = assignment operator perform an operation on the contents of the variable on the left side and store the results in the variable. For example, in the following code, line 2 is equivalent to line 3:

1. int x = 5 ;
2. x += 10 ; // x gets 5 + 10
3. x = x + 10 ;

The compiler makes some assumptions when it sees an operator with assignment. For instance, in the following sequence of statements, the compiler does not object to the fact that line 2 adds an int value to a byte because it performs an explicit cast, the equivalent of line 4; however, in line 3, which is the logical equivalent of line 2, it raises an objection:

1. byte b = 0 ;
2. b += 27 ; 
3. b = b + 27 ;
4. b = (byte)(b + 27) ;

Widening Conversions

Widening conversions of a number are those that don't lose information on the overall magnitude. For instance, the integer primitives byte, char, and short can all be converted to an int primitive, and an int primitive can be converted to a long integer without loss of information. You may see this sort of widening conversion referred to as numeric promotion.

An int can be converted to a float primitive, but there may be some loss of precision in the least significant bits. This conversion is carried out according to the Institute of Electrical and Electronics Engineers (IEEE) standard.

When evaluating an arithmetic expression with two operands, the compiler converts primitives by widening according to these rules:

  1. If either is of type double, the other is converted to double.

  2. Otherwise, if either is a float, the other is converted to float.

  3. Otherwise, if either is of type long, the other is converted to long.

  4. Otherwise, both operands are converted to int.

These automatic conversions can have significant consequences, particularly when you are trying to store the results of an expression in a primitive variable that has a smaller capacity than one of the operands. Consider the following code:

1. int a = 2 ;
2. float x = 1.5f ;
3. a = x * a ;

By rule 2, both sides of the expression in line 3 are converted to float. However, the compiler knows that float variables have a much wider range of magnitude than int variables. Therefore, if you try to compile the code, you get an error message. To avoid this error, you have to use a cast.

Conversion with Casting

You can always direct the order and direction of number conversions with specific casts. As an example, consider the following code fragment:

1. float x = 123 ; 
2. byte b = 23 ;
3. float y = x + b ;
4. b = (byte) y ;

In line 3, the compiler converts b to a float before performing the addition. You have to include the specific cast operation to get the compiler to accept line 4 because converting a float to an 8-bit byte involves potential loss of magnitude and precision.

The Modulo Operator

You can think of the % (modulo) operator as yielding the remainder from an implied division of the left operand (dividend) by the right operand (divisor). The result is negative only when the dividend is negative. Note that if the operands are integers, the ArithmeticException can be thrown if the divisor is zero, just as in integer division.

Using % with floating-point primitives produces results similar to the integer operation, but note that the special floating-point values, such as NaN and POSITIVE_INFINITY, can result.

Numeric Comparisons

The numeric comparisons <, >, <=, >=, !=, and == work pretty much as expected with Java primitives. If the operands are of two different types, the compiler promotes one or both according to the rules for arithmetic operators. Remember that the result of a numeric comparison is a boolean primitive.

The <, >, <=, and >= operators are meaningless for objects, but the == and != operators can be used. When used with object references, == results in true only if the references are identical. We return to this subject later in this chapter in the "Testing Object Equality" section because it is very important.

CAUTION

Be sure you master the differences between the == comparison with primitives and with objects. In our experience, this difference has been one of the most frequent sources of errors (on the exam and in programming).

Arithmetic Errors

In general, Java lets you make a variety of arithmetic errors without warning you. If your code conducts operations that overflow the bounds of 32-bit or 64-bit integer arithmetic, that is your problem. Division by zero in integer arithmetic is the only error that produces a runtime exception, namely, an ArithmeticException.

On the other hand, floating-point operations meet the requirements of the IEEE standard for representing values that are out of the normal range. These special values are defined for float primitives as constants in the Float class, as shown in Table 3.3. The string representation is what you get from the Float.toString method. The Double class defines similar constants for double primitive values.

Table 3.3 Special Floating-Point Values

Constant

Interpretation

Corresponding String

Float.MAX_VALUE

The largest number representable

3.4028235E38

Float.MIN_VALUE

The smallest number representable

1.4E-45

Float.NEGATIVE_INFINITY

Negative divided by zero

-Infinity

Float.POSITIVE_INFINITY

Positive divided by zero

Infinity

Float.NaN

Not a number

NaN


Not a Number

The special NaN value is particularly tricky to handle. NaN can result from mathematical functions that are undefined, such as taking the square root of a negative number.

You cannot directly compare the NaN value with anything. You must detect it with the special Float.isNaN or Double.isNaN methods, as in the following example:

1. float x = (float) Math.sqrt( y ) ; // where y may be neg
2. if( x == Float.NaN ) x = 0.0 ; // WRONG, always false
3. if( Float.isNaN( x ) ) x = 0.0 ; 
     // the right way to detect NaN

This example shows the right way (line 3) and one of the many wrong ways (line 2) to detect the NaN value.

Floating-Point Math and strictfp

The strictfp modifier is related to the way floating-point calculations are carried out, as affected by specialized math coprocessors. Recall that float and double primitives use 32 and 64 bits, respectively, to store values. However, some floating-point coprocessors can use internal representations of numbers that use more bits for the intermediate results of calculation. These processors produce results that are more accurate but differ slightly from what you would get if every intermediate calculation result were forced back to a 32- or 64-bit representation.

Normally, you would want to use the most accurate results possible, but this means that a calculation on one Java Virtual Machine (JVM) could produce a result that is slightly different from the same calculation on another JVM. Of course, this is contrary to the spirit of Java. Starting in Java 1.2, the strictfp modifier has been available so you can force floating-point math to reduce all intermediate results to the standard 32- or 64-bit representation, ensuring that calculations produce the same results on all JVMs.

When used as a method modifier, strictfp ensures that all calculations in the method follow the strict calculation rules. When used as a class modifier, strictfp forces all methods in a class to follow strict calculation rules.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020