10 Programming Tips to Create Maintainable Java Applications

Hello all, today is 31st December, the last day of 2016 and this is probably the last post on Javarevisited for this year and I want to make it special. That's why I am going to share you some tips on creating maintainable Java applications which will help you not just in the new year, but also in all the coming years in your software development career.  One aspect of development, which is often overlooked by developers is to create applications which are both easy to maintain and support. Since a software spends 90% of its lifetime in maintenance mode, it's very important that your application is easy to configure, support, and maintain. Also, Java application is no different than any other software, you must pay attention to these key properties of writing production quality code, hence this tips also applies to any programming language which is used to create real-world software which is used by real users.

How to loop over HashMap in JSP using JSTL

Though there are a number of ways to loop over an HashMap in JSP, or any other Map implementation e.g. Hashtable, I personally prefer the JSTL foreach tag for iteration over HashMap in JSP. As a Java programmer, you will often have a strong urge to use Java code directly in JSP using scriptlet, but that's a bad coding practice and one should always avoid that. In fact, by smart use of expression language and JSTL core tag library, you can reduce a lot of Java code from your JSP page. In our last post, we have seen an example of JSTL foreach tag to loop over List, but not an  HashMap, and that creates a doubt in one of my readers mind that foreach tag doesn't support Map implementation like HashMap or Hashtable as they are not Collection, but that's not true. You can use the same technique to loop over an HashMap in JSP which we have used earlier to loop over a list in JSP.

How to increase Heap memory of Apache Tomcat Server

Increasing Heap size of Tomcat
You can increase heap size of Tomcat by setting JAVA_OPTS or CATALINA_OPTS at the top of the catalina.sh, file if you are running Tomcat in Linux and by putting JAVA_OPTS or CATALINA_OPTS into the catalina.bat file if you are running Tomcat in Windows. This approach will work irrespective of tomcat version i.e. you can increase the heap memory of tomcat 5.5, tomcat 6, tomcat 7 and even tomcat 8 by using this technique. You can find the catalina.sh and catalina.bat file in the bin directory of tomcat installation folder e.g. in my machine catalina.sh and the catalina.bat file is located under C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.41\bin directory. If you open the catalina.bat property you will see the advice given by Tomcat creator itself on how to give additional JVM options to Tomcat process.

Top 10 Java Swing Interview Questions Answers asked in Investment banks

The Swing  API and toolkit are heavily used in developing trading application front end in Java on both large Investment banks and small broker firms. Doesn’t matter whether its front office or middle office application, you will find Java Swing GUI everywhere. The Swing-based GUI is used to develop Order entry system, Order monitoring GUI and for other tools which trader or operations can use on different trade life cycle in the front office. middle office and back office space. Due to its heavy usage, there are a lot of requirements of Java Swing developer in investment banks like Barclays, Citibank, Deutsche Bank, Nomura, JP Morgan, Morgan Stanley and Goldman Sachs etc. Though in the era of Java 8, Java FX is positioned to take over from Swing but there are still a lot of legacy application which means the requirements for Java Swing developers will not dry out soon.

Exception in thread "main" java.lang.NoClassDefFoundError: Running Java from Command line

The "Exception in thread "main" java.lang.NoClassDefFoundError: helloworldapp/HelloWorldApp" error comes when you are trying to run the HelloWorldApp Java program from the command line but either .class file is not there or Java is not able to find the class file due to incorrect classpath settings. The name of the class could be different in each case, it depends upon which class you are passing to java command for running from the command prompt. Another interesting thing to remember is that this error only comes in Java version less than or equal to Java 6 e.g. Java 1.5 or Java 1.4, if you are running in JDK 7 or Java 8 instead of this you will see "Error: could not able to find or load class HelloWorldApp". Technically, both errors come due to same reason and their solution is also exactly same.

Spring Hello World Example using XML Config

In this Spring framework tutorial, you will learn how to write the hello world example in Spring framework. This should be the first tutorial to start learning Spring framework, as it gets the ball rolling. While coding and running this example, you learn a lot about Spring framework, Spring XSD files, necessary JAR files, and more importantly how Spring framework works.This HelloWorld program in Spring framework is an extension of classical Java hello world program, written using dependency Injection design pattern by using Spring Framework's IOC container.  Even though now you can configure Spring dependency using annotations and Java configuration, this example uses traditional XML way to configure dependency.

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

If you are working in Java web or enterprise application which connect to any other web server using HTTPs you might have seen the "javax.net.ssl.SSLHandshakeException". This is one of the particular case of that error. If you know how SSL and HTTPS works that when a Java client connect to a Java server the SSL handshake happens. In this steps server return certificates to confirm it's identity, which then client validates against the root certificate he has in its truststore. If Server return a certificate which cannot be validated against the certificates a browser or Java client holds in its truststore then it throws the "sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target".

5 Tips to Prepare for Oracle Java Certifications (OCAJP and OCPJP)

In this article, I am going to share you some tips to prepare for the Oracle Java certification like OCAJP and OCPJP, both associate and programmer level certification. If you are completely new to Java then you need to first read Head first Java and learn how to program in Java, because getting certified without knowing how to program in Java is of no use. Any certification or degree is only valuable if you know the subject well and back them up with your knowledge, work, and experience. It's ideal for Programmer who has some Java experience, not necessarily work experience but you should have coded a good number of Java programs in your college days, internship or programming courses. Ok, now, let's move to the main topic, how to prepare for the Oracle Java certifications.

How to convert java.util.Date to java.sql.Timestamp?

You can convert a java.util.Date to java.sql.Timestamp value by using the getTime() method of Date class. This method return the long millisecond value from Epoch (1st January 1970 midnight) which you can pass to java.sql.Timestamp to create a new instance of Timestamp object in JDBC. Remember, java.sql.TimeStamp class is a wrapper around java.util.Date to allow JDBC to view it as SQL TIMESTAMP value. Only way to create a Timestamp instance is by passing the long time value because the second constructor of Timestamp class, which accepts individual fields e.g. year, month, date, hour, minute, second and nano is deprecated. Timestamp class can also hold up-to nano second value. 

Difference between a Thread and an Executor in Java

Even though both Thread and Executor, both are used to executed some code in parallel, there are some key differences between them.The main difference between a Thread and an Executor in Java is that later provides a thread pool in Java. Along with several concurrency utilities like CountDownLatch, CyclicBarrier, Semaphore, FutureTask, Callable interface, and Conditions, JDK 5 also introduced built-in thread pool, which provides set of working threads to run your code in parallel. Since creating, starting, and running a thread is a time-consuming and expensive operation, many Java applications create a pool of thread at start-up and leverage that for executing the task in parallel until Java introduced the built-in thread pool.  This thread-pool is known as Executor framework which relieved Java application developers from the responsibility of creating and managing threads

How to Count Number of Leaf Nodes in Binary Tree - Java Iterative and Recursive Algorithm

You can use the same algorithm to count a number of leaf nodes in the binary tree which we have used in the last article, while printing all leaf nodes of a binary tree in Java, using both recursion and iteration. The logic is same for leaf node, any node whose left and right children is null is known as leaf node in binary tree. They are the nodes which resides in the last level of binary tree and they don't have any children. In order to count total number of leaf nodes in binary tree, you need to traverse the tree and increase the count variable whenever you see a leaf node. Since binary tree is an essential data structure and algorithm topics for programming interviews, its better to prepare these kind of questions. I'll show how to solve this using both recursion and iteration in Java in this article.

3 Best Gifts for Programmers and Developers in Christmas and New Year

Happy New year 2017 and Seasons greetings to everyone. May god bless you with success and whatever you want this year. This is festival time and I have a couple of friends asking me what to gift to a programmer friend or colleague? This is a common confusion you face when buying a gift for tech geeks, software engineers, programmers, and web developers. Everybody wants that their gift is well received and make the person excited and happy. Being a Java programmer myself, I am going to suggest a couple of things which I have recently bought for my programmer friends, colleagues, and relatives. If you are in doubt, you can take some inspiration from my list. At times, I have really spent a good amount of time searching for gifts for fellow programmers and I know, we'll keep doing it, so expect this list to grow.

Is "Java Concurrency in Practice" still valid in era of Java 8?

One of my reader Shobhit asked this question on my blog post about 12 must read advanced Java books for intermediate programmers - part1. I really like the question and thought that many Java programmers might have the same doubt whenever someone recommends them to read Java concurrency in Practice. When this book came first in 2006, Java world was still not sure of about new concurrency changes made in Java 1.5, I think the first big attempt to improve Java's built-in support for multi-threading and concurrency. Many Java programmers were even not aware of new tools introduced in the API e.g. CountDownLatch, CyclicBarrier, ConcurrentHashMap and much more. The book offered them the seamless introduction of those tools and how they can use them to write high-performance concurrent Java applications.

How to Print Pyramid Pattern of Alphabets in Java program

In earlier programming tutorials, I have taught you how to print pyramid pattern of stars and numbers in Java, and in this tutorial, you will learn printing pyramid pattern of alphabets. If you understand the logic of previous programs then this one won't be difficult for you because we will use the same logic of printing rows and columns using nested loop in Java. Actually, pyramid pattern is nothing but a matrix where you need to print rows and columns. Though, you need to learn where to print those values and when to move to next row. Once you know this trick of advancing, you can print any kind of pyramid pattern in Java. The one, we'll see in this tutorial is the simplest of one but I'll give you some tough one for exercise to develop your creativity and coding skill.

Integer vs floating point arithmetic - Java Coding Question

I am starting a new series called Java Coding Quiz, in which I'll show you subtle Java concepts hidden in the code. This is an OCAJP or OCPJP style question but focused on teaching subtle details of Java programming language. In today's puzzle, you will learn about one of the key concepts about how floating point and integer arithmetic works in Java. This is a very important concept for any Java developer because Java behaves differently when the same operation is performed by different types of variable but of the same value.

Top 5 Spring and Hibernate Training Courses for Java JEE Programmers

Spring and Hibernate are two of the hottest and most in-demand web frameworks in Java world and also two of the most essential skill for any Java programmer to get a Job in web development space. The Spring framework is in around from 2004 and established itself as the leading framework to develop Java applications both in core Java and web development area. The Spring framework comes with a lot of modules to support a different kind of developments e.g Spring Security to address the security requirements of most web and enterprise Java applications. It supports single sign-on, LDAP authentication, Role-based access control, and much more such essential features. Spring also supports the development of REST services which is no become standard way to provide web services.

Difference between Executor Framework and ForkJoinPool in Java?

Java 5 added Executor Framework to provide out-of-box thread pool to Java programmers and Java 7 added ForkJoinPool an implementation of ExecutorService which specifically designed to execute ForkJoinTask. The Executor Framework provides several classes e.g. Executor, ExecutorService, and Executors for execution and creating thread pools. It also provides several built-in, ready to use thread pools like a pool of fixed threads, cached thread pool which can expand itself, spawn new threads if required due to heavy load.