-->
Skip to main content

Posts

Showing posts from November, 2020

How to Reverse a String in Java [2021]

How to Reverse a String in Java [2021] This tutorial will show how to reverse a string in java using 7 different ways. how to reverse a string in java is a very common question that is being asked in interviews and also with different methods as there is no inbuilt method in the String class for string reversal. Few facts about String class 1. String class is immutable thus any change in string instance creates a new string object. 2. StringBuffer/StringBuilder class has an inbuilt reverse() method. 3. As StringBuffer is synchronized so StringBuilder is preferred over StringBuffer. So let's get started...  Here are the 7 different ways to reverse a string in java Reverse a string in java using for loop. Reverse a string in java using recursion. Using the built-in string reverse function in java of StringBuilder class/StringBuffer class. Converting String to Character Array and then swapping with iteration. Reverse string using the charAt method. Reverse a string in java 8. Re

Difference between double and float in java [2021]

Difference between double and float in java [2021] Both double  and float  are used to represent floating-point numbers in java, although there are similarities as well as differences between double and float in java. double can provide precision up to 15 to 16 decimal points whereas float provides precision up to 6 to 7 decimal places. double is more expensive in case of storage requirements . It requires 8 bytes to store a variable whereas float takes 4 bytes to store a variable. If memory is a constraint then it's better to use float than double. Here is one of the best book to learn Java: Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about similarities and differences between a float and a double data type in Java.  Let's discuss the differences in detail. Difference between double and float in Java 1. Suffix By default, float numbers are double in java. In order to store them in the float variable, you need to explicitly add the suffix

What is Delay Loop in Java?

What is Delay Loop in Java? In this tutorial, we will cover What is Delay loop in java . Delay Loop in Java The word Delay Loop simply refers to a small java program that has an inbuilt delay in execution for certain purposes. For Example: In real-time applications, if we are using third-party Rest Api which runs synchronously so we put some delay between two API calls so that the first API hit response comes back within that time. Code in Action:  public class DelayLoopExample extends Thread { @Override public void run() { for (int i = 1; i <= 10; i++) { System.out.println("Element : "+ i + " is in process : " + LocalTime.now()); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { DelayLoopExample example = new DelayLoopExample(); example.start(); } } Output: Element : 1 is in process : 1