Found 9150 Articles for Object Oriented Programming

Java Program for Binary Insertion Sort

AmitDiwan
Updated on 02-Jul-2024 17:29:47

3K+ Views

Binary insertion sort uses the binary search to find the right position to insert an element at a specific index at every iteration. First, the location where the element needs to be inserted is found. Then, the elements are shifted to the next right location. Now, the specific element is placed in the position. Problem Statement Given an integer of array implement binary insertion sort using Java to sort it. The algorithm should use binary search to determine the correct position for each element and to maintain the sorted order it will shift elements accordingly. Steps for Binary Insertion ... Read More

Java Program for Cocktail Sort

Alshifa Hasnain
Updated on 10-Mar-2025 19:41:25

364 Views

In this article, we will learn to perform cocktail sorting in Java. Cocktail Sort, also known as Bidirectional Bubble Sort, is a variation of the standard Bubble Sort algorithm. What is Cocktail Sort? Cocktail Sort works in contrast to bubble sort, wherein elements are iterated from left to right, and the largest element is first brought to its correct position, and so on. In cocktail sort, elements are iterated over in both directions (left and right) in an alternating fashion. Cocktail Sort Working Following are the steps to perform cocktail sort in Java − Traverse ... Read More

Java Numeric Promotion in Conditional Expression

AmitDiwan
Updated on 04-Jul-2020 09:20:21

154 Views

The conditional operator (? :) leverages the output of one value (which is a bool) to decide which expression has to be evaluated next. Let us see an example −Example Live Demoimport java.io.*; public class Demo{    public static void main (String[] args){       Object my_obj = true ? new Integer(91) : new Float(89);       System.out.println(my_obj);    } }Output91.0A class named Demo contains the main function. Here, an object instance is defined and if it is true, an integer value is displayed otherwise a float value is displayed. Next, they are printed on the console.When promotional expression ... Read More

Java multiplyExact() in Math

AmitDiwan
Updated on 10-Aug-2023 12:10:00

335 Views

In Java, multiplyExact() is an in-built static method of the Math class that accepts two arguments and returns their product as a result. This method can accept values of either integer or long type. One important point to note is that it may throw an exception if the produced result overflows the size of integer. After knowing the functionality of multiplyExact() in Java, one question that may pop into one's mind is that we can also use '*' operator to multiply two operands then why do we need multiplyExact() method. We are going to answer this question and also, explore ... Read More

Java Lambda Expression with Collections

AmitDiwan
Updated on 04-Jul-2020 09:16:54

870 Views

Sorting the elements of a list using lambda expression −Example Live Demoimport java.util.*; public class Demo{    public static void main(String[] args){       ArrayList my_arr = new ArrayList();       my_arr.add(190);       my_arr.add(267);       my_arr.add(12);       my_arr.add(0);       System.out.println("Before sorting, elements in the array list are : " + my_arr);       Collections.sort(my_arr, (o1, o2) -> (o1 > o2) ? -1 : (o1 < o2) ? 1 : 0);       System.out.println("After sorting, elements in the array list are : " + my_arr);    } }OutputBefore sorting, elements ... Read More

Java Interview Questions on Constructors

Shriansh Kumar
Updated on 11-Sep-2024 10:18:51

955 Views

There could be numerous interview questions on Constructors, it is not possible to cover all in just one article. However, we have researched and assorted the most popular Java interview questions on Constructors. In most of the Java interviews, the interviewers always start by asking basic questions. They can test one's knowledge in just a few minutes of the interviews. Therefore, it is essential to get thorough with the fundamental concepts of Java such as class, object and constructor. In Java interviews, the first question one might encounter is to define constructors. So, let's start our discussion with this question. ... Read More

Java ConcurrentHashMap - clear()

AmitDiwan
Updated on 04-Jul-2020 09:06:38

321 Views

The clear function is used to clear up the mapping between the key value pairs. This way, the ConcurrentHashMap mappings would be cleared.Syntaxpublic void clear()Let us see an example −Example Live Demoimport java.util.concurrent.ConcurrentHashMap; import java.util.*; public class Demo{    public static void main(String[] args){       Map my_map = new ConcurrentHashMap();       my_map.put("This", "35");       my_map.put("is", "78");       my_map.put("sample", "99");       System.out.println("The map contains the below elements " + my_map);       my_map.clear();       System.out.println("The elements after the clear function is called on it " + my_map);    } ... Read More

Java Concurrency – join() method

Alshifa Hasnain
Updated on 25-Feb-2025 19:20:42

573 Views

In this article, we will learn about concurrency in Java. It allows multiple threads to execute simultaneously, sharing system resources efficiently. One of the methods provided in the Thread class for managing thread execution is the join() method. What is the join() Method? the join () function is used to join the beginning of the execution of a thread to the end of the execution of another thread. This way, it is ensured that the first thread won’t run until the second thread has stopped executing. This function waits for a specific number of milliseconds for the thread to terminate. ... Read More

Java Concurrency – sleep() method

AmitDiwan
Updated on 04-Jul-2020 09:01:30

418 Views

The sleep functionThis sleep function is used to ensure that the currently executing thread goes to sleep for a specific amount of milliseconds which is passed as a parameter to the function. The thread stops executing for that number of milliseconds.Let us see an exampleExample Live Demoimport java.lang.*; public class Demo implements Runnable{    Thread my_t;    public void run(){       for (int i = 0; i < 3; i++){          System.out.println(Thread.currentThread().getName()+ " " + i);          try{             Thread.sleep(100);          }       ... Read More

Java Concurrency – yield() method

Alshifa Hasnain
Updated on 17-Jan-2025 19:44:20

875 Views

In this article, we will learn that concurrency in Java allows multiple threads to execute simultaneously, sharing system resources efficiently. One of the methods provided in the Thread class for managing thread execution is the yield() method. What is the yield() Method? The yield() method is a static method of the Thread class that hints to the thread scheduler that the current thread is willing to pause its execution in favor of other threads of the same or higher priority. It is part of Java's concurrency toolbox and is primarily used for fine-tuning thread execution. Syntax of yield function − ... Read More

Advertisements