Category Archives: Java

Regular Expressions in Java

Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating and editing a text. It is widely used to define constraint on strings such as password. Regular Expressions are provided under java.util.regex package. The java.util.regex package primarily consists of the following three classes: util.regex.Pattern –… Read More »

Few Tricky Programs in Java

Comments that execute : Till now, we were always taught “Comments do not Execute”. Let us see today “The comments that execute” Following is the code snippet: Output: comment executed The reason for this is that the Java compiler parses the unicode character \u000d as a new line and gets transformed into: Named loops : Output: i… Read More »

Scanner Class in Java

Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double etc. and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming.… Read More »

HashMap and TreeMap in Java

HashMap and TreeMap are part of collection framework. HashMap java.util.HashMap class is a Hashing based implementation. In HashMap, we have a key and a value pair<Key, Value>. HashMap<K, V> hmap = new HashMap<K, V>(); Let us consider below example where we have to count occurrences of each integer in given array of integers. Input: arr[]… Read More »

Array vs ArrayList in Java

In Java, following are two different ways to create an array. Array: Simple fixed sized arrays that we create in Java, like below int arr[] = new int[10] ArrayList : Dynamic sized arrays in Java that implement List interface. ArrayList<Type> arrL = new ArrayList<Type>(); Here Type is the type of elements in ArrayList to be… Read More »

Jagged Array in Java

Prerequisite : Arrays in Java Jagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D arrays but with variable number of columns in each row. These type of arrays are also known as Jagged arrays. Following are Java programs to demonstrate the above concept.… Read More »

Array Copy in Java

Given an array, we need to copy its elements in a different array. Method 1 (Simple but Wrong) We might be tempted to proceed like this: However it’s incorrect! When we do “b = a”, we actually assigning reference of array. Hence if we make any change to one array, it would be reflected in… Read More »

Foreach in C++ and Java

Foreach loop is used to access elements of an array quickly without performing initialization, testing and increment/decrement. The working of foreach loops is to do something for every element rather than doing something n times. There is no foreach loop in C, but both C++ and Jave support foreach type of loop. In C++, it… Read More »

How to swap or exchange objects in Java?

How to swap objects in Java? Let’s say we have a class called “Car” with some attributes. And we create two objects of Car, say car1 and car2, how to exchange the data of car1 and car2? A Simple Solution is to swap members. For example, if the class Car has only one integer attribute… Read More »