All posts in "Java"

How to create and write to a file in Java

January 29, 2017

In this short tutorial, we will discuss how to create and write to a file in Java. 1. Creating file with PrintWriter We will be using* package. We will create a file. And by using PrintWriter object we will write to it.

At the beginning we should create the file:

When we have our […]


Difference between wait() and sleep() in Java

January 16, 2017

In this short post, we will discuss the difference between wait() and sleep(). In simple words sleep() hold on the process in memory for specified time while wait() releases the monitor and keeps “waiting” until calling the notify() method. Wait() is used when we want to communicate between specified threads. Sleep() is just for mere […]


How to convert a stack trace to String

December 26, 2016

In this post, we will convert a stack trace to String. There are multiple methods for doing this. 1. Using StringWriter and PrintWriter The first method is to use StringWriter and PrintWriter classes. Let’s have a look on a below example.

In the first line, we create an instance of the StringWriter class. After that, we create […]


Convert array to ArrayList in Java

December 23, 2016

You can convert arrays to ArrayList by using this method:

1. Convert Integer array to ArrayList

In the first line of above code, we declare an array of Integers. In the second line, we convert this array into ArrayList. Lastly, we print the integerList. The output of the above program is:

2. Convert String array to ArrayList When […]


String concatenation and StringBuilder comparison in Java

November 27, 2016

In this post, we compare String concatenation and StringBuilder. 1. String concatenation Strings are immutable. So we can’t change existing String, every time Java create a new String to replace previous one. String concatenation is adding two strings using operator +;

The result of executing the code above is:

2. StringBuilder StringBuilder is a mutable sequence […]


What is @Override annotation and when to use it in Java

October 13, 2016

@Override Annotation tells the compiler that this method overrides a superclass method. This annotation is optional but is good practice to use it whenever you override a method.

In above example, we override print method and use @Override Annotation Why should you use @Override Annotation? When you use this annotation compiler will generate error when 1. You […]


How to convert int to String in Java.

October 4, 2016

To convert int to String in Java you can use: 1. Integer.toString(int i) method: The String method takes Integer.toString(int s) an int as an argument and returns a String object.

This example uses Integer.toString(int i) to convert int to String. Result of above code is:

2. String.valueOf(int i) method: The String method takes String.valueOf(int s) […]


C++ Pair class equivalent in Java

September 30, 2016

In the standard Java library, there is not Pair class. When you want to use Pair you have some options. 1. Create new class. For example, you want to create a pair of name-price items; better is create a new class, containing field name and price. It will improve code readability, and you can add […]


Difference between StringBuffer and StringBuilder in Java

September 11, 2016

First, we have to remember that String is immutable and final in Java. Every modification of String (concatenation, substring, converting into lowercase or uppercase) creates a new String object. StringBuffer and StringBuilder are classes which allow us to manipulate String without creating garbage. Append method implementations What is the difference between them? Let’s look at the implementations of the […]


How to compare enums in Java

September 6, 2016

Summary The best way to compare enums in Java is == operator, like in an example below:

Comparison of == and equals methods There are two ways to compare enums in java: == operator and equals() method. First of all, I would like to show you an implementation of method equals() of java.lang.Enum:

As […]

Page 3 of 4

Privacy Preference Center