Newer
Older
<chapter annotations="slide" version="5.1"
xml:id="sd1_workingWithNumbers_chap"
xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:ns="http://docbook.org/ns/transclusion"
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/ns/docbook">
<title>Working with Numbers</title>
<section xml:id="sd1_numbers_sect_boxAndUnbox">
<title>Boxing and Unboxing</title>
<figure xml:id="sd1_numbers_fig_stack">
<title><classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/util/Stack.html">Stack</classname>
of integer values</title>
<informaltable border="0">
<td valign="top"><programlisting language="java">final Stack<Integer> values = new Stack<>();
values.push(3);
values.push(1);
values.push(10);
while (!values.empty()) {
final int i = values.pop();
System.out.println(i);
}</programlisting></td>
<td valign="top"><screen>10
1
3</screen></td>
</tr>
</informaltable>
</figure>
<figure xml:id="sd1_numbers_fig_collectionProperties">
<title><xref linkend="glo_Java"/> collection features</title>
<itemizedlist>
<listitem>
<para>Supports searching of objects based on:</para>
<itemizedlist>
<listitem>
<para><methodname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html#equals(java.lang.Object)">public
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html">Object</classname>
obj)</para>
</listitem>
<listitem>
<para><methodname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html#hashCode()">public
int hashCode()</methodname></para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Objects only, no primitive types!</para>
</listitem>
</itemizedlist>
</figure>
<figure xml:id="sd1_numbers_fig_stackBehindTheScenes">
<title>Behind the scenes</title>
<informaltable border="0">
<colgroup width="48%"/>
<colgroup width="50%"/>
<tr>
<td valign="top"><programlisting language="java">final Stack<Integer> values =
new Stack<>();
values.push(3);
values.push(1);
values.push(10);
while (!values.empty()) {
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
getClass().getTypeName());
}</programlisting></td>
<td valign="top"><screen>java.lang.Integer
java.lang.Integer
java.lang.Integer</screen></td>
</tr>
</informaltable>
</figure>
<figure xml:id="sd1_numbers_fig_boxingAndUnboxing">
<title><link
xlink:href="https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html">Boxing
and unboxing</link></title>
<informaltable border="0">
<colgroup width="50%"/>
<colgroup width="50%"/>
<tr>
<td valign="top"><programlisting language="none">int iPrimitive <co
linkends="sd1_numbers_fig_boxingAndUnboxing-1"
xml:id="sd1_numbers_fig_boxingAndUnboxing-1-co"/> = 7;
Integer iInteger = <co linkends="sd1_numbers_fig_boxingAndUnboxing-2"
xml:id="sd1_numbers_fig_boxingAndUnboxing-2-co"/>
iPrimitive;
int iPrimitiveFromInteger = <co linkends="sd1_numbers_fig_boxingAndUnboxing-3"
iInteger;</programlisting></td>
<td valign="top"><programlisting language="none">int iPrimitive <co
linkends="sd1_numbers_fig_conventional-1"
xml:id="sd1_numbers_fig_conventional-1-co"/> = 7;
Integer iInteger = <co linkends="sd1_numbers_fig_conventional-2"
xml:id="sd1_numbers_fig_conventional-2-co"/>
Integer.valueOf(iPrimitive);
int iPrimitiveFromInteger = <co linkends="sd1_numbers_fig_conventional-3"
iInteger.intValue();</programlisting></td>
</tr>
</informaltable>
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<informaltable border="1" role="slideExclude">
<colgroup width="50%"/>
<colgroup width="50%"/>
<tr>
<th>Boxing and unboxing</th>
<th><quote>Conventional</quote> <xref linkend="glo_Java"/></th>
</tr>
<tr>
<td valign="top"><calloutlist>
<callout arearefs="sd1_numbers_fig_boxingAndUnboxing-1-co"
xml:id="sd1_numbers_fig_boxingAndUnboxing-1">
<para>Defining a primitive <code language="java">int</code>
value.</para>
</callout>
<callout arearefs="sd1_numbers_fig_boxingAndUnboxing-2-co"
xml:id="sd1_numbers_fig_boxingAndUnboxing-2">
<para>Creating an instance of <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html">Integer</classname>
by means of boxing.</para>
</callout>
<callout arearefs="sd1_numbers_fig_boxingAndUnboxing-3-co"
xml:id="sd1_numbers_fig_boxingAndUnboxing-3">
<para>Assigning an <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html">Integer</classname>'s
value to a primitive <code language="java">int</code> by means
of unboxing.</para>
</callout>
</calloutlist></td>
<td valign="top"><calloutlist>
<callout arearefs="sd1_numbers_fig_conventional-1-co"
xml:id="sd1_numbers_fig_conventional-1">
<para>Defining a primitive <code language="java">int</code>
value.</para>
</callout>
<callout arearefs="sd1_numbers_fig_conventional-2-co"
xml:id="sd1_numbers_fig_conventional-2">
<para>Creating a new instance of <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html">Integer</classname>
using the class method <methodname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html#valueOf-int-">Integer
valueOf(int i)</methodname>.</para>
</callout>
<callout arearefs="sd1_numbers_fig_conventional-3-co"
xml:id="sd1_numbers_fig_conventional-3">
<para>Assigning an <classname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html">Integer</classname>'s
value to a primitive <code language="java">int</code> using
the <methodname
xlink:href="https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html#intValue--">int
intValue()</methodname> instance method.</para>
Loading
Loading full blame...