Author: Editorial Team
-
Local class in Java
A local class is a class defined in a block. A block is a group of zero or more statements enclosed between braces. Note: A local class can be defined inside any block, like a for loop, or if clause. A very common place of defining a local class is within a method. 1.1. Local…
-
Inner class in Java
An Inner class is one class defined in another class. So inner class is a member of another class just like member variables and methods. 1.1 Types of inner class There are four types of inner classes depending on where you define the inner class: 1.2 Use case and example of inner class A class…
-
Thread priority and yield method
In this tutorial we’ll understand what is a thread priority. We’ll see how to get and set a thread’s priority. We’ll then look into the yield() method. Thread priority In JVM there is a pool of runnable threads. Thread scheduler picks one thread from the pool and provides it time to run. Different JVMs use…
-
Java Thread Life Cycle and Thread States
1. Introduction From creation to termination, a thread can be in one of the five states. A thread can only be in one state at a time. These states are with reference to JVM and not operating system. Thread can be in any of the following states: 2.1 New A thread is in NEW state…
-
Thread.join() method in Java with examples
There are three overloaded versions of join. When to use Thread.join() method Suppose you have two threads, thread A and thread B. If the requirement is that thread B can not start and do its work until thread A has finished its work. In such case, you can join thread B at the end of…
-
How to create and run a thread in Java
1. Introduction A thread is a thread of execution in program. A thread is a light weight sub-process. It is smallest unit of processing. 2. Create a thread There are two ways to create thread in Java: 2.1 Create a thread by extending java.lang.Thread To create a thread by extending Thread class involved two steps:…
-
LocalDate class in Java with examples
LocalDate class is provided by java.time package in Java 8. This class respresents a date often in the form of year-month-date, without a time-zone in the ISO-8601 calendar system. This class does not store or represent time and is used to represent date like your birthday or anniversary, for example 2021-03-13. This class is immutable…
-
java.time package in Java 8 – Introduction
java.time package was introduced in Java 8 as a better way of dealing with date and time. java.util.Date and java.util.Calendar classes were not convenient to use and also didn’t handle internationalization and localization situations very well. java.util.Date, java.util.Calendar, and java.text.DateFormat classes which we were using for so many years are still there but java.time package…
-
Stream forEachOrdered method in Java with example
Many people have written us to explain the use of this method. Many people have confusion about the use of this method and how this method is different from forEach method. forEachOrdered method is provided my Stream interface in Java 8. Syntax Official documentation states the following about this method: Performs an action for each…
-
Java 8 forEach with examples
Java 8 provides a new method forEach in Iterable and Stream interfaces to iterate elements. forEach provides a new way of iterating elements. Classes which implement Iterable interface can use this method to iterate elements. Syntax 1. Syntax of forEach in Iterable 2. Syntax of forEach in Stream Official documentation states the following about forEach:…
