How to use string in switch case in Jdk 7 with example

Have you ever feel that String should be used in switch cases as like int and char? JDK 7 has made an important enhancement in their support of String, now you can use String in switch and case statement, No doubt String is most widely used type in Java and in my opinion they should have made this enhancement long back when they provided support for enum in java and allowed enum to be used in switch statement. In this java tutorial, we will see how we can use String inside switch and case statement in JDK 7. This article is in continuation of my earlier post on JDK7 feature improved exception handling using multi-cache block in Java

string and switch in jdk7 example tutorial many times we want to switch based upon string output received from user or another part of program but before JDK7 we can't do it directly instead either we need to map those String to final integer constant or char constant to use them inside switch and case or you need to fall back on if-else statement which gets clumsy once number of cases getting increased. But now with jdk7, you can directly use String inside switch and case statement. Though it’s pretty straight forward feature let see an example of how to use String inside switch and case statement in JDK7.

Why Java does not support Operator overloading

Unlike C++, Java doesn't support operator overloading. Java doesn't provide freedom to programmer, to overload the standard arithmetic operators e.g. +, -, * and / etc. If you have worked previously in C++, than you know that, Java has left lot of feature supported in C++ e.g.  Java doesn't support multiple inheritance, no pointers in Java, and no pass by reference in Java. Rarely this question asked in Java interviews, to check how programmer thinks about certain features, which is not supported in Java. Another similar questions is regarding Java being pass by reference, which is mostly appear as, whether Java is pass by value or reference. Though I don't know the real reason behind it, I think following observation make sense on, why Operator overloading is not supported in Java.


1) Simplicity and Cleanliness
Why operator overloading is not supported in JavaSimple and clear design was one of the goals of Java designers. They, just don't want to replicate the language, but wanted to have a clear, truly object oriented language. Adding Operator overloading would have definitely made design more complex than without it, and it might have lead to more complex compiler or slows the JVM ,because it needs to do extra work to identify the actual meaning of operators, and reduce the opportunity to optimize the language by guarantee behavior of operators in Java.

2) Avoid Programming Errors
Java doesn't allow user defined operator overloading, because if you allow programmer to do operator overloading, they will come up with multiple meanings for same operator, which will make the learning curve of any developer hard and things more confusing and messy. Its been observed that, there is increase in programming errors, when language supports operator overloading, which in turn increase e development and delivery time. Since Java and JVM has taken most of developers responsibility,  in memory management by providing garbage collector, it doesn't really make sense to left this feature to pollute the code, and as a loop hole for programming errors.


3) JVM Complexity
From JVM perspective, supporting operator overloading is more difficult, and if the same thing can be achieved, by using method overloading in more intuitive and clean way, it does make sense to not support operator overloading in Java. A complex JVM, may result in slower JVM, than a relatively simpler JVM ,and reduce the opportunity of optimization by taking out guaranteed behavior of operators in Java.

4) Easy Development of Tools
This is an additional benefit of not supporting operator overloading in Java. Omission of operator overloading has kept the language easier to handle and process, which in turn makes it easier to develop the tools, that process the language e.g. IDE or re-factoring tool. Re-factoring tools in Java are far better than C++.

In conclusion many things, which can be achieved by operator overloading, can also be achieved using method overloading using more intuitive and easy way and that might be the reason java designer thought that supporting operator overloading will not be a big benefit for language, but in fact only Java designer can answer real motivation of, why Java doesn't support operator overloading, like some other questions as Why Java doesn't support multiple inheritance or Why String is immutable in Java.

5 example of sort command in UNIX or Linux >>>> Unix Tutorial

Sorting is one of the essential tasks we always need to perform and UNIX or Linux has great support for sorting by using sort command. No matter what kind of shell script you are writing or you are looking for some information or reports many times you need to sort the output of one command or a set of line, with a combination of the sort with find command and grep in UNIX you can handle support request and report quite easily. In this UNIX sort command tutorial, we will see how we can use sort command in UNIX to achieve our sorting goal. This tutorial contains some of the practical example of sort command for sorting data in UNIX. With the use of “unique” and “sort” command, you can remove duplicates and present a sorted data in UNIX.

How to resolve java.lang.ClassNotFoundException in Java

What is ClassNotFoundException in Java
ClassNotFoundException is one of Java nightmare every Java developer face in there day to day life. java.lang.NoClassDefFoundError and java.lang.ClassNotFoundException are two errors  which occurs by and now and chew up of your precious time while finding and fixing root cause. From the name java.lang.ClassNotFoundException looks quite simple but underlying cause of it is always different and which classifies it as an environmental issue. In this java tutorial we will see what is ClassNotFoundException in java, what is real cause of it and how to fix it along with some more frequent and infamous examples of java.lang.ClassNotFoundException in Java or J2EE, Don’t mistake this exception with NoClassDefFoundError in Java which is also due to incorrect classpath in Java.  Though both of them are related to missing class file when Java tries to load class in Java they are completely different to each other.  Correct understanding of  When class is loaded in Java and How Classpath works  is must to troubleshoot and fix this error quickly.

10 Best Practices to Follow while writing Code Comments

Comments are an important part of writing code not only in Java but whatever programming or scripting language you use. At the same time, this is one of the most abused things as well. Both writing no comment and writing too much comment is bad and this has been highlighted by many software gurus e.g. Robert C. Martin in his classic book Clean code. There is a whole chapter dedicated to How to write comments and finding pros and cons of comment. This article is my learning in the same direction, here I am going to share with you guys some 0f the rule and best practices I follow while writing comments. Before that let's first see what is the purpose of having a comment in the code? Why do we need comment, isn't writing code is enough. Some of the people I have met always argue that we are getting paid for writing code and not comment :).

What is polymorphism in Java? Method overloading or overriding?

What is Polymorphism in Java
Polymorphism is an important Object oriented concept and widely used in Java and other programming languages.  Polymorphism in java is supported along with other concepts like Abstraction, Encapsulation, and Inheritance. Few words on the historical side; Polymorphism word comes from ancient Greek where poly means much so polymorphic are something which can take many forms. In this Java Polymorphism tutorial we will see what is Polymorphism in Java , How Polymorphism has implemented in Java e.g method overloading and overriding, why should we use Polymorphism and how can we take advantage of polymorphism while writing code in Java. Along the way we will also see a real world example of using Polymorphism in Java

Java Enum Tutorial: 10 Examples of Enum in Java

What is Enum in Java
Enum in Java is a keyword, a feature which is used to represent fixed number of well-known values in Java, For example, Number of days in Week, Number of planets in Solar system etc. Enumeration (Enum) in Java was introduced in JDK 1.5 and it is one of my favorite features of J2SE 5 among Autoboxing and unboxing , Generics, varargs and static import. One of the common use of Enum which emerged in recent years is Using Enum to write Singleton in Java, which is by far easiest way to implement Singleton and handles several issues related to thread-safety and Serialization automatically. By the way, Java Enum as a type is more suitable to represent well known fixed set of things and state,  for example representing the state of Order as NEW, PARTIAL FILL, FILL or CLOSED.

How to view Javadoc in Netbeans IDE 7.0

Recently I have downloaded Netbeans IDE 7.0 to learn its GUI builder Matisse and I was impressed with the changes I saw in the Netbeans from  my last use of it on 2007. I was a hardcore Netbeans fan when I was developing mobile application using J2ME and Netbeans integrated environment with WTK, Profiler and Network Monitor just rocks and on top of that they had conditional compilation feature based on type of platform you   were working like you could maintain your code easily even if you are using device specific libraries e.g. Nokia UI library etc. Anyway, those were the things of past, what attracted me is the Javadoc feature of Netbeans. Though this feature might have existed long back I have just started using it. Earlier to see Javadoc I used to Google and then go to Sun (now Oracle) website to view Javadoc of any class. While playing with Netbeans I discovered that you can easily Javadoc in the Netbeans IDE itself no need to visit the web.

Java swing tutorial: Learning JList with code example

In this java swing JList tutorial, we will learn important properties and function of JList while working on a task with examples. In my opinion best way to learn swing is to actually doing the task and so I designed this tutorial around the task. We will start with a simple example and proceed with more  complex one step by step adding new functionality and in the course, we will learn JList both concept and API. In this first example, we need to implement a list which shows names of companies in sorted order. 
Before start coding let's see some fundamentals of JList:

How to Convert String to Integer to String in Java with Example

Converting String to integer and Integer to String is one of the basic tasks of Java and most people learned about it when they learn Java programming. Even though String to integer and Integer to String conversion is basic stuff but same time its most useful also because of its frequent need given that String and Integer are two most widely used type in all sort of program and you often gets data between any of these formats. One of the common tasks of programming is converting one data type another e.g. Converting Enum to String or Converting Double to String, Which is similar to converting String to Integer. Some programmers asked a question that why not Autoboxing can be used to Convert String to int primitive or Integer Object? 

How to increase java heap space on Maven and ANT

Many times we get java.lang.OutOfMemoryError: java heap space while building our project either by using maven or ANT just because heap size is not enough to build the project. At this point we just want to increase the heap size used by ANT or MAVEN and this java tip will let you do this:

How to increase Java Heap size for Maven on Linux/Unix

1. Open your mvn.sh file and add the following line:
export MAVEN_OPTS=-Xmx512m
this will allow to specify java heap space based on your project needs.