Tag «Java»

Java String comparison. The difference between == and equals().

Many Java beginners find it difficult to differentiate between == operator and the “equals()” method when comparing String variables in Java. They assume that both operations perform the same function and either one can be used to compare two string variables. I have even seen many experienced programmers committing the mistake of using “==” to …

How to configure multiple handlers in a Spring MVC web application

In Spring MVC, DispatcherServlet relies on handler mapping to determine which controller the request should be sent to. All handler mapping classes in Spring implement org.springframework.web.servlet.HandlerMapping interface. Spring distribution contains following four implementation of HandlerMapping interface. BeanNameUrlHandlerMapping SimpleUrlHandlerMapping ControllerClassNameHandlerMappign CommonsPathMapHandlerMapping BeanNameUrlHandlerMapping is the simplest of all and DispatcherServlet looks for this mapping by default. You …

Import static elements for more readable Java code

One of the welcome additions to Java language in Java 5 release is the static import declaration. static import works in the same way as traditional import declaration but it imports only the static members of a class. Traditional import declaration looks like this import java.util.*; The above statement will import all the classes under …

How to read properties file in Java.

Reading properties file in Java is much easier than you might have thought. Following example illustrates one simple way of reading properties from a properties file. Let’s say we need to read from myConfig.properties file. The properties file has following entries. Directory = C:/prodFiles/ NumberOfFiles = 25 Extension = java Here is the java code …

How to validate email, SSN, phone number in Java using Regular expressions.

Email

Regular Expressions offer a concise and powerful search-and-replace mechanism. They are patterns of characters used to perform search, extract or replace operations on the given text. Regular expressions can also be used to validate that the input conforms to a given format. For example, we can use Regular Expression to check whether the user input …