What is the equivalent of the C++ Pair in Java?

In a thread on comp.lang.java.help, Hunter Gratzner gives some arguments against the presence of a Pair construct in Java. The main argument is that a class Pair doesn’t convey any semantics about the relationship between the two values (how do you know what “first” and “second” mean ?). A better practice is to write a very simple class, like the one … Read more

What exactly is Apache Camel?

If you have 5 to 10 minutes, I generally recommend people to read this Integration with Apache Camel by Jonathan Anstey. It’s a well written piece which gives a brief introduction to and overview of some of Camel’s concepts, and it implements a use case with code samples. In it, Jonathan writes: Apache Camel is an open … Read more

How to use the toString method in Java?

From the Object.toString docs: Returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name … Read more

&& (AND) and || (OR) in IF statements

No, it will not be evaluated. And this is very useful. For example, if you need to test whether a String is not null or empty, you can write: or, the other way around If we didn’t have ‘short-circuits’ in Java, we’d receive a lot of NullPointerExceptions in the above lines of code.

How to format strings in Java

In addition to String.format, also take a look java.text.MessageFormat. The format less terse and a bit closer to the C# example you’ve provided and you can use it for parsing as well. For example: A nicer example takes advantage of the varargs and autoboxing improvements in Java 1.5 and turns the above into a one-liner: MessageFormat is … Read more