HTML

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

Java Enum in Switch Case Statement
Yesterday, someone 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 the 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.

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 by 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.

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.

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.

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.

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.

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 :