Best way to Convert Integer to String in Java with Example

Integer to String conversion in Java
There are many ways to convert an Integer to String in Java e.g. by using Integer.toString(int) or by using String.valueOf(int), or by using new Integer(int).toString(), or by using String.format() method, or by using DecimalFormat, String concatenation, or by using StringBuilder and StringBuffer etc. Some of them we have already seen on my earlier posts e.g. how to convert int to String and converting double to String. Even though those posts gives you tricks to convert primitive int to String and primitive double to String, you can still use those tricks to convert a wrapper class object e.g. Integer or Double to String in Java. Both are good ways, but out of those which is the best way to convert an Integer to String in Java?  You will find out in this article.

Top 5 Reasons of Exception in thread "main" java.lang.NoClassDefFoundError in Java

NoClassDefFoundError in Java
The Exception in thread "main" java.lang.NoClassDefFoundError is a common error in Java which occurs if a ClassLoader is not able to find a particular class in the classpath while trying to load it. The Exception in thread "main" suggests that this error has occurred in the main thread, the thread which is responsible for running Java application. This error can occur to any thread but if it happens in main thread then your program will crash. As per Javadoc, NoClassDefFoundError can be thrown during linking or loading of the class file. It's denoted by java.lang.NoClassDefFoundError and comes when that particular class is present during compile time but somehow not available during runtime. This could be due to missing JAR file, any permission issue, or incorrect classpath on runtime, depending upon your environment.

How to create a ZIP File in Java? ZipEntry and ZipOutputStream Compression Example

Since compressing and archiving old log file is an essential housekeeping job in any Java application environment, a Java programmer should know how to compress files in .zip format and then how to read them programmatically if required. The JDK provides full support to create and read ZIP files in Java. There is a separate package java.util.zip to hold all classes related zipping and unzipping files and streams. In this series of article, you will learn how to use those classes e.g. ZipFile, ZipEntry, ZipInputStream, and ZipOutputStream etc. This is the second article about how to work with compressed archives in Java e.g. .zip files. In the last article, I have shown you how to read ZIP archives in Java and today, I'll teach you how to compress files in the ZIP file format by yourself using a Java program. You will compress a bunch of text file to create a .zip file by using JDK's ZIP file support classes.

Java Enum FAQ - What Every Java Developer should know about Enumeration type

Java Enum was one of the best features added on JDK 1.5 Tiger release along with Generics, Autoboxing, and varargs. Enum in Java represents fixed number of well-known things e.g. number of days in Week, the number of months in the calendar year etc. Unlike C and C++ Enum in Java are much powerful and they are not an integer constant, Instead, Enum is a Type like class or interface which provides compile time type safety. In this Java Enum tutorial, we will see a couple of important points about Java Enum which help to understand Enum better and get most out of it. I have written in the style of FAQ i.e. frequently asked questions, this will help you to learn more about Java Enum's capability and features e.g. whether Enum can extend class or implement methods or not? When you can override methods inside enum? whether you can compare Enum using equals() or == in Java? etc

Java ArrayList remove() and removeAll() - Example Tutorial

In this Java ArrayList tutorial, you will learn how to remove elements from ArrayList in Java e.g. you can remove String from ArrayList of String or Integer from ArrayList of Integers. There are actually two methods to remove an existing element from ArrayList, first by using the remove(int index) method, which removes elements with given index, remember index starts with zero in ArrayList. So a call to remove(2) in an ArrayList of {"one", "two", "three"} will remove 3rd element which is "three". The second method to remove element is remove(Object obj), which removes given object from ArrayList. For example, a call to remove("two") will remove the second element from ArrayList. Though you should remember to use Iterator or ListIterator remove() method to delete elements while iterating, using ArrayList's remove methods, in that case, will throw ConcurrentModificationException in Java.

Best way to convert java.util.Date to java.time.LocalDate in Java 8 - Examples

There seem to be a couple of ways to convert a java.util.Date to java.time.LocalDate in Java 8, but which one is the best way? We'll figure it out in this article, but first, let's explore these different ways to convert a java.util.Date object to LocalDate in Java 8. Btw, even though both Date and LocalDate is used to represent dates in Java they are not quite same, they don't contain even similar information. For example, the old Date class contains both date and time components but LocalDate is a just date, it doesn't have any time part in it e.g. "15-12-2016".

How to get current Day, Month, Year from Date in Java 8 and before? LocalDate vs java.util.Date

In this article, I'll show you how to get the current day, month, year, and dayOfWeek in Java 8 and earlier version e.g. Java 6 and JDK 1.7. Prior to Java 8, you can use the Calendar class to get the various attribute from java.util.Date in Java. The Calendar class provides a get() method which accepts an integer field corresponding to the attribute you want to extract and return the value of the field from given Date, as shown here. You might be wondering, why not use the getMonth() and getYear() method of java.util.Date itself, well, they are deprecated and can be removed in the future version, hence it is not advised to use them. The key point is, using old Date and Calendar API is not easy, it's very difficult to reason and debug code written using Calendar API, as it uses integer value instead of Enum or String. So, when you print e.g. Month, it will print 0 (Zero which may look wrong, but it's the value for January in old Date and Calendar API.

5 Spring Framework Books for Java developers (Includes Spring Security and Spring Boot) - Best of lot

Spring framework is one of the most popular framework in Java world which used to develop Java application, both core Java, and web application. It is also one of the most job oriented skill for Java developer, hence many beginners and intermediate Java programmers tries to learn Spring framework by their own. Though there are some good Spring training courses available online, books are still the most popular and preferred way to learn Spring framework e.g. Spring core, Spring Boot, Spring MVC, Spring Batch or Spring Security.  I often receive queries for Spring book recommendations e.g. which Spring book is good for beginners, or which Spring book is good for intermediate and advanced developers, which book should I read to learn Spring boot, Spring Batch or Spring security etc?

7 Examples of String.format() and printf() in Java - Formatting Text and String in Java

You can format String in Java either by using String.format() method or by using System.printf() method, both uses TextFormat class to format String input internally. Both of these methods are inspired from C-Style printf() method and formatting instruction are very similar to C e.g. %s for String, %d for numeric, %f for floating point value and %n for new lines etc, but it is not exactly same as C. Some customization have been made to accommodate some Java language features. Java's String formatting is also more strict than C's, for example, if a conversion is incompatible with a flag then Java throws an Exception ( java.util.IllegalFormatConversionException), while in C, incompatible flags are silently ignored.