Posts

Java Autoboxing and Unboxing

Java 1.5 introduced a special feature of auto conversion of primitive types to the corresponding Wrapper class and vice versa. Autoboxing : Automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing. For example – conversion of int to Integer, long to Long, double to Double etc. Unboxing : It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. For example – conversion of Integer to int, Long to long, Double to double etc. Primitive type Wrapper class boolean         Boolean byte             Byte char             Character float            Float int              Integer long             Long short            Short double           Double When does the autoboxing and unboxing happens in Java  Autoboxing Lets see few cases with examples, where autoboxing happens.   Case 1 : Wh

Java Regular Expressions (java regex)

Regular expressions are used for defining String patterns that can be used for searching, manipulating and editing a text. These expressions are also known as Regex (short form of Regular expressions). Lets take an example to understand it better: In the below example, the regular expression .*book.* is used for searching the occurrence of string “book” in the text. import java.util.regex.*;   class RegexExample1{      public static void main(String args[]){         String content = "This is Chaitanya " +             "from Beginnersbook.com.";       String pattern = ".*book.*";       boolean isMatch = Pattern.matches(pattern, content);       System.out.println("The text contains 'book'? " + isMatch);    } }   Output: The text contains 'book'? true In this tutorial we will learn how to define patterns and how to use them. The java.util.regex API (the package which we need to import w