How to Split String in Java using Regular Expression

String class provides split() method to split String in Java, based upon any delimiter, e.g. comma, colon, space or any arbitrary method. split() method splits the string based on delimiter provided, and return a String array, which contains individual Strings. Actually, split() method takes a regular expression, which in simplest case can be a single word. split() is also overloaded method in java.lang.String class and its overloaded version take a limit parameter which is used to control how many times pattern will be applied during splitting process. if this limit is positive n, then the pattern will be applied at most n-1 times, if it's negative or zero then split operation is applied any number of time. For example, if we split String "First,Second,Third" on comma and provide limit as 2 then pattern will run one time, and split() will return String array with 2 Strings, "First" and "Second,Third". 

How to replace characters on String in Java

One of the common programming tasks is to replace characters or substring from a String object in Java. For example, you have a String "internet" and you want to replace the letter "i" with letter "b", how do you that? Well, String class in Java provides several methods to replace characters, CharSequence and substring from a String in Java.You can call replace method on the String, where you want to replace characters and it will return a result where characters are replaced. What is the most important point to remember is that the result object would be a new String object? Since String is immutable in Java, every time you perform an operation on String either replacement or removing white space from String, it generates a new String object. The good thing about these methods is that they support regular expression, which means you can specify a pattern and all the characters which match the pattern will be replaced. 

Difference between notify and notifyAll in Java

wait, notify, and notifyAll methods are used for inter-thread communication in Java. wait() allows a thread to check for a condition, and wait if the condition doesn't meet, while notifying() and notifyAll() method informs waiting for a thread for rechecking condition, after changing the state of a shared variable. One good example of how to wait and notify method works is Producer consumer problem, where one thread produces and wait if the bucket is full; and another thread consumes and waits if the bucket is empty. Both Producer and Consumer thread, notify each other as well. Producer thread notifies consumer thread after inserting an item in the shared queue, while consumer thread notifies producer, after consuming item from the queue. Though Both notify() and notifyAll()  are used to notify waiting for threads, waiting on shared queue object, but there are some subtle differences between notify and notifyAll in Java.

How to convert String to int in Java

There are 3 main ways to convert String to int in Java, using the constructor of Integer class, parseInt() method of java.lang.Integer and Integer.valueOf() method. Though all those methods return an instance of java.lang.Integer, which is a wrapper class for primitive int value, it's easy to convert Integer to int in Java. From Java 5, you don't need to do anything, autoboxing will automatically convert Integer to int. For Java 1.4 or lower version, you can use intValue() method from java.lang.Integer class, to convert Integer to int. As the name suggest, parseInt() is the core method to convert String to int in Java. parseInt() accept a String which must contain decimal digits and first character can be an ASCII minus sign (-) to denote negative integers. parseInt() throws NumberFormatException, if provided String is not convertible to int value. 

HelloWorld Program in Java with Example

First of all, welcome to the exciting world of Java programming. If you are ready to write your first Java program i.e. HelloWorld in Java, it means you already crossed major hurdles to start Java programming, in terms of installing JDK and setting PATH for Java. If you haven't done so then you can follow those tutorials to install JDK in Windows 7 and 8 and setting PATH for Java. Before we start writing HelloWorld in Java, few notes about editors. Many Java beginners insist or try to use Eclipse or Netbeans IDE from the very start of the first program, which is not a good idea. In order to learn syntax and semantics of Java programming, you should code in notepad, word pad or any other text editor. I started coding in DOS Editor, but I won't suggest that to you. Notepad++ is best of both world, it support Java syntax highlighting, which helps to understand keywords better, and its also a pleasant coding experience. 

How to Iterate over Java Enum : values() Example

Enum Values() Example
In this Java programming tutorial, we will learn how to iterate over enum in Java. Since Enums are a collection of a finite number of well-known objects, often we need to iterate over them. Enums are also final in Java and has a private constructor, which means you can not create enum instances once declared. Iteration over Enum is extremely simple, provided you know about implicit values() method, which is a static method, provided by java.lang.Enum. Since every enum in Java extends java.lang.Enum, they all get this implicit values() method. Actually there are couple of them, e.g. valueOf(), name(), ordinal() etc. This method returns enum constants in the order they are declared in Enum class. Even the ordinal() method returns the same order.

How to use ArrayList in Java with Examples

Java ArrayList Example
ArrayList in Java is one of the most popular Collection class. ArrayList is an implementation of List interface via AbstractList abstract class, and provides ordered and index based way to store elements. Java ArrayList is analogous to an array, which is also index based. In fact, ArrayList in Java is internally backed by an array, which allows them to get constant time performance for retrieving elements by index. Since an array is fixed length and you can not change their size, once created, Programmers, starts using ArrayList, when they need a dynamic way to store object, i.e. which can re-size itself. See the difference between Array and List for more differences. Though, apart from ArrayList, there are other collection classes like Vector and LinkedList which implements List interface and provides similar functionalities, but they are slightly different. ArrayList is different to Vector in terms of synchronization and speed. Most of the methods in Vector requires a lock on Collection which makes them slow. See the difference between ArrayList and Vector more differences.