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.

Difference between OCAJP and OCPJP Certification Exams

Earlier when Sun Microsystems was in charge of Java, the popular Java certifications were called "Sun Certified Java Programmer" or "SCJP" and that time there was just one exam, you need to pass to become a certified Java Developer, but when Oracle took over Sun Microsystems on 2010, the SCJP goes away and OCAJP and OCPJP born. Since Oracle already has his certifications for database administrations e.g. OCA which stands for Oracle certified associates and OCP, which stands for Oracle Certified Professional, it introduces new Java certifications to match their existing hierarchy and they are known as OCAJP and OCPJP in the Java world.

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.

Java 8 - Convert ArrayList to HashMap or LinkedHashMap - Example Tutorial

One of the common task in Java is to convert a List of object e.g. List<T> into a Map e.g. Map<K, V>, where K is some property of the object and V is the actual object. For example, suppose you have a List<Order> and you want to convert it into a Map e.g. Map<OrderId, Order>, how do you that? Well, the simplest way to achieve this is iterating over List and add each element to the Map by extracting keys and using the actual element as an object. This is exactly many of us do it in pre-Java 8 world but JDK 8 has made it even simpler. In Java 8, you can get the stream from List and then collect all elements into a Map by using a Collector. The collect() method of Stream class and class gives you ample choices to decide which property goes into the key and which object goes into the value.

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.

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.

Difference between first level and second level cache in Hibernate

The main difference between first level and second level cache in Hibernate is that the first level is maintained at the Session level and accessible only to the Session, while the second level cache is maintained at the SessionFactory level and available to all Sessions. This means, you can use the first level cache to store local data i.e. the data which is needed by the Session and you can use the second level cache to store global data i.e. something which can be shared across sessions. This is also one of the frequently asked Hibernate Interview questions and popular in both telephonic rounds as well as on face to face interview, in both fresher and experienced level interviews.

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.

5 Best Gifts for Programmers,Software Developers and Tech Geeks 2017 - 2018

So, its that time of the year again when you start thinking about gifts for your programmer boyfriend, husband, colleagues or your near dear ones. Even if selecting gifts were not tough, if your friend happened to be a programmer, it becomes slightly tougher. You might have heard that Programmers are aloof, nerd and tech freaks, well, you heard it right. Programmers  prefers to be alone in their own world but they also like gifts and you got it right, they are a nerd so they like books, gadgets, music and something which help them to learn something e.g. a PluralSight membership.

In 2017, gadgets like Apple Watch and Samsung Gear is rocking but that's what everyone is buying so if you want to be a little bit different, chose the one from this list.

How to test RESTful Web Services using curl command in Linux

The curl command of Linux is very powerful and versatile command which allows you to send sophisticated HTTP request right from Linux command line. You can use curl to test your RESTful Web Services by sending GET and POST request, doing authentication, saving a cookie in the file etc. The curl command is the go-to tool for many Java and Spring developers for consuming the secured RESTful Web Services, but you can also use it test your simple REST Web APIs without security.

3 ways to convert String to byte array in Java - Example Tutorial

Today, I am going to discuss one of the common tasks for programmers, converting a String to a byte array. You need to do that for multiple reasons e.g. for saving content to a file, sending over a network or maybe some other reason. Suppose you have a String "abcd" and you want to convert it into a byte array, how will you do that in a Java program? Remember, String is made of the char array, so it involves character to byte conversion, which is subject to character encoding intricacies. Thankfully, Java provides a convenient getBytes() method to convert String to byte array in Java, but unfortunately, many developers don't use it correctly. Almost 70% of the code I have reviewed uses getBytes() without character encoding, leaving it on the chance that platform's default character encoding will be same as of the source String.