Java Enum ValueOf Example - How to use

valueOf Example in Java Enum
valueOf method of Java Enum is used to retrieve Enum constant declared in Enum Type by passing String in other words valueOf method is used to convert String to Enum constants. In this Java Enum valueOf example we will see how to use valueOf method in Java. valueOf method is implicitly available to all Java Enum because every enum in Java implicitly extends java.lang.Enum class. valueOf method of enum accepts exactly same String which is used to declare Enum constant to return that Enum constant. valueOf method is case-sensitive and invalid String will result in IllegalArgumentException. In short String passed to valueOf method of Java enum must be same to String returned by name() method e.g. returns RED and you should pass RED to valueOf() to get TrafficSignal.RED. This will be more clear will following Java Enum valueOf example. By the way its third tutorial on Enum in Java after previous post on Java Enum Swith Example  and Java Enum toString example. If you have not read those tutorial you may find useful.

JDOM Example : Reading and Parsing XML with SAX parser in Java

XML parsing with JDOM parser
JDOM is an open source library which allow XML parsing and reading in Java program. JDOM is designed by using Java programming technique and customized for Java programmers, so that Java programmer with  very little knowledge of XML documents can use JDOM to read XML files. Unlike DOM Parser of Java API , which uses Factory design pattern to create instance of parser e.g DocumentBuilderFactory and DocumentBuilder, as seen in our last example of parsing XML documents in Java, JDOM uses new() operator to create its parser instances. In fact JDOM is very easy to understand and most of the time its self explanatory. While trying this Java program to parse XML file, you can explore JDOM API. JDOM API also allows to use XPATH expression to query XML documents with package org.jdom2.xpath. In next section we will see complete code example of How to read an XML file using JDOM parser in Java program.

How to Search Element in Java Array with Example

find an index of the object in Array
While programming in Java, many times we need to check if a String array contains a particular String or an int array contains a number or not. Though array is an object in Java but it does not provide any convenient method to perform these kinds of operation. Sometimes you even need an index of an item stored in Array, unfortunately, Java API does not provide any direct method. Thanks to open Source Apache Commons provides a utility class called ArrayUtils which allows you to check for an Item in Array, find its index in the array, find its lastIndex in Array and perform several other common operations. In fact, ArrayUtils has overloaded methods for different kind of Array e.g. String, int, float, double, Object etc, which makes this kind of programming task trivial. In this article, we will see an Apache commons example of How to use ArrayUtils class to search an item in Array and find its index.

How to fix java.lang.unsupportedclassversionerror unsupported major.minor version 49.0 50.0 51.0 in Java

java.lang.unsupportedclassversionerror unsupported major.minor version 50.0 error comes in Java environment when you compile your Java file in a higher Java version e.g. Java 1.6 and then trying to run same Java program in lower Java version e.g. Java 1.5. Java is backward compatible i.e. you can run your Java 5 binary or JAR into Java or JRE 6 but vice-versa is not true. You can not run Java program which is compiled in higher Java version into lower JRE. class file version changes between every Java version and class file generated by javac command of Java 6 installation is not appropriate  for JRE of Java 5 Installation. In order to solve java.lang.unsupportedclassversionerror unsupported major.minor version 49.0 , 50.0 or 51.0 we need to first understand What is major and minor version of class file in Java. Every Java release has a major source version as shown in this table

How to sort Object in Java - Comparator and Comparable Example

Java Object Sorting Example
How do you sort a list of Objects in Java is one of the frequently asked coding questions in Java interviews and surprisingly not every Java programmers know How sorting of object happens in Java. Comparator and Comparable interface along with Collections.sort() method are used to sort the list of object in Java. compare() and compareTo() method of Comparator and Comparable interface provides comparison logic needed for sorting objects. compareTo() method is used to provide Object's natural order sorting and compare() method is used to sort Object with any arbitrary field. Almost all value classes in Java library e.g. String, Integer, Double, BigDecimal implement compareTo() to specify their natural sorting order. While overriding compareTo method String is sorted lexicographically and Integers are sorted numerically. Just beware that it must inconsistent with equals method i.e. two objects which are equal by equals method in Java, compareTo() method must return zero for them.  Anyway,  sorting standard value Object is not a problem for many Java programmer but some of them really struggle when it comes to sorting custom Objects or domain Objects. In this Java sorting tutorial, we will create a custom object and sort list of Object in ascending and descending order.

Difference between Primary key vs Candidate Key in table - SQL database

Primary key vs Candidate Key
What is the difference between primary key and candidate key is another popular SQL and database interview questions which appears in various programming interviews now and then? The concept of primary key and candidate key is not just important from interview point of view but also on designing database and normalization. By the way, this is my second post about primary keys, In last one, we have seen a comparison of primary key vs unique key, which also happens to be one of the frequently asked database questions. By definition primary key is a column or collection of columns, which uniquely define a row in a table. Candidate keys are keys which can be a primary key and also able to uniquely identify any row in the table.

What is difference between Clustered and Non-Clustered Indexes in SQL - Database

Clustered vs Nonclustered Indexes in SQL
The difference between Clustered and Nonclustered index in a relational database is one of the most popular SQL interview questions almost as popular as the difference between truncate and deleteprimary key vs unique key and correlated vs noncorrelated subqueries. Indexes are a very important concept, it makes your queries run fast and if you compare a SELECT query which uses an indexed column to one who doesn't you will see a big difference in performance. There can be two kinds of indexes in relational database Clustered and Nonclustered indexes. A clustered index determines physical sorting order of rows in a table similar to entries on yellow pages which are sorted in alphabetical order.

What is difference between primary key and unique key in table - SQL database

primary key vs unique key in SQL
The primary key and unique key are two important concepts in a relational database and used to uniquely identify a row in a table. Both primary key and unique key can identify a row uniquely but there is some subtle difference between them which we will see in this article. In fact, primary key vs unique is a popular SQL interview questions along with classics like truncate vs delete and  How to manage transaction in a database, mostly asked to fresher and 2 to 3 years experience guys in any programming language. SQL is not just limited to any DBA or PLSQL developer but its an important skill even for Java programmer and you can expect SQL interview question even in many Java interviews.

Difference between Serializable vs Externalizable in Java - Interview Question

Serializable vs Externalizable
What is difference between Serializable and Externalizable in Java is famous core Java interview questions and for some of them its one of those difficult Java question, which no one want to see in Java interview. I was on that category until I explored How Serialization works in Java and find out more about Serialization process. What makes Serialization questions tricky is , Serialization as persistence mechanism is not very popular. Many programmer prefer database, memory mapped file or simple text file over Serialization. But Serialization has a distinguished advantage over these mechanism. Java provides default mechanism to preserver object state and your job many time would be as simple as adding implements Serializable on any class.In this article we will compare Serializable to Externalizable and see some common difference between Serializable vs Externalizable to tackle this Java question.

Java program to calculate Simple Interest - homework exercise

How to calculate simple interest in Java or Write a Java program to calculate Simple interest is a popular Java programming exercise people use in Java course in school and college. So if you have recently got an homework exercise about calculating Simple interest in Java and wondering How to write Java program than this tutorial can help you, provided you know What is simple interest and What is formula of simple interest. If you are completely new in Java programming than I would suggest to try HelloWorld Example in Java and understand What is Path and Classpath in Java. IF you have been doing Java programming and looking for some good coding exercise than you can also see our earlier programs e.g. calculating Fibonacci series using recursion, checking if number is palindrome and How to reverse String in Java using recursion.

Could not create the Java virtual machine Invalid maximum heap size: -Xmx

"Could not create the Java virtual machine" is a general Java Virtual Machine error when you run java command directly or indirectly and its not able to create virtual machine because of invalid maximum heap size, invalid minimum heap size or just any error in command line. This error not only come when you run Java program from command line but also when you run them using any IDE like Eclipse or Netbeans. Could not create the Java virtual machine invalid maximum heap size -Xmx is a special case for it which comes when you try to run your java command with -Xmx option and value of that option is invalid like syntax error, value error or anything else. In this Java article we will see couple of scenarios when you get "Could not create the Java virtual machine Invalid maximum heap size: -Xmx" which will help you to avoid those error and fix this problem.

String contains() and indexOf() Example - Check String for SubString

Some time you want to check if one String contains another String or SubString or not e.g. "HelloWorld" contains "Hello" and "World" SubString. There are many ways to check SubString in String in Java e.g. contains(), indexOf() or matches() method of java.lang.String class. In our last article we have seen how to use matches method of String class with regular expression and In this article we will see example of contains() and indexOf() method to check any String or SubString in Java. contains and indexOf method are defined in java.lang.String class and used to check if String contains a particular SubString or not, only difference between contains() and indexOf() method is that, contains is added in Java 5 along with String matches() method while indexOf() is as old as equals(), hashCode and compareTo(). Another difference between contains() and indexOf() method is that contains() return boolean value e.g. true if String contains SubString and false if String doesn't contains SubString but indexOf() return int value, which is actually index of SubString i.e. starting location. indexOf() return -1 if source String does not contain SubString in Java.

How to escape HTML Special characters in JSP and Java

Escaping HTML special characters in JSP or Java is a common task for Java programmers. There are many ways to escape HTML meta characters in Java, some of them we have already seen in last article escaping XML metacharacters in Java.  For those who are not familiar with HTML special characters, there are five e.g. < , >, &, ' and '' and if you want to print them literally just like here, Than you need to escape those character so < becomes &lt; , > becomes &gt; and so on. Of course, you can write your own custom tag or method for converting HTML special characters to entity format which browser understand but you don't need to do this because there are more easy and standard way to escape HTML special characters in JSP and Java. In this JSP and Java tutorial, we will learn about HTML special characters and explore some techniques to escape them in JSP pages and Java code. By the way, this is also a popular JSP Interview question mostly asked 2 years experience programmers.

Top 10 Servlet JSP Interview Questions for Java J2EE programmer - FAQ

Servlets and JSP Interview Questions are core of any J2EE Interview. Its expected from candidate to have good knowledge of Servlet, JSP, EJB , Struts or Spring in any J2EE interview. Consequently Servlets and JSP Interview Questions are very common in any J2EE Interviews along with some questions from core Java, Some interview Questions from Spring and Some EJB interview questions. Since J2EE is main platform for building enterprise and web application using Java programming language, its important to prepare well for J2EE interview, especially If you are in application development space. Servlet and JSP forms web layer of J2EE framework where Java Server pages is main view technology and Servlets are main Controller component of standard MVC architecture. In this article we will see couple of most common or frequently asked JSP and Servlet Interview questions and answers, which can help you on your J2EE interview preparation and understanding some key concept of Java web applications.

Difference between throw vs throws in Java

throw vs throws in Java
throw vs throws in Java programming Exceptionthrow and throws are two Java keyword related to Exception feature of Java programming language. If you are writing Java program and familiar with What is Exception in Java, its good chance that you are aware of What is throw and throws in Java. In this Java tutorial, we will compare throw vs throws and see some worth noting difference between throw and throws in Java. Exception handling is an important part of Java programming language which enables you to write robust programs. There are five keywords related to Exception handling in Java e.g. try, catch, finally, throw and throws. Apart from the difference between final, finally and finalize,  throw vs throws is one of the frequently asked Java interview questions. Difference listed here will not only help you to answer this interview question but also understand concept behind throw and throws better.

Java Interview Questions for 2 to 3 years experience - Answered

Java Interview Questions 2 to 4 years experienced
If you are a Java programmer with 2 to 3 years experience of working in Java and looking for job change then its better to know what is expected from a 2 to 3 years experienced Java programmer in a typical core Java or J2EE interview. Since 2 to 3 years is not a huge experience and you still fall under beginner to intermediate category , its not expected from you to have done profiling, Garbage collection tuning or designing complex Java applications or even creating concurrent Java design pattern. What is expected from a 2 to 4 years experience guy in Java programming is sound knowledge of Java fundamental including OOPS concepts like Abstraction, Encapsulation, Polymorphism, Composition and Inheritance, good knowledge of Java collection API and familiarity with different kinds of collection e.g. List, Set and Map with some familiarity with popular Collection classes e.g. ArrayList, HashMap, Vector and HashSet etc. Fundamental knowledge of Java threading API, wait-notify mechanism and Exception handling in Java is also something Interviewer look in a intermediate Java programmer and of-course some basic programming and coding exercise e.g. finding length of linked List in one pass or reversing String in Java etc. If you manage to prepare these topics well, you are most likely to do well in Java interviews on service based companies e.g. Tech Mahindra, CapeGemini, CTS, Wipro, TCS etc.

How to iterate over HashSet in Java - loop or traverse Example

Iterating over HashSet in Java
Java program to Iterate over HashSet in Java with ExampleIn our last Java collection tutorial, we have seen How to iterate over ArrayList in Java and in this tutorial we will see How to iterate over HashSet in Java. There are two ways to iterate, loop or traverse over HashSet in Java, first using advanced for-each loop added on Java 5 and second using Iterator which is a more conventional way of iterating over HashSet in Java. Now questions are When should you use for loop and when Iterator is an appropriate option. Well I usually use for loop If I only read from HashSet and doesn't remove any element, while Iterator is preferred approach. You should not remove elements from HashSet while iterating over it in for loop, Use Iterator to do that.

How to convert Hexadecimal to Decimal, Binary and Octal in Java program - Example

Hexadecimal to decimal and binary conversion in Java
This Java program will convert Hexadecimal number to decimal, binary and Octal in Java programming language using JDK standard API methods. For beginners hexadecimal is base 16 number, while decimal is base 10, Octal is base 8 and binary is base 2 numbers in Number systems. Binary only contains 0 and 1 bits, octal contains 0 to 7, decimal contains 0 to 9 and Hexadecimal contains 0-9,A,B,C,D,E,F where F represent 16. Thankfully Java library provides convenient method to convert any integer from one number system to another. In our last article we have seen How to convert decimal number to binary in Java and in this article we will convert Hexadecimal numbers to binary, octal and decimal numbers.If you are going for programming interviews, then its better to prepare How to do this exercise without using Java API as well, as interviewer commonly expect programmer to create there own method during interviews e.g. reversing String without using StringBuffer reverse() method.

How to convert ArrayList to String in Java - Spring Example

Some time we need to convert ArrayList to String in Java programming language in order to pass that String to stored procedure, any method or any other program. Unfortunately Java collection framework doesn't provide any direct utility method to convert ArrayList to String in Java. But Spring framework which is famous for dependency Injection and its IOC container also provides API with common utilities like method to convert Collection to String in Java. You can convert ArrayList to String using Spring Framework's StringUtils class. StringUtils class provide three methods to convert any collection e.g. ArrayList to String in Java, as shown below:

How to find swap space and usage in Solaris - UNIX command

Swap space in Solaris or any UNIX host is an important disk space which is used for swapping process from physical memory (RAM)  to disk. Virtual memory allowed a process to run even if physical memory gets full by using swap space (which is located on your hard disk) to swap out memory pages which are not currently in use. In this UNIX command tutorial, we will see some Solaris commands to find configured and current usage of swap space. Remember if your machine runs out of swap space then you can not start a new process and also an old process which will try to expand themselves will likely to fail. 

UNIX command to find symbolic link or soft link in Linux

There are two ways you can find a symbolic link or soft link in UNIX based operating system e.g. Linux, Solaris, BSD or IBM AIX. The first way is by using ls command in UNIX which displays files, directories, and links in any directory and other way is by using UNIX find command which has the ability to search any kind of files e.g. file, directory or link. In this UNIX command tutorial, we will see examples both of these UNIX commands for finding a soft link in any directory. If you are new to UNIX operating system and not familiar with the concept of the soft link and hard link, I would recommend getting a good hand on it, as it is one of the most powerful features of UNIX based system.

Difference between static and non static nested class in Java - Inner class

Static vs non Static class in Java
In Java you can make a class either static or non static. Now what is difference between making a class static vs non static? well there is lot of difference between them. First of all there are two kinds of class in Java, one is called top level class and other is called nested class. As name suggested top level class is a class which is declared in .java file and not enclosed under any other class. On other hand nested class is declared inside another class. The class which enclosed nested class is known as Outer class. Now let's come back to static vs non static class. In Java programming language you can not make a top level class static. You can only make nested class either static or non static. If you make a nested class non static then it also referred as Inner class. Now let's come to the point regarding Difference between static and non static nested class in Java

20 EJB 3.0 Interview Questions and Answers - Java J2EE

EJB interview questions are core part of any Java J2EE interview. As EJB forms business layer for modern J2EE enterprise application, Good knowledge of EJB is expected from J2EE programmer. Purpose of these EJB interview questions is to give an Idea about what kind of questions you can expect on J2EE and EJB interviews. EJB was always tough for Java programmer because of heavy weight architecture comprised with many interfaces e.g. home interface, remote interface, local interface, bean class etc. It take too much time and knowledge to implement and use EJB in your Java web and enterprise application forget about challenges posed by application servers like WebLogic or IBM WebSphere. All these problem persist till EJB 2.0 which makes EJB infamous along with J2EE and created path for Spring framework which is based on POJO. But with EJB 3.0, Enterprise Java Beans are back again and they are back with bang. In this article I will share some EJB Interview questions from my collection. These EJB questions are from EJB 2.0 and EJB 3.0 and also related to fundamental concept of using Enterprise Java beans. By the way I don't have answer of these Enterprise Java beans questions but I will update the post once I have answers of all these interview questions asked on EJB interviews. This article is divided in two category, in first category we will see some common EJB interview questions and in second category we will see EJB 3.0 interview question, which is more popular now days. I have seen EJB question on companies like Capegemini, TCS, CTS, Infosys and Wipro and mostly on finance domain projects

Java program to read File in Java - Windows UNIX

Java provides  package in JDK API for reading File in Java from File system e.g. C:\ or D:\ drive in Windows or any other directory in UNIX. First, you can use FileInputStream to open a file for reading. FileInputStream takes a String parameter which is a path for the file you want to read. Be careful while specifying File path as path separator is different on Window and UNIX. Windows uses back slash while UNIX uses forward slash as a path separator. By the way, Java supports forward slash in Windows, It means if use forward slash your program will work on both Windows and UNIX. Alternatively, you can also get PATH separator from System property path.separator. In this Java tutorial, we will code example of How to read File in Java.