Pages

Thursday, September 5, 2024

Java Program To Reverse A String Without Using String Inbuilt Function reverse()

1. Introduction


In this tutorial, You'll learn how to write a java program to reverse a string without using string inbuilt function reverse().
This is a very common interview question that can be asked in many forms as below.

A) java program to reverse a string without using string inbuilt function
B) java program to reverse a string using recursion
C) java program to reverse a string without using the reverse method
C) Try using other classes of Java API (Except String).



The interviewer's main intention is not to use the String class reverse() method.

Anonymous Comparator In Java For Sorting

1. Overview

In this tutorial, We'll learn how to sort arrays or lists using an anonymous comparator in java.

A complete guide on how to use comparator in java?

Anonymous class means creating the class and providing the implementation at the same without a class name.

For example, we have an interface Job and it has the method post() to post the new job. here, a new job1 instance is created for the implementation class of Job interface without a class name.

Example 1

interface Job {
	void post();
}

Job job1 = new Job() {

	@Override
	public void post() {
		System.out.println("Posting job 1 now");

}


This is similar to the anonymous implementation of the abstract class or interface. This is the actual implementation of the Comparator without creating a class and implementing the Comparator interface.

Sunday, September 1, 2024

Java Thread.join() Examples to Wait for Another Thread

1. Introduction


In this tutorial, We'll learn how to use Thread.join() method in java. And also how to join the multiple threads at one place after completing the execution of all threads or one thread or any other threads.

join() method is part of the Thread class and it is part of the java.lang package. All the classes in java.lang package is not needed to add import statements. So, directly you can use it in the programs. join() is mainly used to sleep or wait the current thread until completion of another thread execution.

Similar to the wait() and notify(), join() also widely used in the thread intercommunication.

2. Thread.join() Syntax


below is the syntax from Thread API. join() method is an overloaded method so we should be careful which method should be used.

public final void join() throws InterruptedException

public final void join(long millis) throws InterruptedException

public final void join(long millis, int nanos) throws InterruptedException


Java 8 String API also has join() method.

Friday, January 7, 2022

Java Program To Find First Non-Repeated Character In String (5 ways)

1. Overview


In this article, We will be learning and understanding the various ways to find the first non repeated character in a given string in various ways along with Java 8 Streams. Many programs may face this type of question in programming interviews or face to face round.
Let us start with the simple approach and next discover the partial and single iteration through the string.

Java Program To Find First Non-Repeated Character In String (5 ways)


Java 8 - How to convert Calendar to LocalDateTime?

1. Overview

In this article, You'll learn how to convert Calendar to LocalDateTime object in java 8 DateTime api

LocalDateTime api can be used completely replacement to the Date class as all features of Date functions are done in a simple and precise way such as adding minutes to the current date.

Java 8 - How to convert Calendar to LocalDateTime?