How to break from a nested loop in Java? [Example]

There are situations we need to be nested loops in Java, one loop containing another loop like to implement many O(n^2) or quadratic algorithms e.g. bubble sort, insertion sort, selection sort, and searching in a two-dimensional array. There are a couple of more situations where you need nesting looping like printing the pascal triangle and printing those star structures exercises from school days. Sometimes depending upon some condition we also like to come out of both inner and outer loops. For example, while searching a number in a two-dimensional array, once you find the number, you want to come out of both loops. The question is how can you break from the nested loop in Java. 

4 Examples to Round Floating-Point Numbers in Java up to 2 Decimal Places

Rounding numbers up to 2 or 3 decimal places id a common requirement for Java programmers. Thankfully, Java API provides a couple of ways to round numbers in Java. You can round any floating-point numbers in Java unto n places By using either Math.round(), BigDecimal, or DecimalFormat. I personally prefer to use BigDecimal to round any number in Java because of it's convenient API and support of multiple Rounding modes. Also, if you are working in the financial industry, it's best to do a monetary calculation in BigDecimal rather than double as I have discussed in my earlier post about common Java mistakes

3 ways to create random numbers in a range in Java - Examples

Many times you need to generate random numbers, particular integers in a range but unfortunately, JDK doesn't provide a simple method like nextIntegerBetween(int minInclusive, int maxExclusive), because of that many Java programmers, particularly beginners struggle to generate random numbers between a range, like random integers between 1 to 6 if you are creating a game of dice, or a random number between 1 to 52 if you are creating a game of playing cards, and you need to choose a random card, or most commonly random numbers between 1 to 10 and 1 to 100. Then, the question comes, how to solve this problem? How to generate random int values between a range? Well, you need to do a little bit of work.

float and double data types in Java with Examples

Hello guys, float and double are two of the important data types in Java, but many developers don't pay enough attention to these two data types, resulting in writing poor code with subtle bugs. To be honest, they are not easy to use because of the complexity with floating-point calculation but knowing even simple things like the maximum and minimum limit of float and double data types and how to compare float and double variables can go a long way in eliminating bugs which are not obvious.

The Ultimate Guide to Package in Java? Examples

If you are learning Java then you might have come across a package concept and if you are wondering what is package and why should we use it then you have come to the right place. In this article, I will explain what is package in Java and other stuff around the package, including some best practices which using the package in Java. In the simplest form, a package is a way to organize related functionality or code in a single place in Java. If you look from a File System perspective then a package in Java just represent a directory where Java source file is stored in compilation and class files are stored after compilation.