Java

Java programming language. A collection of notes and code snippets.

Published: Sunday, 28 October 2007
Last modified: Wednesday, 8 April 2020

Java is a powerful and mature programming language that lets you write programs that run virtually anywhere. It accomplishes this by using Java Virtual Machines (JVMs).

In 2020 the latest stable release is version 14.

Notable languages that utilise the JVM include Scala and Kotlin.

Profilers

I’ve used both YourKit and JProfiler. Both were sufficient for my needs and integrate well into IntelliJ.

For a free option, try jvisualvm that is bundled with the JDK.

JDBC

MySQL

Troubleshooting

Address already in use: connect

 ** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.net.BindException: Address already in use: connect

STACKTRACE:

java.net.SocketException: java.net.BindException: Address already in use: connect
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)
	at com.mysql.jdbc.Connection.createNewIO(Connection.java:2555)
	at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)

It isn’t saying that its trying to listen on a local socket, but can’t create a socket. Check if an excessive amount of TCP sockets are already open using netstat.

System Properties

The JVM takes arguments on the command line -Dname=value

e.g. To specify the default timezone, java -Duser.timezone=GMT

To specify an endorsed directory, where jars will take precedence over the default JRE classes, use -Djava.endorsed.dirs

JSR

JSR 310: Date time calendar API

Consider Joda Time as a replacement date time API.

Patterns and Regular Expressions

Non-Greedy matching

Add the ? character to the expression to indicate a non-greedy search.

String greedy = "fish<br>cakes<i>penguin".replaceAll("<.*>","");
String nonGreedy = "fish<br>cakes<i>penguin".replaceAll("<.*?>","");
System.out.println("greedy: " + greedy);
System.out.println("nonGreedy: " + nonGreedy);

The above will output

greedy: fishpenguin
nonGreedy: fishcakespenguin

Group matching and back references

To use back references (i.e. referring to a portion of the pattern that was matched), create your pattern, and add round brackets to surround the groups you are interested in.

An as example, consider the following snippet

Pattern compile = Pattern.compile("Your balance is (.*) dollars");
String message = "Your balance is 123 dollars";
Matcher matcher = compile.matcher(message);
if (matcher.matches()) {
    // matched
    String wholePattern = matcher.group(0);
    String group1 = matcher.group(1);
    System.out.println("wholePattern: " + wholePattern);
    System.out.println("group1: " + group1);
}

and output:

wholePattern: Your balance is 123 dollars
group1: 123

Note the round brackets that where used in the pattern. This is a group which can be back referenced.

A matcher is created, and the matches method is called to test whether a match was found.

If a match was found, you can call the group method with 0 to see the whole match, and if you used round brackets or groups, you can refer to specific groups that were matched starting from 1.

Non-capturing group

Sometimes you want to use a group (i.e. round brackets) to denote logical OR, but do not want to capture the group. e.g. If you want to look for the word apple or banana, you can have a pattern such as:

String pattern = ".*(apples|bananas).*";

In this case, matcher.group(1) will not exist

Monitoring

Setup JMX so you can connect to your instance via jconsole.

For thread dumps, you can use jstack $pid which will display a thread dump for you.

Javadoc

Specify the class, and optionally the linkText.

{@link com.magicmonster.example.Class linkText}

java.lang.IncompatibleClassChangeError: Implementing class

You probably have duplicate classes from different jars. Check your deployment does not include different versions of the same jar.


More Articles