Category Archives: Java

Enumerations in Java

Java supports enumeration types (in short enum) since Java 5. Enumeration is used to define a list of constants which can be later referenced in source code by its name. Enumeration type in Java is based on concept of the … Continue reading

Posted in Java, Uncategorized | Tagged | Leave a comment

Unexpected NullPointerException (NPE) in ternary conditional operator in Java

Sometimes I come across Java code that unexpectedly throws NullPointerException in ternary operator. It is especially surprising if this particular line of code seems very simple like: or even: At first, it might look like the condition is not met … Continue reading

Posted in Java, Uncategorized | Tagged | Leave a comment

Create text file in Java with contents

Creating a text file (with given contents) is pretty straightforward task in Java. Recent Java versions and popular libraries like Apache Commons or Guava make this task even easier than it used to be. Below are some ways how to … Continue reading

Posted in Guava, Java | Tagged | Leave a comment

Immutable objects

Most of the objects in Java are mutable. It means that their state/fields can be changed after the object was created. The examples of those are: ArrayList, Calendar, StringBuilder. Immutable objects An object is immutable if its state/fields cannot be … Continue reading

Posted in Java, Software development practices | Tagged | Leave a comment

Delete directory with contents in Java

Removing empty directory in Java is as simple as calling File.delete() (standard IO) or Files.delete() (NIO) method. However, if the folder is not empty (for example contains one or more files or subdirectories), these methods will refuse to remove it. … Continue reading

Posted in Java, Maven, Uncategorized | Tagged , , , | 3 Comments

How to delete file in Java

File management (good old CRUD: create, read, update, delete) is quite common operation in software development. In this short post I would like to present 2 ways of removing files in Java. Method available in every Java version Every Java … Continue reading

Posted in Java, Uncategorized | Tagged , | Leave a comment

Importing WSDL with Java and Maven

SOAP web services are often used in commercial software. If we plan to use existing SOAP web service, we should receive a WSDL file which defines the contract between the web service and its clients. This contract defines at least: … Continue reading

Posted in Java, Java EE, Maven, Web-Services, XML | Tagged , , , , | 1 Comment

Objects utility class in Java

Today I would like to quickly mention java.util.Objects class. The JavaDoc documentation for this class says: This class consists of static utility methods for operating on objects. These utilities include null-safe or null-tolerant methods for computing the hash code of … Continue reading

Posted in Java | Tagged | Leave a comment

Default and static methods in interfaces in Java 8

Before Java 8 interfaces could only contain static fields (usually simple constants) and abstract methods. Java 8 provided the ability to define concrete (default and static) methods in interfaces. This new language feature is used extensively in Java core packages. … Continue reading

Posted in Java | Tagged | 1 Comment

Database schema creation in JPA using SQL scripts

Recent versions of JPA provide a feature to automatically create the database objects (like tables, sequences or indexes), load initial data into database on application deployment; and also remove them after the application is undeployed. All that is needed is … Continue reading

Posted in Hibernate, Java, Java EE, JPA | Tagged , , , | Leave a comment