-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathjava-pitfalls-language-syntax.html
More file actions
287 lines (246 loc) · 84.2 KB
/
java-pitfalls-language-syntax.html
File metadata and controls
287 lines (246 loc) · 84.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Java - Java Pitfalls - Language syntax</title>
<meta name="generator" content="VuePress 1.8.2">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="description" content="Pitfall - Missing a ‘break’ in a 'switch' case, Pitfall - Leaving out braces: the dangling if and dangling else problems, Pitfall - Declaring classes with the same names as standard classes, Pitfall - Ignoring method visibility, Pitfall - Octal literals, Pitfall - Using '==' to test a boolean, Pitfall - Misplaced semicolons and missing braces, Pitfall - Wildcard imports can make your code fragile, Pitfall: Using 'assert' for argument or user input validation, Pitfall - Overloading instead of overriding, Pitfall of Auto-Unboxing Null Objects into Primitives">
<meta property="og:site_name" content="DevTut">
<meta property="og:title" content="Java - Java Pitfalls - Language syntax">
<meta property="og:description" content="Pitfall - Missing a ‘break’ in a 'switch' case, Pitfall - Leaving out braces: the dangling if and dangling else problems, Pitfall - Declaring classes with the same names as standard classes, Pitfall - Ignoring method visibility, Pitfall - Octal literals, Pitfall - Using '==' to test a boolean, Pitfall - Misplaced semicolons and missing braces, Pitfall - Wildcard imports can make your code fragile, Pitfall: Using 'assert' for argument or user input validation, Pitfall - Overloading instead of overriding, Pitfall of Auto-Unboxing Null Objects into Primitives">
<meta property="og:type" content="article">
<meta property="og:url" content="/java/java-pitfalls-language-syntax.html">
<meta property="og:image" content="/logo.png">
<meta name="twitter:title" content="Java - Java Pitfalls - Language syntax">
<meta name="twitter:description" content="Pitfall - Missing a ‘break’ in a 'switch' case, Pitfall - Leaving out braces: the dangling if and dangling else problems, Pitfall - Declaring classes with the same names as standard classes, Pitfall - Ignoring method visibility, Pitfall - Octal literals, Pitfall - Using '==' to test a boolean, Pitfall - Misplaced semicolons and missing braces, Pitfall - Wildcard imports can make your code fragile, Pitfall: Using 'assert' for argument or user input validation, Pitfall - Overloading instead of overriding, Pitfall of Auto-Unboxing Null Objects into Primitives">
<meta name="twitter:url" content="/java/java-pitfalls-language-syntax.html">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="/logo.png">
<meta name="theme-color" content="#ffffff">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="msapplication-TileImage" content="/mstile-150x150.png">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="google-site-verification" content="76_rKXgwMVIjd-axJC_1zPV9OS4mEjvtgjYOWVkAdnQ">
<link rel="preload" href="/assets/css/0.styles.60619e34.css" as="style"><link rel="preload" href="/assets/js/app.1779e102.js" as="script"><link rel="preload" href="/assets/js/3.2cfa8016.js" as="script"><link rel="preload" href="/assets/js/1597.ab8ff6ef.js" as="script">
<link rel="stylesheet" href="/assets/css/0.styles.60619e34.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">DevTut</span></a> <div class="links"><form id="search-form" role="search" class="algolia-search-wrapper search-box"><input id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Java</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/java/" aria-current="page" class="sidebar-link">Disclaimer</a></li><li><a href="/java/java-editions-versions-releases-and-distributions.html" class="sidebar-link">Java Editions, Versions, Releases and Distributions</a></li><li><a href="/java/installing-java-standard-edition.html" class="sidebar-link">Installing Java (Standard Edition)</a></li><li><a href="/java/getting-started-with-java-language.html" class="sidebar-link">Getting started with Java Language</a></li><li><a href="/java/type-conversion.html" class="sidebar-link">Type Conversion</a></li><li><a href="/java/getters-and-setters.html" class="sidebar-link">Getters and Setters</a></li><li><a href="/java/reference-data-types.html" class="sidebar-link">Reference Data Types</a></li><li><a href="/java/java-compiler-javac.html" class="sidebar-link">Java Compiler - 'javac'</a></li><li><a href="/java/documenting-java-code.html" class="sidebar-link">Documenting Java Code</a></li><li><a href="/java/command-line-argument-processing.html" class="sidebar-link">Command line Argument Processing</a></li><li><a href="/java/the-java-command-java-and-javaw.html" class="sidebar-link">The Java Command - 'java' and 'javaw'</a></li><li><a href="/java/literals.html" class="sidebar-link">Literals</a></li><li><a href="/java/primitive-data-types.html" class="sidebar-link">Primitive Data Types</a></li><li><a href="/java/strings.html" class="sidebar-link">Strings</a></li><li><a href="/java/stringbuffer.html" class="sidebar-link">StringBuffer</a></li><li><a href="/java/stringbuilder.html" class="sidebar-link">StringBuilder</a></li><li><a href="/java/string-tokenizer.html" class="sidebar-link">String Tokenizer</a></li><li><a href="/java/splitting-a-string-into-fixed-length-parts.html" class="sidebar-link">Splitting a string into fixed length parts</a></li><li><a href="/java/date-class.html" class="sidebar-link">Date Class</a></li><li><a href="/java/dates-and-time-java-time.html" class="sidebar-link">Dates and Time (java.time.*)</a></li><li><a href="/java/localtime.html" class="sidebar-link">LocalTime</a></li><li><a href="/java/bigdecimal.html" class="sidebar-link">BigDecimal</a></li><li><a href="/java/biginteger.html" class="sidebar-link">BigInteger</a></li><li><a href="/java/numberformat.html" class="sidebar-link">NumberFormat</a></li><li><a href="/java/bit-manipulation.html" class="sidebar-link">Bit Manipulation</a></li><li><a href="/java/arrays.html" class="sidebar-link">Arrays</a></li><li><a href="/java/collections.html" class="sidebar-link">Collections</a></li><li><a href="/java/lists.html" class="sidebar-link">Lists</a></li><li><a href="/java/sets.html" class="sidebar-link">Sets</a></li><li><a href="/java/list-vs-set.html" class="sidebar-link">List vs SET</a></li><li><a href="/java/maps.html" class="sidebar-link">Maps</a></li><li><a href="/java/linkedhashmap.html" class="sidebar-link">LinkedHashMap</a></li><li><a href="/java/weakhashmap.html" class="sidebar-link">WeakHashMap</a></li><li><a href="/java/sortedmap.html" class="sidebar-link">SortedMap</a></li><li><a href="/java/treemap-and-treeset.html" class="sidebar-link">TreeMap and TreeSet</a></li><li><a href="/java/queues-and-deques.html" class="sidebar-link">Queues and Deques</a></li><li><a href="/java/dequeue-interface.html" class="sidebar-link">Dequeue Interface</a></li><li><a href="/java/enums.html" class="sidebar-link">Enums</a></li><li><a href="/java/enum-map.html" class="sidebar-link">Enum Map</a></li><li><a href="/java/enumset-class.html" class="sidebar-link">EnumSet class</a></li><li><a href="/java/enum-starting-with-number.html" class="sidebar-link">Enum starting with number</a></li><li><a href="/java/hashtable.html" class="sidebar-link">Hashtable</a></li><li><a href="/java/operators.html" class="sidebar-link">Operators</a></li><li><a href="/java/constructors.html" class="sidebar-link">Constructors</a></li><li><a href="/java/object-class-methods-and-constructor.html" class="sidebar-link">Object Class Methods and Constructor</a></li><li><a href="/java/annotations.html" class="sidebar-link">Annotations</a></li><li><a href="/java/immutable-class.html" class="sidebar-link">Immutable Class</a></li><li><a href="/java/immutable-objects.html" class="sidebar-link">Immutable Objects</a></li><li><a href="/java/visibility-controlling-access-to-members-of-a-class.html" class="sidebar-link">Visibility (controlling access to members of a class)</a></li><li><a href="/java/generics.html" class="sidebar-link">Generics</a></li><li><a href="/java/classes-and-objects.html" class="sidebar-link">Classes and Objects</a></li><li><a href="/java/local-inner-class.html" class="sidebar-link">Local Inner Class</a></li><li><a href="/java/nested-and-inner-classes.html" class="sidebar-link">Nested and Inner Classes</a></li><li><a href="/java/the-java-util-objects-class.html" class="sidebar-link">The java.util.Objects Class</a></li><li><a href="/java/default-methods.html" class="sidebar-link">Default Methods</a></li><li><a href="/java/packages.html" class="sidebar-link">Packages</a></li><li><a href="/java/inheritance.html" class="sidebar-link">Inheritance</a></li><li><a href="/java/reference-types.html" class="sidebar-link">Reference Types</a></li><li><a href="/java/console-i-o.html" class="sidebar-link">Console I/O</a></li><li><a href="/java/streams.html" class="sidebar-link">Streams</a></li><li><a href="/java/inputstreams-and-outputstreams.html" class="sidebar-link">InputStreams and OutputStreams</a></li><li><a href="/java/readers-and-writers.html" class="sidebar-link">Readers and Writers</a></li><li><a href="/java/preferences.html" class="sidebar-link">Preferences</a></li><li><a href="/java/collection-factory-methods.html" class="sidebar-link">Collection Factory Methods</a></li><li><a href="/java/alternative-collections.html" class="sidebar-link">Alternative Collections</a></li><li><a href="/java/concurrent-collections.html" class="sidebar-link">Concurrent Collections</a></li><li><a href="/java/choosing-collections.html" class="sidebar-link">Choosing Collections</a></li><li><a href="/java/super-keyword.html" class="sidebar-link">super keyword</a></li><li><a href="/java/serialization.html" class="sidebar-link">Serialization</a></li><li><a href="/java/optional.html" class="sidebar-link">Optional</a></li><li><a href="/java/object-references.html" class="sidebar-link">Object References</a></li><li><a href="/java/exceptions-and-exception-handling.html" class="sidebar-link">Exceptions and exception handling</a></li><li><a href="/java/calendar-and-its-subclasses.html" class="sidebar-link">Calendar and its Subclasses</a></li><li><a href="/java/using-the-static-keyword.html" class="sidebar-link">Using the static keyword</a></li><li><a href="/java/properties-class.html" class="sidebar-link">Properties Class</a></li><li><a href="/java/lambda-expressions.html" class="sidebar-link">Lambda Expressions</a></li><li><a href="/java/basic-control-structures.html" class="sidebar-link">Basic Control Structures</a></li><li><a href="/java/bufferedwriter.html" class="sidebar-link">BufferedWriter</a></li><li><a href="/java/new-file-i-o.html" class="sidebar-link">New File I/O</a></li><li><a href="/java/file-i-o.html" class="sidebar-link">File I/O</a></li><li><a href="/java/scanner.html" class="sidebar-link">Scanner</a></li><li><a href="/java/interfaces.html" class="sidebar-link">Interfaces</a></li><li><a href="/java/regular-expressions.html" class="sidebar-link">Regular Expressions</a></li><li><a href="/java/comparable-and-comparator.html" class="sidebar-link">Comparable and Comparator</a></li><li><a href="/java/java-floating-point-operations.html" class="sidebar-link">Java Floating Point Operations</a></li><li><a href="/java/currency-and-money.html" class="sidebar-link">Currency and Money</a></li><li><a href="/java/object-cloning.html" class="sidebar-link">Object Cloning</a></li><li><a href="/java/recursion.html" class="sidebar-link">Recursion</a></li><li><a href="/java/converting-to-and-from-strings.html" class="sidebar-link">Converting to and from Strings</a></li><li><a href="/java/random-number-generation.html" class="sidebar-link">Random Number Generation</a></li><li><a href="/java/singletons.html" class="sidebar-link">Singletons</a></li><li><a href="/java/autoboxing.html" class="sidebar-link">Autoboxing</a></li><li><a href="/java/2d-graphics-in-java.html" class="sidebar-link">2D Graphics in Java</a></li><li><a href="/java/jaxb.html" class="sidebar-link">JAXB</a></li><li><a href="/java/class-java-reflection.html" class="sidebar-link">Class - Java Reflection</a></li><li><a href="/java/networking.html" class="sidebar-link">Networking</a></li><li><a href="/java/nio-networking.html" class="sidebar-link">NIO - Networking</a></li><li><a href="/java/httpurlconnection.html" class="sidebar-link">HttpURLConnection</a></li><li><a href="/java/jax-ws.html" class="sidebar-link">JAX-WS</a></li><li><a href="/java/nashorn-javascript-engine.html" class="sidebar-link">Nashorn JavaScript engine</a></li><li><a href="/java/java-native-interface.html" class="sidebar-link">Java Native Interface</a></li><li><a href="/java/functional-interfaces.html" class="sidebar-link">Functional Interfaces</a></li><li><a href="/java/fluent-interface.html" class="sidebar-link">Fluent Interface</a></li><li><a href="/java/remote-method-invocation-rmi.html" class="sidebar-link">Remote Method Invocation (RMI)</a></li><li><a href="/java/iterator-and-iterable.html" class="sidebar-link">Iterator and Iterable</a></li><li><a href="/java/reflection-api.html" class="sidebar-link">Reflection API</a></li><li><a href="/java/bytebuffer.html" class="sidebar-link">ByteBuffer</a></li><li><a href="/java/applets.html" class="sidebar-link">Applets</a></li><li><a href="/java/expressions.html" class="sidebar-link">Expressions</a></li><li><a href="/java/json-in-java.html" class="sidebar-link">JSON in Java</a></li><li><a href="/java/xml-parsing-using-the-jaxp-apis.html" class="sidebar-link">XML Parsing using the JAXP APIs</a></li><li><a href="/java/xml-xpath-evaluation.html" class="sidebar-link">XML XPath Evaluation</a></li><li><a href="/java/xom-xml-object-model.html" class="sidebar-link">XOM - XML Object Model</a></li><li><a href="/java/polymorphism.html" class="sidebar-link">Polymorphism</a></li><li><a href="/java/encapsulation.html" class="sidebar-link">Encapsulation</a></li><li><a href="/java/java-agents.html" class="sidebar-link">Java Agents</a></li><li><a href="/java/varargs-variable-argument.html" class="sidebar-link">Varargs (Variable Argument)</a></li><li><a href="/java/logging-java-util-logging.html" class="sidebar-link">Logging (java.util.logging)</a></li><li><a href="/java/log4j-log4j2.html" class="sidebar-link">log4j / log4j2</a></li><li><a href="/java/oracle-official-code-standard.html" class="sidebar-link">Oracle Official Code Standard</a></li><li><a href="/java/character-encoding.html" class="sidebar-link">Character encoding</a></li><li><a href="/java/apache-commons-lang.html" class="sidebar-link">Apache Commons Lang</a></li><li><a href="/java/localization-and-internationalization.html" class="sidebar-link">Localization and Internationalization</a></li><li><a href="/java/parallel-programming-with-fork-join-framework.html" class="sidebar-link">Parallel programming with Fork/Join framework</a></li><li><a href="/java/non-access-modifiers.html" class="sidebar-link">Non-Access Modifiers</a></li><li><a href="/java/process.html" class="sidebar-link">Process</a></li><li><a href="/java/java-native-access.html" class="sidebar-link">Java Native Access</a></li><li><a href="/java/modules.html" class="sidebar-link">Modules</a></li><li><a href="/java/concurrent-programming-threads.html" class="sidebar-link">Concurrent Programming (Threads)</a></li><li><a href="/java/executor-executorservice-and-thread-pools.html" class="sidebar-link">Executor, ExecutorService and Thread pools</a></li><li><a href="/java/threadlocal.html" class="sidebar-link">ThreadLocal</a></li><li><a href="/java/using-threadpoolexecutor-in-multithreaded-applications.html" class="sidebar-link">Using ThreadPoolExecutor in MultiThreaded applications.</a></li><li><a href="/java/common-java-pitfalls.html" class="sidebar-link">Common Java Pitfalls</a></li><li><a href="/java/java-pitfalls-exception-usage.html" class="sidebar-link">Java Pitfalls - Exception usage</a></li><li><a href="/java/java-pitfalls-language-syntax.html" aria-current="page" class="active sidebar-link">Java Pitfalls - Language syntax</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/java/java-pitfalls-language-syntax.html#pitfall-missing-a-break-in-a-switch-case" class="sidebar-link">Pitfall - Missing a ‘break’ in a 'switch' case</a></li><li class="sidebar-sub-header"><a href="/java/java-pitfalls-language-syntax.html#pitfall-leaving-out-braces-the-dangling-if-and-dangling-else-problems" class="sidebar-link">Pitfall - Leaving out braces: the "dangling if" and "dangling else" problems</a></li><li class="sidebar-sub-header"><a href="/java/java-pitfalls-language-syntax.html#pitfall-declaring-classes-with-the-same-names-as-standard-classes" class="sidebar-link">Pitfall - Declaring classes with the same names as standard classes</a></li><li class="sidebar-sub-header"><a href="/java/java-pitfalls-language-syntax.html#pitfall-ignoring-method-visibility" class="sidebar-link">Pitfall - Ignoring method visibility</a></li><li class="sidebar-sub-header"><a href="/java/java-pitfalls-language-syntax.html#pitfall-octal-literals" class="sidebar-link">Pitfall - Octal literals</a></li><li class="sidebar-sub-header"><a href="/java/java-pitfalls-language-syntax.html#pitfall-using-to-test-a-boolean" class="sidebar-link">Pitfall - Using '==' to test a boolean</a></li><li class="sidebar-sub-header"><a href="/java/java-pitfalls-language-syntax.html#pitfall-misplaced-semicolons-and-missing-braces" class="sidebar-link">Pitfall - Misplaced semicolons and missing braces</a></li><li class="sidebar-sub-header"><a href="/java/java-pitfalls-language-syntax.html#pitfall-wildcard-imports-can-make-your-code-fragile" class="sidebar-link">Pitfall - Wildcard imports can make your code fragile</a></li><li class="sidebar-sub-header"><a href="/java/java-pitfalls-language-syntax.html#pitfall-using-assert-for-argument-or-user-input-validation" class="sidebar-link">Pitfall: Using 'assert' for argument or user input validation</a></li><li class="sidebar-sub-header"><a href="/java/java-pitfalls-language-syntax.html#pitfall-overloading-instead-of-overriding" class="sidebar-link">Pitfall - Overloading instead of overriding</a></li><li class="sidebar-sub-header"><a href="/java/java-pitfalls-language-syntax.html#pitfall-of-auto-unboxing-null-objects-into-primitives" class="sidebar-link">Pitfall of Auto-Unboxing Null Objects into Primitives</a></li></ul></li><li><a href="/java/java-pitfalls-threads-and-concurrency.html" class="sidebar-link">Java Pitfalls - Threads and Concurrency</a></li><li><a href="/java/java-pitfalls-nulls-and-nullpointerexception.html" class="sidebar-link">Java Pitfalls - Nulls and NullPointerException</a></li><li><a href="/java/java-pitfalls-performance-issues.html" class="sidebar-link">Java Pitfalls - Performance Issues</a></li><li><a href="/java/serviceloader.html" class="sidebar-link">ServiceLoader</a></li><li><a href="/java/classloaders.html" class="sidebar-link">Classloaders</a></li><li><a href="/java/creating-images-programmatically.html" class="sidebar-link">Creating Images Programmatically</a></li><li><a href="/java/atomic-types.html" class="sidebar-link">Atomic Types</a></li><li><a href="/java/rsa-encryption.html" class="sidebar-link">RSA Encryption</a></li><li><a href="/java/secure-objects.html" class="sidebar-link">Secure objects</a></li><li><a href="/java/security-cryptography.html" class="sidebar-link">Security & Cryptography</a></li><li><a href="/java/securitymanager.html" class="sidebar-link">SecurityManager</a></li><li><a href="/java/jndi.html" class="sidebar-link">JNDI</a></li><li><a href="/java/sun-misc-unsafe.html" class="sidebar-link">sun.misc.Unsafe</a></li><li><a href="/java/java-memory-model.html" class="sidebar-link">Java Memory Model</a></li><li><a href="/java/java-deployment.html" class="sidebar-link">Java deployment</a></li><li><a href="/java/java-plugin-system-implementations.html" class="sidebar-link">Java plugin system implementations</a></li><li><a href="/java/javabean.html" class="sidebar-link">JavaBean</a></li><li><a href="/java/java-se-7-features.html" class="sidebar-link">Java SE 7 Features</a></li><li><a href="/java/java-se-8-features.html" class="sidebar-link">Java SE 8 Features</a></li><li><a href="/java/dynamic-method-dispatch.html" class="sidebar-link">Dynamic Method Dispatch</a></li><li><a href="/java/generating-java-code.html" class="sidebar-link">Generating Java Code</a></li><li><a href="/java/jshell.html" class="sidebar-link">JShell</a></li><li><a href="/java/stack-walking-api.html" class="sidebar-link">Stack-Walking API</a></li><li><a href="/java/sockets.html" class="sidebar-link">Sockets</a></li><li><a href="/java/java-sockets.html" class="sidebar-link">Java Sockets</a></li><li><a href="/java/ftp-file-transfer-protocol.html" class="sidebar-link">FTP (File Transfer Protocol)</a></li><li><a href="/java/using-other-scripting-languages-in-java.html" class="sidebar-link">Using Other Scripting Languages in Java</a></li><li><a href="/java/c-comparison.html" class="sidebar-link">C++ Comparison</a></li><li><a href="/java/audio.html" class="sidebar-link">Audio</a></li><li><a href="/java/java-print-service.html" class="sidebar-link">Java Print Service</a></li><li><a href="/java/completablefuture.html" class="sidebar-link">CompletableFuture</a></li><li><a href="/java/runtime-commands.html" class="sidebar-link">Runtime Commands</a></li><li><a href="/java/unit-testing.html" class="sidebar-link">Unit Testing</a></li><li><a href="/java/asserting.html" class="sidebar-link">Asserting</a></li><li><a href="/java/multi-release-jar-files.html" class="sidebar-link">Multi-Release JAR Files</a></li><li><a href="/java/just-in-time-jit-compiler.html" class="sidebar-link">Just in Time (JIT) compiler</a></li><li><a href="/java/bytecode-modification.html" class="sidebar-link">Bytecode Modification</a></li><li><a href="/java/disassembling-and-decompiling.html" class="sidebar-link">Disassembling and Decompiling</a></li><li><a href="/java/jmx.html" class="sidebar-link">JMX</a></li><li><a href="/java/java-virtual-machine-jvm.html" class="sidebar-link">Java Virtual Machine (JVM)</a></li><li><a href="/java/xjc.html" class="sidebar-link">XJC</a></li><li><a href="/java/jvm-flags.html" class="sidebar-link">JVM Flags</a></li><li><a href="/java/jvm-tool-interface.html" class="sidebar-link">JVM Tool Interface</a></li><li><a href="/java/java-memory-management.html" class="sidebar-link">Java Memory Management</a></li><li><a href="/java/java-performance-tuning.html" class="sidebar-link">Java Performance Tuning</a></li><li><a href="/java/benchmarks.html" class="sidebar-link">Benchmarks</a></li><li><a href="/java/fileupload-to-aws.html" class="sidebar-link">FileUpload to AWS</a></li><li><a href="/java/appdynamics-and-tibco-businessworks-instrumentation-for-easy-integration.html" class="sidebar-link">AppDynamics and TIBCO BusinessWorks Instrumentation for Easy Integration</a></li><li><a href="/java/the-classpath.html" class="sidebar-link">The Classpath</a></li><li><a href="/java/resources-on-classpath.html" class="sidebar-link">Resources (on classpath)</a></li><li><a href="/java/contributors.html" class="sidebar-link">The Contributors</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="java-pitfalls-language-syntax"><a href="#java-pitfalls-language-syntax" class="header-anchor">#</a> Java Pitfalls - Language syntax</h1> <p>Several Java programming language misusage might conduct a program to generate incorrect results despite being compiled correctly. This topic main purpose is to list common pitfalls with their causes, and to propose the correct way to avoid falling in such problems.</p> <h2 id="pitfall-missing-a-break-in-a-switch-case"><a href="#pitfall-missing-a-break-in-a-switch-case" class="header-anchor">#</a> Pitfall - Missing a ‘break’ in a 'switch' case</h2> <p>These Java issues can be very embarrassing, and sometimes remain undiscovered until run in production. Fallthrough behavior in switch statements is often useful; however, missing a “break” keyword when such behavior is not desired can lead to disastrous results. If you have forgotten to put a “break” in “case 0” in the code example below, the program will write “Zero” followed by “One”, since the control flow inside here will go through the entire “switch” statement until it reaches a “break”. For example:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">switchCasePrimer</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">int</span> caseIndex <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">switch</span> <span class="token punctuation">(</span>caseIndex<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">case</span> <span class="token number">0</span><span class="token operator">:</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Zero"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"One"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token keyword">case</span> <span class="token number">2</span><span class="token operator">:</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Two"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token keyword">default</span><span class="token operator">:</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Default"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>In most cases, the cleaner solution would be to use interfaces and move code with specific behaviour into separate implementations (<strong>composition over inheritance</strong>)</p> <p>If a switch-statement is unavoidable it is recommended to document "expected" fallthroughs if they occur. That way you show fellow developers that you are aware of the missing break, and that this is expected behaviour.</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">switch</span><span class="token punctuation">(</span>caseIndex<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span>
<span class="token keyword">case</span> <span class="token number">2</span><span class="token operator">:</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Two"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// fallthrough</span>
<span class="token keyword">default</span><span class="token operator">:</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Default"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="pitfall-leaving-out-braces-the-dangling-if-and-dangling-else-problems"><a href="#pitfall-leaving-out-braces-the-dangling-if-and-dangling-else-problems" class="header-anchor">#</a> Pitfall - Leaving out braces: the "dangling if" and "dangling else" problems</h2> <p>The latest version of the Oracle Java style guide mandates that the "then" and "else" statements in an <code>if</code> statement should always be enclosed in "braces" or "curly brackets". Similar rules apply to the bodies of various loop statements.</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// <- open brace</span>
<span class="token function">doSomething</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">doSomeMore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token comment">// <- close brace</span>
</code></pre></div><p>This is not actually required by Java language syntax. Indeed, if the "then" part of an <code>if</code> statement is a single statement, it is legal to leave out the braces</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token function">doSomething</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>or even</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token function">doSomething</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>However there are dangers in ignoring Java style rules and leaving out the braces. Specifically, you significantly increase the risk that code with faulty indentation will be misread.</p> <p><strong>The "dangling if" problem:</strong></p> <p>Consider the example code from above, rewritten without braces.</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token function">doSomething</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">doSomeMore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>This code <strong>seems to say</strong> that the calls to <code>doSomething</code> and <code>doSomeMore</code> will both occur <strong>if and only if</strong> <code>a</code> is <code>true</code>. In fact, the code is incorrectly indented. The Java Language Specification that the <code>doSomeMore()</code> call is a separate statement following the <code>if</code> statement. The correct indentation is as follows:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token function">doSomething</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">doSomeMore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>The "dangling else" problem</strong></p> <p>A second problem appears when we add <code>else</code> to the mix. Consider the following example with missing braces.</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span>
<span class="token function">doX</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>c<span class="token punctuation">)</span>
<span class="token function">doY</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">else</span>
<span class="token function">doZ</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>The code above <strong>seems to say</strong> that <code>doZ</code> will be called when <code>a</code> is <code>false</code>. In fact, the indentation is incorrect once again. The correct indentation for the code is:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span>
<span class="token function">doX</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>c<span class="token punctuation">)</span>
<span class="token function">doY</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">else</span>
<span class="token function">doZ</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>If the code was written according to the Java style rules, it would actually look like this:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">doX</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>c<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">doY</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token function">doZ</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>To illustrate why that is better, suppose that you had accidentally mis-indented the code. You might end up with something like this:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">doX</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">doX</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>c<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>c<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">doY</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">doY</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token function">doZ</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">doZ</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span> <span class="token punctuation">}</span>
</code></pre></div><p>But in both cases, the mis-indented code "looks wrong" to the eye of an experienced Java programmer.</p> <h2 id="pitfall-declaring-classes-with-the-same-names-as-standard-classes"><a href="#pitfall-declaring-classes-with-the-same-names-as-standard-classes" class="header-anchor">#</a> Pitfall - Declaring classes with the same names as standard classes</h2> <p>Sometimes, programmers who are new to Java make the mistake of defining a class with a name that is the same as a widely used class. For example:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>example</span><span class="token punctuation">;</span>
<span class="token comment">/**
* My string utilities
*/</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">String</span> <span class="token punctuation">{</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
</code></pre></div><p>Then they wonder why they get unexpected errors. For example:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>example</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Hello world!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>If you compile and then attempt to run the above classes you will get an error:</p> <div class="language-java extra-class"><pre class="language-java"><code>$ javac com<span class="token operator">/</span>example<span class="token comment">/*.java
$ java com.example.Test
Error: Main method not found in class test.Test, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
</span></code></pre></div><p>Someone looking at the code for the <code>Test</code> class would see the declaration of <code>main</code> and look at its signature and wonder what the <code>java</code> command is complaining about. But in fact, the <code>java</code> command is telling the truth.</p> <p>When we declare a version of <code>String</code> in the same package as <code>Test</code>, this version takes precedence over the automatic import of <code>java.lang.String</code>. Thus, the signature of the <code>Test.main</code> method is actually</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name"><span class="token namespace">com<span class="token punctuation">.</span>example<span class="token punctuation">.</span></span>String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span>
</code></pre></div><p>instead of</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name"><span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span>String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span>
</code></pre></div><p>and the <code>java</code> command will not recognize <strong>that</strong> as an entrypoint method.</p> <p>Lesson: Do not define classes that have the same name as existing classes in <code>java.lang</code>, or other commonly used classes in the Java SE library. If you do that, you are setting yourself open for all sorts of obscure errors.</p> <h2 id="pitfall-ignoring-method-visibility"><a href="#pitfall-ignoring-method-visibility" class="header-anchor">#</a> Pitfall - Ignoring method visibility</h2> <p>Even experienced Java developers tend to think that Java has only three protection modifiers. The language actually has four! The <strong>package private</strong> (a.k.a. default) level of visibility is often forgotten.</p> <p>You should pay attention to what methods you make public. The public methods in an application are the application’s visible API. This should be as small and compact as possible, especially if you are writing a reusable library (see also the <a href="https://en.wikipedia.org/wiki/Open/closed_principle" target="_blank" rel="noopener noreferrer">SOLID<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> principle). It is important to similarly consider the visibility of all methods, and to only use protected or package private access where appropriate.</p> <p>When you declare methods that should be <strong>private</strong> as public, you expose the internal implementation details of the class.</p> <p>A corollary to this is that you only <a href="http://www.tutorialspoint.com/junit/junit_test_framework.htm" target="_blank" rel="noopener noreferrer">unit test<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> the public methods of your class - in fact you can <strong>only</strong> test public methods. It is bad practice to increase the visibility of private methods just to be able to run unit tests against those methods. Testing public methods that call the methods with more restrictive visibility should be sufficient to test an entire API. You should <strong>never</strong> expand your API with more public methods only to allow unit testing.</p> <h2 id="pitfall-octal-literals"><a href="#pitfall-octal-literals" class="header-anchor">#</a> Pitfall - Octal literals</h2> <p>Consider the following code snippet:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token comment">// Print the sum of the numbers 1 to 10</span>
<span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">010</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// Mistake here ....</span>
count <span class="token operator">=</span> count <span class="token operator">+</span> i<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The sum of 1 to 10 is "</span> <span class="token operator">+</span> count<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>A Java beginner might be surprised to know that the above program prints the wrong answer. It actually prints the sum of the numbers 1 to 8.</p> <p>The reason is that an integer literal that starts with the digit zero ('0') is interpreted by the Java compiler as an octal literal, not a decimal literal as you might expect. Thus, <code>010</code> is the octal number 10, which is 8 in decimal.</p> <h2 id="pitfall-using-to-test-a-boolean"><a href="#pitfall-using-to-test-a-boolean" class="header-anchor">#</a> Pitfall - Using '==' to test a boolean</h2> <p>Sometimes a new Java programmer will write code like this:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">check</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> ok<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>ok <span class="token operator">==</span> <span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// Note 'ok == true'</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"It is OK"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>An experienced programmer would spot that as being clumsy and want to rewrite it as:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">check</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> ok<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>ok<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"It is OK"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>However, there is more wrong with <code>ok == true</code> than simple clumsiness. Consider this variation:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">check</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> ok<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>ok <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// Oooops!</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"It is OK"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>Here the programmer has mistyped <code>==</code> as <code>=</code> ... and now the code has a subtle bug. The expression <code>x = true</code> unconditionally assigns <code>true</code> to <code>x</code> and then evaluates to <code>true</code>. In other words, the <code>check</code> method will now print "It is OK" no matter what the parameter was.</p> <p>The lesson here is to get out of the habit of using <code>== false</code> and <code>== true</code>. In addition to being verbose, they make your coding more error prone.</p> <p>Note: A possible alternative to <code>ok == true</code> that avoids the pitfall is to use <a href="https://en.wikipedia.org/wiki/Yoda_conditions" target="_blank" rel="noopener noreferrer">Yoda conditions<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>; i.e. put the literal on the left side of the relational operator, as in <code>true == ok</code>. This works, but most programmers would probably agree that Yoda conditions look odd. Certainly <code>ok</code> (or <code>!ok</code>) is more concise and more natural.</p> <h2 id="pitfall-misplaced-semicolons-and-missing-braces"><a href="#pitfall-misplaced-semicolons-and-missing-braces" class="header-anchor">#</a> Pitfall - Misplaced semicolons and missing braces</h2> <p>This is a mistake that causes real confusion for Java beginners, at least the first time that they do it. Instead of writing this:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>feeling <span class="token operator">==</span> HAPPY<span class="token punctuation">)</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Smile"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">else</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Frown"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>they accidentally write this:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>feeling <span class="token operator">==</span> HAPPY<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Smile"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">else</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Frown"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>and are puzzled when the Java compiler tells them that the <code>else</code> is misplaced. The Java compiler with interpret the above as follows:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>feeling <span class="token operator">==</span> HAPPY<span class="token punctuation">)</span>
<span class="token comment">/*empty statement*/</span> <span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Smile"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// This is unconditional</span>
<span class="token keyword">else</span> <span class="token comment">// This is misplaced. A statement cannot</span>
<span class="token comment">// start with 'else'</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Frown"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>In other cases, there will be no be compilation errors, but the code won't do what the programmer intends. For example:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Hello"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>only prints "Hello" once. Once again, the spurious semicolon means that the body of the <code>for</code> loop is an empty statement. That means that the <code>println</code> call that follows is unconditional.</p> <p>Another variation:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The number is "</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>This will give a "Cannot find symbol" error for <code>i</code>. The presence of the spurious semicolon means that the <code>println</code> call is attempting to use <code>i</code> outside of its scope.</p> <p>In those examples, there is a straight-forward solution: simply delete the spurious semicolon. However, there are some deeper lessons to be drawn from these examples:</p> <li>
The semicolon in Java is not "syntactic noise". The presence or absence of a semicolon can change the meaning of your program. Don't just add them at the end of every line.
</li> <li>
Don't trust your code's indentation. In the Java language, extra whitespace at the beginning of a line is ignored by the compiler.
</li> <li>
Use an automatic indenter. All IDEs and many simple text editors understand how to correctly indent Java code.
</li> <li>
This is the most important lesson. Follow the latest Java style guidelines, and put braces around the "then" and "else" statements and the body statement of a loop. The open brace (`{`) should not be on a new line.
</li> <p>If the programmer followed the style rules then the <code>if</code> example with a misplaced semicolons would look like this:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>feeling <span class="token operator">==</span> HAPPY<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Smile"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Frown"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>That looks odd to an experienced eye. If you auto-indented that code, it would probably look like this:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">if</span> <span class="token punctuation">(</span>feeling <span class="token operator">==</span> HAPPY<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Smile"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Frown"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>which should stand out as wrong to even a beginner.</p> <h2 id="pitfall-wildcard-imports-can-make-your-code-fragile"><a href="#pitfall-wildcard-imports-can-make-your-code-fragile" class="header-anchor">#</a> Pitfall - Wildcard imports can make your code fragile</h2> <p>Consider the following partial example:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>example<span class="token punctuation">.</span>somelib<span class="token punctuation">.</span></span><span class="token operator">*</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>acme<span class="token punctuation">.</span>otherlib<span class="token punctuation">.</span></span><span class="token operator">*</span><span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span>
<span class="token keyword">private</span> <span class="token class-name">Context</span> x <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Context</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// from com.example.somelib</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
</code></pre></div><p>Suppose that when when you first developed the code against version 1.0 of <code>somelib</code> and version 1.0 of <code>otherlib</code>. Then at some later point, you need to upgrade your dependencies to a later versions, and you decide to use <code>otherlib</code> version 2.0. Also suppose that one of the changes that they made to <code>otherlib</code> between 1.0 and 2.0 was to add a <code>Context</code> class.</p> <p>Now when you recompile <code>Test</code>, you will get a compilation error telling you that <code>Context</code> is an ambiguous import.</p> <p>If you are familiar with the codebase, this probably is just a minor inconvenience. If not, then you have some work to do to address this problem, here and potentially elsewhere.</p> <p>The problem here is the wildcard imports. On the one hand, using wildcards can make your classes a few lines shorter. On the other hand:</p> <li>
Upwards compatible changes to other parts of your codebase, to Java standard libraries or to 3rd party libraries can lead to compilation errors.
</li> <li>
Readability suffers. Unless you are using an IDE, figuring out which of the wildcard imports is pulling in a named class can be difficult.
</li> <p>The lesson is that it is a bad idea to use wildcard imports in code that needs to be long lived. Specific (non-wildcard) imports are not much effort to maintain if you use an IDE, and the effort is worthwhile.</p> <h2 id="pitfall-using-assert-for-argument-or-user-input-validation"><a href="#pitfall-using-assert-for-argument-or-user-input-validation" class="header-anchor">#</a> Pitfall: Using 'assert' for argument or user input validation</h2> <p>A question that occasionally on StackOverflow is whether it is appropriate to use <code>assert</code> to validate arguments supplied to a method, or even inputs provided by the user.</p> <p>The simple answer is that it is not appropriate.</p> <p>Better alternatives include:</p> <ul><li>Throwing an IllegalArgumentException using custom code.</li> <li>Using the <code>Preconditions</code> methods available in Google Guava library.</li> <li>Using the <code>Validate</code> methods available in Apache Commons Lang3 library.</li></ul> <p>This is what the <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.10" target="_blank" rel="noopener noreferrer">Java Language Specification (JLS 14.10, for Java 8)<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> advises on this matter:</p> <blockquote></blockquote> <p>Typically, assertion checking is enabled during program development and testing, and disabled for deployment, to improve performance.
Because assertions may be disabled, programs must not assume that the expressions contained in assertions will be evaluated. Thus, these boolean expressions should generally be free of side effects. Evaluating such a boolean expression should not affect any state that is visible after the evaluation is complete. It is not illegal for a boolean expression contained in an assertion to have a side effect, but it is generally inappropriate, as it could cause program behavior to vary depending on whether assertions were enabled or disabled.
In light of this, assertions should not be used for argument checking in public methods. Argument checking is typically part of the contract of a method, and this contract must be upheld whether assertions are enabled or disabled.
A secondary problem with using assertions for argument checking is that erroneous arguments should result in an appropriate run-time exception (such as <code>IllegalArgumentException</code>, <code>ArrayIndexOutOfBoundsException</code>, or <code>NullPointerException</code>). An assertion failure will not throw an appropriate exception. Again, it is not illegal to use assertions for argument checking on public methods, but it is generally inappropriate. It is intended that <code>AssertionError</code> never be caught, but it is possible to do so, thus the rules for try statements should treat assertions appearing in a try block similarly to the current treatment of throw statements.</p> <h2 id="pitfall-overloading-instead-of-overriding"><a href="#pitfall-overloading-instead-of-overriding" class="header-anchor">#</a> Pitfall - Overloading instead of overriding</h2> <p>Consider the following example:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
<span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">String</span> firstName<span class="token punctuation">;</span>
<span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">String</span> lastName<span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token class-name">String</span> firstName<span class="token punctuation">,</span> <span class="token class-name">String</span> lastName<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>firstName <span class="token operator">=</span> <span class="token punctuation">(</span>firstName <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">""</span> <span class="token operator">:</span> firstName<span class="token punctuation">;</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>lastName <span class="token operator">=</span> <span class="token punctuation">(</span>lastName <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">""</span> <span class="token operator">:</span> lastName<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">equals</span><span class="token punctuation">(</span><span class="token class-name">String</span> other<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token punctuation">(</span>other <span class="token keyword">instanceof</span> <span class="token class-name">Person</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token class-name">Person</span> p <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Person</span><span class="token punctuation">)</span> other<span class="token punctuation">;</span>
<span class="token keyword">return</span> firstName<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>p<span class="token punctuation">.</span>firstName<span class="token punctuation">)</span> <span class="token operator">&&</span>
lastName<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>p<span class="token punctuation">.</span>lastName<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">hashcode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> firstName<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">31</span> <span class="token operator">*</span> lastName<span class="token punctuation">.</span><span class="token function">hashCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>This code is not going to behave as expected. The problem is that the <code>equals</code> and <code>hashcode</code> methods for <code>Person</code> do not override the standard methods defined by <code>Object</code>.</p> <ul><li>The <code>equals</code> method has the wrong signature. It should be declared as <code>equals(Object)</code> not <code>equals(String)</code>.</li> <li>The <code>hashcode</code> method has the wrong name. It should be <code>hashCode()</code> (note the capital <strong>C</strong>).</li></ul> <p>These mistakes mean that we have declared accidental overloads, and these won't be used if <code>Person</code> is used in a polymorphic context.</p> <p>However, there is a simple way to deal with this (from Java 5 onwards). Use the <code>@Override</code> annotation whenever you <strong>intend</strong> your method to be an override:</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">equals</span><span class="token punctuation">(</span><span class="token class-name">String</span> other<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token function">hashcode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>When we add an <code>@Override</code> annotation to a method declaration, the compiler will check that the method <strong>does</strong> override (or implement) a method declared in a superclass or interface. So in the example above, the compiler will give us two compilation errors, which should be enough to alert us to the mistake.</p> <h2 id="pitfall-of-auto-unboxing-null-objects-into-primitives"><a href="#pitfall-of-auto-unboxing-null-objects-into-primitives" class="header-anchor">#</a> Pitfall of Auto-Unboxing Null Objects into Primitives</h2> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Foobar</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// example: </span>
<span class="token class-name">Boolean</span> ignore <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>ignore <span class="token operator">==</span> <span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Do not ignore!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>The pitfall here is that <code>null</code> is compared to <code>false</code>. Since we're comparing a primitive <code>boolean</code> against a <code>Boolean</code>, Java attempts to <strong>unbox</strong> the the <code>Boolean</code> <code>Object</code> into a primitive equivalent, ready for comparison. However, since that value is <code>null</code>, a <code>NullPointerException</code> is thrown.</p> <p>Java is incapable of comparing primitive types against <code>null</code> values, which causes a <code>NullPointerException</code> at runtime. Consider the primitive case of the condition <code>false == null</code>; this would generate a <strong>compile time</strong> error <code>incomparable types: int and <null></code>.</p> <h4 id="remarks"><a href="#remarks" class="header-anchor">#</a> Remarks</h4> <p>This topic is about specific aspects of the Java language syntax that are either error prone or that that should not be used in certain ways.</p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/devtut/generate/edit/master/docs/java/java-pitfalls-language-syntax.md" target="_blank" rel="noopener noreferrer">Edit this page on GitHub</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/java/java-pitfalls-exception-usage.html" class="prev">
Java Pitfalls - Exception usage
</a></span> <span class="next"><a href="/java/java-pitfalls-threads-and-concurrency.html">
Java Pitfalls - Threads and Concurrency
</a>
→
</span></p></div> </main></div><div class="global-ui"><!----></div></div>
<script src="/assets/js/app.1779e102.js" defer></script><script src="/assets/js/3.2cfa8016.js" defer></script><script src="/assets/js/1597.ab8ff6ef.js" defer></script>
</body>
</html>