Skip to content
JackSparrow414
Go back

Everyday Development Pitfalls

Table of contents

Open Table of contents

Article body

First pitfall:

When comparing a string and a variable with equals, suppose STR1 =“你好” and STR2 is a variable. The recommended form for comparing them is:

str1.equals(STR2);

The incorrect form is:

str2.equals(“你好”);

In the second form, a null STR2 causes a NullPointerException. The first form does not throw that exception; it returns false.

Equality comparisons between different types return null.

In other words, when comparing two variables, call the method on the one that is known to exist, and pass the potentially null one as the argument.

Second pitfall:

When declaring a List or Map, we commonly write:

List sqls = new ArrayList ();

The type information on the right can be omitted and inferred by the compiler. The example is:

List sqls = new ArrayList ();

Third pitfall:

Oracle uses || for string concatenation.

Fourth pitfall:

If you write a SQL query as select * from A where A.name != null,

it will not return any rows.

Remember to use IS NULL and IS NOT NULL when testing for null. 

For more detail on the difference between IS NOT NULL and != NULL, see the material in my personal bookmarks.


Share this post:

Previous Post
Oracle IN and EXISTS: Query Notes
Next Post
Small Details to Remember in Everyday Development

Comments

Questions, corrections, and experiences are welcome. Sign in with GitHub to comment; both language versions share this discussion.

Comments are available on the live site only.