Difference between revisions of "Increment and Decrement Operators"

From CompSciWiki
Jump to: navigation, search
(updated w/ new content)
(headerization)
Line 1: Line 1:
This section will introduce you to the concept of ''Casting'', and explain when and why it is done. In addition, there is a reference table that explains what happens when we cast between the different primative types.
+
{{Template:1010Topic|Chapter_TOC=[[Your First Java Program]]|Introduction=Increment and decrement are concise arithmetic operators that quickly add or subtract one from an integer variable type, such as int and long.|Overview=This section will teach you about the increment and decrement operators, how to use them, the distinction between pre/post-increment and pre/post-decrement, and about special cases to watch out for.}}
  
== Introduction to Casting ==
+
== What Increment and Decrement Operators Are ==
  
When we are working with different primative types in Java, it may be necessary to convert between them while the program is running. Casting occurs within an expression, and has the effect of converting one type of data to another. So the act of casting to a particular type is to convert an existing variable or value to that type. What this conversion entails depends on the type of data being cast. For instance, casting a primitive floating point value (type <code>float</code>) to an integer value (type <code>int</code>) means that the decimal portion of the <code>float</code> will be lost, because it cannot be represented by the <code>int</code>As a general rule, casting will always try to preserve as much of the data as possible.
+
The increment and decrement operators increase or decrease the value of a [[Your_First_Java_Program#Introduction_3]] by 1. These two operators are <code>++</code> and <code>--</code>, respectivelyOne would use an increment or decrement operator to concisely add or substract 1 from a variable.
  
== Explicit Casting ==
+
== Usage ==
  
An explicit cast occurs when you write out the type to be cast to as part of an expression. Explicit casts are easy to notice, because they use the following form:
+
There are two ways to use these operators, either on their own line along with a variable, or as part of a larger expression. Using them as part of a larger expression is more difficult because the operators may change the value before or after the expression is evaluated, depending on their placement.
  
<code>
+
== Basic Usage ==
  float floatingPoint = 30.5;
+
  int integerValue = '''(int)'''floatingPoint;
+
</code>
+
  
As you can see, the type being cast to occurs in brackets before the data that is being cast. In the above example, the <code>float</code> ''floatingPoint'' is cast to type <code>int</code>, and is assigned to the variable ''integerValue''. As mentioned, this has the effect of assigning <code>30</code> to ''integerValue'', because <code>int</code> variables cannot represent the decimal portion, <code>.5</code>.
+
We can use these operators by simply placing them before or after the variable to modify. If the operator is placed before a variable, it is known as a preincrement or predecrement, and if the operator is placed after a variable, it is known as a postincrement or postdecrement.
  
== Implicit Casting ==
+
The following code snippet demonstrates the difference between these four operators:
  
An implicit cast, also known as ''coercion'', is caused when a value must be converted to a different type for some reason. Such reasons may include:
 
* Assignment of a variable of one type to a variable of another, compatible type
 
* Conversion required as part of an expression
 
 
The following example code demonstrates two causes of an implicit cast:
 
 
<pre>
 
<pre>
   float result = 50.0f + 5;
+
   int number = 5;
   double doubleResult = result;
+
 
 +
   ++number; // preincrement, the variable 'number' would be 6 as a result
 +
  number++; // postincrement, the variable 'number' would be 6 as a result
 +
  --number; // predecrement, number = 4
 +
  number--; // postdecrement, number = 4
 
</pre>
 
</pre>
  
First, the integer ''5'' is implicitly cast to type float so it can be added to 50.0f, and then assigned to ''result''. Second, the float ''result'' is assigned to ''doubleResult'', and is implicitly cast to type [[Your_First_Java_Program#Primitive_Type_double|double]].
+
These increment and decrement expressions are really shorthand for:
  
== Casting Between Primative Types ==
+
<pre>
 +
  number = number + 1; // longer way of writing number++ or ++number
 +
  number = number - 1; // longer way of writing number-- or --number
 +
</pre>
  
Since each of the primative types represent different information, it is necessary to describe these types in more detail, including the possible values each can contain, and the effects of casting between these values.
+
Note that the regular [[Your_First_Java_Program#Rules_for_Evaluating_Expressions|rules for arithmetic]] in Java apply here, so be careful not to unintentionally increment or decrement outside the value of a variable to outside of its range.
  
The below table gives the maximum and minimum possible values of the different primative types. This is important information to have on hand, because if you cast to a type with a value outside the type's range, the result will be meaningless.
+
== Within an Expression ==
  
{|border="1"
+
These increment and decrement operators can occur as part of a larger expression as well. One might write the operator this way for conciseness, that is, to avoid writing the increment or decrement on its own line. In this case, the meaning of the expression actually changes depending on whether the operator is placed before or after a variable name. Until you are comfortable using these operators, you should not attempt to use them as part of larger expressions.
!type!!minimum!!maximum
+
|-
+
  |int||-2147483648 (-2<sup>31</sup>)||2147483647 (2<sup>31</sup> - 1)
+
|-
+
  |long||-9223372036854775808L (-2<sup>63</sup>)||9223372036854775807L (2<sup>63</sup> - 1)
+
|-
+
  |double||4.9E-324 (4.9x10<sup>-324</sup>)||1.7976931348623157E308 (1.7976931348623157x10<sup>308</sup>)
+
|-
+
  |float||1.4E-45f (1.4x10<sup>-45</sup>)||3.4028235E38f (3.4028235x10<sup>38</sup>)
+
|}
+
  
Note that the first value given in the minimum/maximum columns is how the value would appear in your code, and the second bracketed value is the actual value.  
+
When a preincrement is done, the variable is changed '''before''' it is used in the expression. Oppositely, when a postincrement is done the variable is changed '''after''' the expression is evaluated.
  
Given the above information, the following table describes the effects of casting between the different primative types. The legend below the table explains each case.
+
The following example illustrates the difference between pre- and post- increment.
  
{|border="1"
+
<pre>
!from\to!!int!!long!!float!!double
+
  int number = 3;
|-
+
  int value = (++number) * 2;
!int
+
   // value = 8, number = 4
  |OK||OK||2||OK
+
|-
+
!long
+
   |1||OK||2||2
+
|-
+
!float
+
  |3||3||OK||OK
+
|-
+
!double
+
  |3||3||1||OK
+
|}
+
  
'''OK''': All information is preserved in the cast.
+
  int number = 3;
 +
  int value = (number++) * 2;
 +
  //value = 6, number = 4 
 +
</pre>
  
'''1''': There is a loss of information because the type being cast to cannot represent the full range of values of the original type. For example, a <code>long</code> can hold a much higher value than a <code>int</code>, so if a <code>long</code>is cast to type <code>int</code> and is holding a value an int cannot, the result will be meaningless.
+
As you can see, whether or not the modified value of ''number'' is used as part of the expression in which it occurs depends on which side of the variable the <code>++</code>, and likewise <code>--</code>, occur.
  
'''2''': Information may be lost since the type being cast to has less significant digits than the type being cast from.
+
== Precautions ==
  
'''3''': Fractional values represented by <code>float</code>s or <code>double</code>s is lost when casting to an integer type. Also, the range of values a <code>float</code> or <code>double</code> can represent is much larger than that of an <code>int</code> or <code>long</code>, so if value lies within this larger range, the result of the cast will be meaningless.
+
Note that some expressions have unclear meaning, such as <code>number = number++</code> or <code>number = ++number</code>. Does the variable ''number'' receive its incremented value, or is number incremented after the assignment has taken place? It is best to avoid assignment to a varaible when that variable is incremented or decremented as part of the assignment's expression.

Revision as of 15:33, 20 March 2007

COMP 1010 Home > Your First Java Program


Introduction

Increment and decrement are concise arithmetic operators that quickly add or subtract one from an integer variable type, such as int and long.

   

{{{Body}}}

What Increment and Decrement Operators Are

The increment and decrement operators increase or decrease the value of a Your_First_Java_Program#Introduction_3 by 1. These two operators are ++ and --, respectively. One would use an increment or decrement operator to concisely add or substract 1 from a variable.

Usage

There are two ways to use these operators, either on their own line along with a variable, or as part of a larger expression. Using them as part of a larger expression is more difficult because the operators may change the value before or after the expression is evaluated, depending on their placement.

Basic Usage

We can use these operators by simply placing them before or after the variable to modify. If the operator is placed before a variable, it is known as a preincrement or predecrement, and if the operator is placed after a variable, it is known as a postincrement or postdecrement.

The following code snippet demonstrates the difference between these four operators:

  int number = 5;

  ++number; // preincrement, the variable 'number' would be 6 as a result
  number++; // postincrement, the variable 'number' would be 6 as a result
  --number; // predecrement, number = 4
  number--; // postdecrement, number = 4

These increment and decrement expressions are really shorthand for:

  number = number + 1; // longer way of writing number++ or ++number
  number = number - 1; // longer way of writing number-- or --number

Note that the regular rules for arithmetic in Java apply here, so be careful not to unintentionally increment or decrement outside the value of a variable to outside of its range.

Within an Expression

These increment and decrement operators can occur as part of a larger expression as well. One might write the operator this way for conciseness, that is, to avoid writing the increment or decrement on its own line. In this case, the meaning of the expression actually changes depending on whether the operator is placed before or after a variable name. Until you are comfortable using these operators, you should not attempt to use them as part of larger expressions.

When a preincrement is done, the variable is changed before it is used in the expression. Oppositely, when a postincrement is done the variable is changed after the expression is evaluated.

The following example illustrates the difference between pre- and post- increment.

  int number = 3;
  int value = (++number) * 2; 
  // value = 8, number = 4

  int number = 3;
  int value = (number++) * 2; 
  //value = 6, number = 4  

As you can see, whether or not the modified value of number is used as part of the expression in which it occurs depends on which side of the variable the ++, and likewise --, occur.

Precautions

Note that some expressions have unclear meaning, such as number = number++ or number = ++number. Does the variable number receive its incremented value, or is number incremented after the assignment has taken place? It is best to avoid assignment to a varaible when that variable is incremented or decremented as part of the assignment's expression.