Java Archive

Java Break and Continue in Loops

These two keywords help us control the loop from within it. break will cause the loop to stop and will go immediately to the next statement after the loop: continue will stop the current iteration and will move to the next one. Notice that inside a for loop, it will still run the third section.

Java Foreach Loop

Another version of for, is the foreach. The keyword we use is still for, but when we want to iterate on the elements inside an array we can simply use it: This is a short version and equivalent to: Notice that if you want to use the index of the element inside the loop, you have

Java While Loop

The syntax is very similar to the previous for we looked at: The condition will run for the first time when entering and every time the loop is done. If it returns false, the loop will not run. If we want the loop to always run at least one, we can use do-while Notice the ; in

Java For Loop

The for loop has three sections: First section runs once when we enter the loop. Second section is the gate keeper, if it returns true, we run the statements in the loop, if it returns false, we exit the loop. It runs right after the first section for the first time, then every time the loop is

Java If-else Statement

The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in Java. if statement if-else statement if-else-if ladder nested if statement Java if Statement The Java if statement tests the condition. It executes the if block if condition is true. Example: The Java

How to create an Array List with Java?

To store dynamically-sized elements in Java, we used ArrayList. Whenever new elements are added to it, its increase their size automatically. Key Points of an ArrayList An ArrayList is a re-sizable array, also known as a dynamic array. It raises its size according to new elements and decreases the size when the elements are removed.

How to return lambda using Java 8?

The first thing, which I am going to show you – how you can easily return lambda as a return type. This is example is very simple and you could adjust it regarding your needs with your domain objects, etc. We are using java.util.function.Function<T, R> interface to return function from our method. T – means

How to compare Strings in Java?

Problem There are many ways how to compare strings in Java and most of them are different. Here we will cover these cases: Using .equals() method; Using .compareTo() method; Using == operator. Solution Let’s check what we can compare using .equals() / .equalsIgnoreCase() methods. If you are newbie in programming / Java – I am almost sure that this

How to read UTF8 file with BOM?

Wikipedia about UTF8 BOM The byte order mark (BOM) is a Unicode character, U+FEFF byte order mark (BOM), whose appearance as a magic number at the start of a text stream can signal several things to a program consuming the text:[1] What byte order, or endianness, the text stream is stored in; The fact that the