Category: Java core

StringBuilder VS StringBuffer

Since String is immutable, it is very costly to use it when constructing a dynamic character string due to the fact that a new memory location will be allocated at each assignment or modification. Since its first release, Java supports a mutable class called StringBuffer which constructs dynamic character strings through allocating just 1 memory location, this saves a lot...

Java try-with-resources

Java 7 supports a new statement called try-with-resources which extends the behavior of the traditional try/catch block for the sake of automatic resource management, since Java 7 developers are able to access resources (files, db connections, sockets) inside a try-with-resources block without the need to worry about closing them afterwards, the resource closure is done automatically. 1. Resource management using traditional...

Define an abstract property in java

Abstract keyword is normally applied on classes and methods in order to delegate the implementation of some behavior to subclasses. Java doesn’t support abstract properties, if you try to mark a class property as abstract, you get a compilation error. In this tutorial, we introduce 2 ways for defining abstract properties which are set by subclasses without using the abstract...

Static keyword in java

Every instance of a class has its own state and behavior, whenever the state of a particular instance is modified, other instances are not affected. In java, a static keyword is normally applied to class members or methods in order to make them shared between all instances, the static keyword forces the variable or the method to only have one...

How to use Enums in java

In this article, we provide several examples on the usage of Enums in java. 1. Overview of Enum Enum is a java type/class which holds a fixed set of related constants, it is the replacement of the traditional definition of multiple static final variables in java. It is mostly recommended when you define a method which accepts a constant argument,...

Java IO: difference between absolute,relative and canonical path

The common way of accessing the file system in java is through the java.io.File API. In this tutorial, we explain how to use the File API to access a file using absolute and relative paths, we also describe the difference between getPath(), getAbsolutePath() and getCanonicalPath() methods provided by the API. 1. Absolute vs Relative In general, a path is a way...

How to run a jar file from command line

So you got an executable jar and you are wondering how to run it! . In this tutorial we define the different ways to run an executable jar through the windows command line. In case you don’t know what’s an executable jar, refer to our previous tutorial about creating executable jars in java. Throughout this tutorial, we consider that you’ve...

How to create an executable jar in java

A jar file is executable if it contains at least one entry point class with a main method. In this tutorial, we define the different ways to create an executable jar. 1. Eclipse export tool Let’s consider a typical hello world example as the following: Eclipse provides a tool to generate an executable jar out of any standalone application, to...

How to create an immutable class in java

An object is immutable if its state cannot change after construction, immutable objects don’t expose any way for other objects to modify their state, the object’s fields are initialized only once inside the constructor and never change again. In this article, we define the typical steps for creating an immutable class in java, we also shed the light on the common...

Working with hashcode() and equals() in java

By default, the java super class java.lang.Object provides 2 important methods: equals() and hashcode() for comparing objects, these methods become very useful when implementing large business which requires interactions between several classes. In this article we talk about the relation between these methods, their default implementation and the circumstances which force developers to provide a custom implementation for each of them. 1. Method...