Home » Java Basics » Page 8

Java Basics

Java offline installer – How to install Java in Windows

Bipartite Graph

In this tutorial we will show you how to install the latest Java version in Windows, using the Java offline installer. The Java offline installer is an executable file that contains all necessary files required for a complete installation. An internet connection is required, in order for the offline installer to be downloaded. Once the installer is downloaded, the computer ...

Read More »

Substring Java Example

Bipartite Graph

In this post, we feature a comprehensive Substring Java Example. We will show how to use Java String substring() API method. 1. Syntax substring() method can be expressed in two ways: String substring(int start, int end)String substring(int start) 2. Parameters start This parameter is mandatory and it specifies the starting position of the extracted string. Note that first character of string ...

Read More »

java.lang.NullPointerException Example – How to handle Java Null Pointer Exception (with video)

Bipartite Graph

In this post, we feature a comprehensive example of java.lang.NullPointerException – Java Null Pointer Exception. In Java, a special null value can be assigned to an object’s reference and denotes that the object is currently pointing to an unknown piece of data. A java.lang.NullPointerException is thrown when an application is trying to use or access an object whose reference equals to null. The following cases throw ...

Read More »

Synchronization with multiple locks

Bipartite Graph

The basic idea is to use separate locks to guard multiple independent state variables of a class, instead of having only one lock in class scope. public class Grocery { private final ArrayList fruits = new ArrayList(); private final ArrayList vegetables = new ArrayList(); public synchronized void addFruit(int index, String fruit) { fruits.add(index, fruit); } public synchronized void removeFruit(int index) ...

Read More »

Synchronized method and block

Bipartite Graph

The first level of synchronization is on method scope: public class HelloSync { private Map dictionary = new HashMap(); public synchronized void boringDeveloper(String key, String value) { long startTime = (new java.util.Date()).getTime(); value = value + "_"+startTime; dictionary.put(key, value); System.out.println("I did this in "+ ((new java.util.Date()).getTime() - startTime)+" miliseconds"); } } However we should consider the basic rule of concurrency: ...

Read More »

Simple Do While loop Java Example

Bipartite Graph

1. Introduction A do-while loop in Java is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block. The do while construct consists of a process symbol and a condition. First, the code within the block is executed, and then the condition is evaluated. If the ...

Read More »

Sum Array of Numbers with for loop

Bipartite Graph

This is an example of how to get the sum of the numbers in an array using a for loop. The for statement provides a compact way to iterate over a range of values. Getting the sum using a for loop implies that you should: Create an array of numbers, in the example int values. Create a for statement, with an ...

Read More »

Check for Palindrome Number with for loop

Bipartite Graph

In this example we shall show you how to check if a palindrome number exists in an array, using a for loop. A palindrome number is a number that is equal to its reverse number. To check if a palindrome number exists in an array, using a for loop one should perform the following steps: Create an array of the numbers ...

Read More »

Generate Prime Numbers with for loop

Bipartite Graph

With this example we are going to demonstrate how to generate prime numbers with a simple for loop. A prime number is a number that has no positive divisors other than 1 and itself. In short, to generate a prime number using a for loop you should: Create a for statement with an int i variable from 1 to a ...

Read More »

Simple for loop

Bipartite Graph

This is an example of a simple for statement. The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the “for loop” because of the way in which it repeatedly loops until a particular condition is satisfied. Creating a simple for loop implies that you should: Create a for statement with an ...

Read More »