How to use Java Enum in Switch Case Statement - Exampel Tutorial

Java Enum in Switch Case Statement
Yesterday, some one ask me Can we use Java Enum in Switch case ? Obviously, he was learning Enum and not aware that How powerful Enum in Java is. Yes, You can use Enum in Switch case statement in Java like int primitive. If you are familiar with enum int pattern, where integers represent enum values prior to Java 5 then you already knows how to  use Switch case with Enum. Using Java Enum in Switch case is pretty straightforward, Just use Enum reference variable in Switch and Enum constants or instances in CASE statement. In this Java tutorial we will see one example of How to use Enum in Switch statement in Java. By the way Enum is feature rich in Java, Enum can implement interface in Java, Enum can override method in Java, Enum can have constructor in Java and Enum is full functional Type like class or interface. Programmers use Enum with different ways. One of the best example of Enum in Java is replace enum int pattern and enum String pattern. You can also use Enum to write Thread-safe Singleton in Java.

Java program to add two numbers - Example code how to

How to add two numbers in Java
In this Java program tutorial, we will add two numbers provided from the user on the command line. Write a Java program to add two numbers in Java is a common programming exercise for programmers and mostly asked in college exams , school homework, and Java courses as well. There are a different variant as well where you need to ask that write a method to add two numbers e.g. sum(int a, int b) and may ask you to get numbers as input from the user. Taking user input is a little bit tricky for Java programmers but once you know how to do that, it's rather simple.

NullPointerException in Java - A nemesis of Programmer - Tips to fix and prevent

NullPointerException in Java is a unchecked Exception defined in java.lang package and comes when a member of a an object either field or method is called on a object which is null. null is a keyword in Java which means nothing and calling method on Object whose value is null will result in NullPointerException. Since default value of Object is null, if you call any method or access any field on an Object which is not initialized will throw NullPointerException. Some programmer get confused with name as well, Since Java does not support pointers, How can you have NullPointerException in Java? Well java.lang.NullPointerException doesn't have anything to do with pointers, it just an Exception in Java. If you look at it more deeply, NullPointerException is an unchecked Exception and it's not mandatory to provide Exception handling code for it using try, catch and finally. In fact most of NullPointerException comes because of programming errors. See most common cause of NullPointerException to find out most common scenario where NullPointerException comes in Java. In this Java article we will not only see What is NullPointerException but also How to avoid NullPointerException and How to fix java.lang.NullPointerException in Java.

Java program to get SubList from ArrayList - Example

Sometimes we need subList from ArrayList in Java. For example, we have an ArrayList of 10 objects and we only need 5 objects or we need an object from index 2 to 6, these are called subList in Java. Java collection API provides a method to get SubList from ArrayList. In this Java tutorial, we will see an example of getting SubList from ArrayList in Java. In this program, we have an ArrayList which contains 4 String objects. Later we call ArrayList.subList() method to get part of that List.

DOM vs SAX Parser in Java - XML Parsing in Java

DOM vs SAX parser in Java
DOM and SAX parser are two most popular parser used in Java programming language to parse XML documents. DOM and SAX concept are originally XML concept and Java programming language just provide an API to implement these parser. Despite both DOM and SAX are used in XML parsing, they are completely different to each other. In fact difference between DOM and SAX parser is a popular Java interview question asked during Java and XML interviews. DOM and SAX parser has different way of working, which makes Java programmer to understand difference between DOM and SAX parser even more important. Careless use of DOM parser may result in java.lang.OutOfMemoryError if you try to parse a huge XML file and with small heap size while careless use of SAX parser may result in poor performance while parsing small and medium sized XML files with good enough heap space in Java. In this Java article we will compare DOM and SAX parser and learn difference between them.

Java Program to convert String ArrayList to String Array

Converting String ArrayList into String array is very common programming task in Java. you often need to convert Array to Array List in Java  and vice-versa. In this Java program, we will How to convert String ArrayList to String array. This is also a common programming exercise which is asked too many Java programmers in various Java related courses. It’s also worth noting that ArrayList in Java is internally backed by array and Array in Java are objects much like String which is also an Object in Java. In this Java program, we first create an ArrayList which stores name of months as String e.g. Jan, Feb, and Mar. Later we use ArrayList  toArray() method to convert ArrayList into an array.

What is blank final variable in Java - Example

What is blank final variable in Java
Blank final variable in Java is a final variable which is not initialized while declaration, instead they are initialized in a constructor. Java compiler will complain if a blank final variable is not initialized during construction. If you have more than one constructor or overloaded constructor in your class then blank final variable must be initialized in all of them, failing to do so is a compile time error in Java. Alternatively, you can use constructor chaining to call one constructor from other using this keyword, in order to delegate initialization of blank final variable in Java. In this Java tutorial, we will see What is blank final variable in Java and a code example on How to use a blank final variable.

Java program to find square root of number - Example Code

Java program for square root or a number in Java
How to write Java program to find square root of a number is common Java programming exercise which many institute use in there Java course along with Java program to print Fibonacci series and How  to find Armstrong numbers in Java, which we have seen earlier.  Java program for square root is also a popular question during college semester exams and  various programming tests. If you are familiar with Java API then writing a Java program which can find square root of number using java.lang.Math class is not difficult. You just need to pass a double value and it returns a double which is square root of number you passed.  In next section we will see complete code example of finding square root of a number from Java program. Another Java coding questions which is very popular and related to programming exercise is writing Java program to find prime numbers, if you are going to appear in Java interviews then its worth looking.

What is difference between interface and abstract class in Java

What is abstract class and interface in Java
The difference between abstract class and interface in Java is one of the tricky Java interview question and mostly appear in core Java interviews. It has become now even trickier after Java 8 introduced default methods and allowed interfaces to have both default and static methods. Even though both interface and abstract class is a way to achieve abstraction in Java, there are significant differences between them, which you will learn in this article. Some time interviewer also not just focus on key differences between abstract class and interface in Java but he is also interested in some practical experience e.g. when to use interface in Java and when to use abstract class is Java. This is actually the tricky part of this interview question and  you must have a good understanding of what is an interface and abstract class in Java and how to use them. Anyway in this Java article we will first see some syntactical difference between interface and abstract class in Java programming language and later we will see where to use abstract class and interface.

Autoboxing, Enum, Generics, Varargs methods - Java 5 Features Quick Overview

What is Autoboxing , Generics, Enum and Varargs method in Java 5
Java 5 introduces Autoboxing, Generics, varargs and Enum along with several other features and improvement. It's been few years when Java programming language was enhanced with these features but still Java programmer thing Autoboxing, Enum, Generics or Variable arguments as an advanced feature and afraid to learn them. They are very much part of Java fundamentals just like Abstraction, Inheritance, Encapsulation and Polymorphism are part of Object oriented programming concepts. It's important to understand what are these feature and How to use them, even if you don't use them in your code, you may have to work on someone else code which is written in Java 5 and uses Generics Collection, Autoboxing quite frequently. In this Java 5 tutorial, we will see the brief overview of Java 5 features like Generics, Enum, Autoboxing and unboxing and variable arguments methods along with examples and explanations.

What is Struts Action Class in Java J2EE - How to use

What is Action class in Struts
Struts in java is a framework, used to make web application its is based on Model View Controller or MVC design Pattern where Model represent the internal state and action used to change the state view represent presentation component and a controller is responsible for receiving the request from the client and decide which business logic should be called. Basically, Struts have different classes to represent this Model, View, and Controller we call them as Action, Action Form, and Action Servlet. So

Model – Action classes
View - Action form classes
Controller – Action Servlet classes

In this article, we are focusing on the Model layer of struts framework. Action class is used to provide an interface to application model layer.  What is Action class and how to use Action class is also a popular Struts interview Question asked in various J2EE interviews.

What is CopyOnWriteArrayList in Java - Example Tutorial

CopyOnWriteArrayList vs Array List in Java
CopyOnWriteArrayList is a concurrent Collection class introduced in Java 5 Concurrency API along with its popular cousin ConcurrentHashMap in Java. CopyOnWriteArrayList implements List interface like ArrayList, Vector, and LinkedList but its a thread-safe collection and it achieves its thread-safety in a slightly different way than Vector or other thread-safe collection class. As the name suggests CopyOnWriteArrayList creates a copy of underlying ArrayList with every mutation operation e.g. add, remove, or when you set values. That's why it is only suitable for a small list of values which are read frequently but modified rarely e.g. a list of configurations.

Top 10 Java Collection interview Questions Answers | Java Collection FAQ

Java Collection Framework interview questions and answers contain questions from popular Java collection classes e.g. HashMap, ArrayList, HashSet, ConcurrentHashMap and legacy collection classes like Vector and Hashtable. Interview questions from Java Collection framework are one of the most asked concepts on any Core Java interview or J2EE interview at both beginners, experienced and intermediate level e.g. 2 to 4 years experienced Java programmer. In order to answer interview questions from Java collection framework, You need a good knowledge of various Collection interfaces e.g. Map, List and Set and familiarities with their frequently used implementation e.g. HashMap, Hashtable, TreeMap from Map, ArrayList, LinkedList and Vector for List interface and HashSet, TreeSet of Set interface.

What is new in Java 7 - Top 5 JDK 7 feature

What is new in Java or JDK 7
It's close to a year now JDK 7 or Java 7 has been released, still programmer asks What is new in Java 7 ? What is the best feature introduced in JDK7, where is the list of all Java 7 new feature etc? I thought to let's document Top 5 new features introduced in Java 7 for easy reference, this will not only answer What is new in Java 7 but also provide a quick overview of What are those features in Java 7.  Java 7 introduced many new features in Java programming language like try catch with a resource, String in Switch etc but it also makes a lot of change on Java Development API by introducing new File API in Java and several other minor changes. E.g. you can now find hidden files from Java program without applying any hack. anyway let's see my list of Top 5 Java 7 features :

Top 18 Java Design Pattern Interview Questions Answers for Experienced

Design pattern interview question in Java
Both OOP and GOF design pattern interview questions are an integral part of any good list of core Java interview questions. Java is a popular Object oriented programming language and has lots of design pattern and design principles, contributed by many developers and open source framework. As a Java programmer its expected from you to know OOPS concepts like Abstraction, Encapsulation, and Polymorphism, What is design pattern in Java, Some popular Java design pattern and most importantly when to use those design pattern in Java application. The purpose of asking design pattern interview question in Java is to check whether Java programmer is familiar with those essential design patterns or not.