Swapping strings Swapping two strings means interchanging their values with one another. Example, there are two string variables s1 and s2 with values s1 = “codippa.com” and s2 = “the website”, Swapping the two strings will make their value as s1 = “the website” and s2 = “codippa.com” Common Approach The most common approach of swapping two strings is to store the value of one of the strings into a temporary variable and then assigning the value of second string to first one. Finally, assigning the value of temporary variable back to the first string as shown below String s1 = “codippa.com”; String s2 =Read More →

Meaning of @Override annotation @Override annotation is used over methods defined in a class. When this annotation is written over a method, then this method : Might be declared in a super class and defined in sub-class. Might be defined in a super(or parent) class and re-defined in sub-class(overridden). Might be declared in an interface and defined in the class implementing the interface. Example of this annotation is given below. public abstract class ParentClass { /* * Implemented method in parent class */ public void implementedMethod() { System.out.println(“Parent class method”); } /* * Abstract method in parent class */ public void declaredMethod(); } public classRead More →

Scenario Suppose your application starts a process (a server embedded in the application, let’s say). The application may be running from a command line interface or a GUI. Now, you want that whenever user terminates the application either by typing Ctrl + C(control and c keys pressed together) on command line or pressing close button on the application GUI, the process started by the application is also terminated. Shutdown Hook is the solution A shutdown hook is a piece of code which executes before an application terminates. Java provides a mechanism to apply shutdown hooks in an application. This shutdown hook is executed in belowRead More →

Monitoring directory : Meaning & Reason Monitoring a directory or a folder means keeping a watch on that directory for events such as a new file created, a file removed or modified. A typical requirement for such monitoring would be to implement an auto refresh functionality where you show the contents of a folder. With active monitoring on a folder, you can display the accurate list of files when the directory contents are modified in the background. How to Monitor a directory in Java Java 7 introduced java.nio package which provides support for monitoring a directory for changes. It allows us to create a serviceRead More →

Why use File filters Many times it happens that only files of certain type are required. Type may mean files of certain extension or files having some definite characters in their name. There is no sense of showing or listing files which are of no use in that scenario. Consider an example where you need to upload image files to an application. When you click on Browse button, you would only be interested in viewing the list of image files(having .bmp, .jpg, .jpeg or .png extension) in the directory you select. It would be a hassle for you to select the desired file if allRead More →

What is an enum ? An enum is a kind of data type in java which can contain a fixed set of values. These values are constants. The enum can have any one value out of these fixed set of values. Enum Syntax An enum is created using keyword enum followed by a user defined name and some fixed set of values enclosed between { and }. Where is enum used An enum is utilized when a particular type can have multiple fixed set of constant values. For Example, your application can have different types of user roles. Instead of using constants for these roleRead More →

Many times developers encounter this scenario where they have an array of string values and another single string value and they may need to perform any of the following tasks : Check whether the string exists in the array or search array for the given string. If the string exists in the array, then get the index of its position in array. This post will detail out 5 different methods to search a string in an array to check if the array contains it or not with example programs. Method 1 : Iterating over the array This is a conventional and most used method whereRead More →

Shuffling the contents of an ArrayList is a problem which can test the logic of a person and also demonstrates knowledge of java api. This post contains a couple of methods which shuffle the contents of ArrayList. Method 1 : Without using Collections class This method utilizes the approach of random number generation which is within the size of the list and then swapping the elements at two different indices of the list. Algorithm Logic to shuffle ArrayList without using Collections class involves the following steps : Calculate the size of the list. Iterate over the list and in each iteration : a. Retrieve theRead More →