Linking to an external URL in Javadoc?

This creates a “See Also” heading containing the link, i.e.: /** * @see <a href=”http://google.com”>http://google.com</a> */ will render as: See Also:            http://google.com whereas this: /** * See <a href=”http://google.com”>http://google.com</a> */ will create an in-line link: See http://google.com

Jackson with JSON: Unrecognized field, not marked as ignorable

You can use Jackson’s class-level annotation: import com.fasterxml.jackson.annotation.JsonIgnoreProperties @JsonIgnoreProperties class { … } It will ignore every property you haven’t defined in your POJO. Very useful when you are just looking for a couple of properties in the JSON and don’t want to write the whole mapping. More info at Jackson’s website. If you want to … Read more

File to byte[] in Java

From JDK 7 you can use Files.readAllBytes(Path). Example: import java.io.File; import java.nio.file.Files; File file; // …(file is initialised)… byte[] fileContent = Files.readAllBytes(file.toPath());

Efficiency of Java “Double Brace Initialization”?

Here’s the problem when I get too carried away with anonymous inner classes: 2009/05/27 16:35 1,602 DemoApp2$1.class 2009/05/27 16:35 1,976 DemoApp2$10.class 2009/05/27 16:35 1,919 DemoApp2$11.class 2009/05/27 16:35 2,404 DemoApp2$12.class 2009/05/27 16:35 1,197 DemoApp2$13.class /* snip */ 2009/05/27 16:35 1,953 DemoApp2$30.class 2009/05/27 16:35 1,910 DemoApp2$31.class 2009/05/27 16:35 2,007 DemoApp2$32.class 2009/05/27 16:35 926 DemoApp2$33$1$1.class 2009/05/27 16:35 4,104 … Read more

How do I copy an object in Java?

Create a copy constructor: class DummyBean { private String dummy; public DummyBean(DummyBean another) { this.dummy = another.dummy; // you can access } } Every object has also a clone method which can be used to copy the object, but don’t use it. It’s way too easy to create a class and do improper clone method. … Read more

Scanner is skipping nextLine() after using next() or nextFoo()?

That’s because the Scanner.nextInt method does not read the newline character in your input created by hitting “Enter,” and so the call to Scanner.nextLine returns after reading that newline. You will encounter the similar behaviour when you use Scanner.nextLine after Scanner.next() or any Scanner.nextFoo method (except nextLine itself). Workaround: Either put a Scanner.nextLine call after … Read more

How to convert a char to a String?

You can use Character.toString(char). Note that this method simply returns a call to String.valueOf(char), which also works. As others have noted, string concatenation works as a shortcut as well: String s = “” + ‘s’; But this compiles down to: String s = new StringBuilder().append(“”).append(‘s’).toString(); which is less efficient because the StringBuilder is backed by … Read more