Home » Java Basics » Page 9

Java Basics

Calculate Fibonacci Series with for loop

Bipartite Graph

In this example we shall show you how to calculate Fibonacci series using a for loop in Java. To calculate Fibonacci series with for loop one should perform the following steps: Create a long array with the numbers to be calculated. Create a for statement where the Fibonacci numbers are calculated and stored in the array. Then in another for statement ...

Read More »

Simple while loop Java Example (with video)

Bipartite Graph

1. Introduction With this example we are going to demonstrate how to use a simple while loop Java statement. The while statement continually executes a block of statements while a particular condition is true. In short, to use a simple while loop you should: Create a while statement that evaluates an expression, which must return a boolean value and has ...

Read More »

Java Break Statement Example

Bipartite Graph

This is an example of how to use the java break statement. 1. Java Break overview The break statement can be used to terminate a for, while, or do-while loop. Also it is used in switch statement to exit the current case.  2. Break in loop example In the example the break statement is used when checking an array’s elements, as ...

Read More »

Continue Java Statement

Bipartite Graph

1. Continue Java Statement In this example, we shall show you how to use the continue keyword statement in Java. The continue statement skips the current iteration of a for, while, or do-while loop and it continues the current flow of the program. In case of an inner loop, the continue statement continues only the inner loop. In this example, ...

Read More »

Compare integers

Bipartite Graph

With this example we are going to demonstrate how to compare integers. The comparation between two integers can be performed using the greater than and less than symbols. In short, to compare two integers i1 and i2 you should: Check if i1 is greater than i2 in an if statement. If it is true, that means that i1 is greater ...

Read More »

Simple if else Java Example

Bipartite Graph

In this post, we feature a simple if else Java Example. The Java if statement is used to test a boolean condition i.e. true or false. 1. Introduction The logic inside the if condition executes when the condition is true otherwise the else block is executed. Java supports various types of if statements. if statementif else statementif else if statementnested ...

Read More »

Simple if statement

Bipartite Graph

In this example we shall show you how to use a simple if statement. The if-else statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular expression evaluates to true. To use a simple if statement one should perform the following steps: Create a new boolean ...

Read More »

Free Flowing Switch Statement

Bipartite Graph

With this example we are going to demonstrate how to create a free flowing switch statement. A free flowing switch statement is a switch statement where break statement is not specified, thus all cases after the matching one (including the default) will be executed. In short, to create a free flowing switch statement you should: Create a switch statement that evaluates ...

Read More »

try/catch/finally InputStream example

Bipartite Graph

This is an example of an InputStream in a try/catch/finally statement. Using try/catch/finally statement to create an InputStream implies that you should: Create an InputStream and initialize it to null. Open a try statement and initialize the InputStream to a FileInputStream, by opening a connection to an actual file. Include the catch statement to catch any IOExceptions thrown while trying ...

Read More »

try with resource example

Bipartite Graph

The NewResource code below: public class NewResource implements AutoCloseable{ String closingMessage; public NewResource(String closingMessage) { this.closingMessage = closingMessage; } public void doSomeWork(String work) throws ExceptionA{ System.out.println(work); throw new ExceptionA("Exception thrown while doing some work"); } public void close() throws ExceptionB{ System.out.println(closingMessage); throw new ExceptionB("Exception thrown while closing"); } public void doSomeWork(NewResource res) throws ExceptionA{ res.doSomeWork("Wow res getting res to do ...

Read More »