Java Reference
In-Depth Information
Next, the plus signs represent the concatenation function, because they appear between numeric values and strings.
System.out.println(doubleTest + " " + intTest);
The JVM converts the numeric to a string and concatenates the two strings. In this case, the value of doubleTest
(increased to 3.0 in the previous println) is converted to a string and concatenated to a space resulting in the text “3.0”.
The second plus sign is also between text (“3.0”) and a numeric value (intTest), so intTest is converted to a string (“4”)
and concatenated to “3.0”, resulting in the text “3.0 4”, which is then displayed in the console. Result:
6.0
3.0 4
We're onto the last group now. Notice in the first statement of this group that there are two post-increments and
one pre-increment.
System.out.println(" " + Math.pow(double Test++, ++intTest) + doubleTest++);
The Math.pow operation will be performed before the concatenations. Inside of Math.pow is one pre-increment
that is performed first, resulting in intTest being set to 4. The value in doubleTest (2) is then raised to the fourth power,
resulting in 16.0. The post-increment is then performed on doubleTest, changing its value to 3.0. You may be confused
that the post-increment is performed before the println. Post increments are done after the owning instruction is
executed. In this case, the post-increment is embedded in the Math.pow instruction. Therefore, once Math.pow
completes, the increment is performed. The second doubleTest post increment is not done until its owning operation
(the println) is performed. Concatenations are done next (from left to right), resulting in a space being concatenated
to the result of the Math.pow function (16.0). The text “16.0” is then concatenated to the value of doubleTest (3.0)
resulting in the text “16.03.0”. Now that the println has completed, doubleTest is incremented to 4.0.
The next println simply prints the value of doubleTest as 4.0 and proves that the first statement really did change
the value of doubleTest to 4.0.
System.out.println(doubleTest);
Result:
16.03.0
4.0
The purpose of this exercise was to impress upon you the complicated nature of combining many numeric and
String operations. Many programmers strive to write compact, dense instructions that use the smallest amount of
source code. The small benefits of source code compactness are usually greatly outweighed by the increased testing
time and errors that this hard-to-understand, complicated code generates. It is best to err on the side of simplicity and
clarity when coding, even if this does result in more source code.
Converting Between Primitive Types
Working with the various numeric primitives is confusing enough; however, converting between primitives and
strings adds yet another layer of complexity. Unfortunately, strings have to be discussed, because GUI components
only work with text. This means any numeric data that you want to display (on a frame, window, etc.) must be
converted to text. In addition, any information retrieved from a GUI component is a string. If you want to perform
mathematical functions on that data, a conversion from string to a numeric primitive must be performed. First, we
will deal with converting between primitives, and then bring strings into the mix.
 
Search WWH ::




Custom Search