Monday, July 22, 2013

Java Mysteries

If you aren't a computer nerd -- you might want to skip this one.

I have started to use the FindBugs plugin for Eclipse, and it is very nice.  It finds many obscure failures that Eclipse or MyEclipse alone does not.  One particular problem it found in the code that I am attempting to brutalize into usefulness was use of == and != operators for doing String compares.  If you program in Java, you immedately recognize that:

    if (strObject == "hello")

is wrong.  String compares are done with

   if (strObject.equals("hello")

But we had almost 200 string compares using == or !=.  So why do we not have more problems?  The always useful StackOverflow explains why this often works, in spite of being wrong.  Java is smart enough to create a single copy of a String if it is a constant and there is another constant of the same content.  As a result, == and != will work if you are comparing two String objects that are both constants.  Thus, the many compares of the form strObj == "" work because the passed in parameter strObj was initialized somewhere to "".  If for any reason strObj is created in some other manner, such as new String(""), or as the result of some concatenation operation, the use of == or != is not going to work correctly.

As a result, in many cases, these really invalid uses of == and != work -- but it is a hazardous practice!


6 comments:

SJ said...

For all the usefulness of Java, this kind of thing makes me distrust it slightly.

On the other hand, there will always be money to be made fixing mistakes made by coders who didn't care about the distinction between "==" and ".equals()"...

Clayton said...

For the life of me, I don't see why someone early on didn't define == and != to call the equals method appropriately for String objects. But once defined this way, changing it would have been dangerous.

asdf said...

In CSharp, == gives the same result as .Equals(), but the method can be overloaded and offers some other functionality. In some versions of CSharp, the method of comparison was different, although it produced the same results, and that may still be true. But I think a CSharp coder uses == routinely.

And since the CSharp studio is far, far superior to Eclipse (which is annoyingly sluggish) I have to wonder why anyone is still using Java.

Clayton said...

Visual Studio is definitely a nicer development environment than Eclipse. Java has the advantage of not being tied to Microsoft. Since this is likely the last job that I will ever have, it's all academic.

Sigivald said...

Yeah, I'm a .NET person and it baffles me that Java does it that way.

It's like Sun doesn't like programmers or something...

(Note that technically the .NET languages aren't tied to Microsoft either; the CLR and IL and language specs are all open, which is why Mono exists.

Visual Studio, of course, is tied to MS.)

asdf said...

What I like about MS is that they have used their market position to allow an extraordinary amount of interplay among their products. You can write aggregate functions in csharp that are called in SQL server. VBA is consistent across all the office programs, and automating excel is a remarkable time saver.

Let's face it. MS is one of the good guys.