-
Best way to round number to nth decimal place in Java
To format numbers, Java provides classes like NumberFormat and DecimalFormat classes. DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale. All of these can be localized. Read more about DecimalFormat class on Oracle website.…
-
What are ways to split the string in Java?
Java String class provides a method to split the string using a delimiter. It takes delimiter as a regular expression and Splits this string around matches of the given regular expression. This method has two overloaded versions. public String[] split(String regex, int limit) The array returned by this method contains each substring of this string that…
-
Best way to generate random integers within a specific range in Java
There are many classes in Java to generate random numbers. Random and ThreadLocalRandom are best to generate numbers between ranges. ThreadLocalRandom class Since Java 7, it provides ThreadLocalRandom class to generate random numbers. A random number generator isolated to the current thread. Like the global Random generator used by the Math class, a ThreadLocalRandom is…
-
Best and Simplest ways to print Java Array
Many time printing arrays in Java are required to logging and debugging purpose. In Java provides Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays (a nested array) to formatting simple arrays into printable strings. Example of printing a simple array: String names[] = new String[] {“ssp”,”anu”,”kirti”,”shaurya”}; System.out.println(names); System.out.println(Arrays.toString(names)); the output of above code is given below: [Ljava.lang.String;@2a139a55 [ssp, anu,…
-
Best way to prevent ArrayIndexOutOfBoundsException in Java
ArrayIndexOutOfBoundsException is an exception, thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array. illegal index in java is index value less than zero or -ve values and index value equal to or greater than array…
-
Is Java method parameters are “pass-by-reference” or “pass-by-value”?
Many beginners think that Java is pass-by-reference. The Java specification document says that everything in Java is pass-by-value. There is no such thing as “pass-by-reference” in Java and this is fact. In Java when we pass an object to some method parameter it actually passes objects reference value which many people think that it is…
-
Best way to compare strings in Java
In Java, strings are the reference type. So we can not compare two strings using the == operator as it compares references (will only return true when both references are of the same object). In Java, all objects are derived from class Object which has one method equals, which is used to compare equality of…
-
What is NullPointerException in Java, and how to avoid and fix it?
In Java NullPointerExceptions are exceptions that occur when we try to use a reference that points to no location in memory (Know as null) as though it were referencing an object (uninitialized objects or assigned to null object references). Calling a method or accessing a member on a null reference will throw a NullPointerException. Generally,…
-
How to install MySql on Ubuntu 18 server
MySql 5.7 is an open-source free RDBMS for small and midsize data. This supports connectivity with many different programming languages and platforms via drivers. Om Linux system is part of the LAMP(Linux, Apache, MySQL, PHP/Python/Perl) stack. In this post, I am writing methods to install MySQL 5.7 on Ubuntu 18 via the apt repository. Simply…
-
Installing and Configuring Apache ActiveMQ on Ubuntu 18
Apache ActiveMQ ™ is one of the most popular and powerful open source JAVA based messaging and Integration Patterns server. This supportes many protocols and provides client library for many platforms and languages. Apache ActiveMQ is a message broker server written in Java with JMS, REST and WebSocket interfaces, however it supports many protocols like…