Tuesday, December 17, 2013
Switch Statements are not new for any Programmer, it is available in C, C++, Java and in all major programming language. Switch statement in Java allows you to a clear, concise and efficient multiple-branch statement without lots and lots of messy if-else statement. But Java Switch and case statement has limitation, you cannot use String in them. Since String is one of the most used class in Java, and almost every program, starting from Hello World to complex multi-tier Java application uses them, it makes lot of sense to allow them in Switch case. In Java 6 and before, the values for the cases could only be constants of integral type e.g. byte, char, short, int and enum constants. If you consider Autoboxing then you can also corresponding wrapper class e.g. Byte, Character, Short and Integer. From Java 1.7, language has been extended to allow String type in switch and case statement as well. This makes, Java programmers life a bit easier, as it no more has to map String to Integers, to use them inside switch constructs. In this Java 1.7 tutorial, we will learn How to use Strings in switch in Java. Like many Project Coin enhancements, e.g. underscore in numeric literals, Automatic resource management, this is really a very simple change to make life in Java 7 easier. This is not a JVM level change, instead it is implemented as syntactic sugar. In all other respects, the switch statement remains the same, and internally it uses equals and hashcode method to make it work.
Saturday, November 23, 2013
Every Java programmer loves free eBooks on Java, don't you? When I shared my collection of top 5 Java programming books, one of my reader asked me to share some free Java books as well. Doing a quick search on internet, reveals lots of free books, resource and tutorials to learn Java. I have chosen some of the good Java books, which are FREE, available for download or you can read it online in HTML or PDF format. This books are excellent resource for any Java beginners, as well as experienced programmer, and since they are free, it makes absolute sense to have a look on this before buying any other book in Java. This free Java books covers a wide range of technology including core Java, J2EE, JSP, Servlets, XML and general programming concepts. Though books like Effective Java or Java Concurrency in Practice are not free, they are worth of every penny spent. I didn't find any good FREE Java books on concurrency and multithreading, which I really wanted to include. If you come across a genuine FREE multithreading books for Java programmer, then please let us know.
Tuesday, November 19, 2013
In this article, we will take on a popular programming exercise of counting vowels in a word. You need to write a Java program to count how many vowels in a String, which is entered from command prompt. It's similar to program of counting occurrence of characters in a String, in fact it's a special case, where you need to count occurrences of all vowels, which includes five characters a, e, i, o and u. We will further use Scanner to get input from user, as shown in this article. Though I have put down all code inside main method for quick testing, if you are asked to write this program as part of your homework or during interview, better writing a method called public int countVowels(String word) and put logic of counting vowels there. That's a better coding style than writing everything inside main method. By the way, you can also use this logic to count number of consonants in a Java String. What you need to do is first count number of vowels and then subtract those characters from length of String, but remember this will only work if your String contains only alphabetic words, if it contains special character like @, _, | or numbers like 1,2,3 etc, than it will not work. In that case you need to extend your logic to only count consonants, by extending your switch case to include rest of 21 characters from English alphabets.