Found 9150 Articles for Object Oriented Programming

Binary Search vs Contains Performance in Java List

Neetika Khandelwal
Updated on 12-Jul-2023 13:01:06

643 Views

When it comes to searching for elements in a collection, Java provides different options depending on the data structure you're using. Two popular methods for searching in a list are binary search and the contains() method. In this blog post, we'll compare the performance of binary search and contains in a Java list, highlighting their differences, strengths, and best use cases. Binary Search Binary search is an efficient way for locating a specific member in a sorted list. The search space is divided in half on a regular basis until the target element is found or the search space is ... Read More

12 Tips to Optimize Java Code Performance

Neetika Khandelwal
Updated on 12-Jul-2023 12:48:07

4K+ Views

Java is a well−known programming language that is used to create a variety of applications. The user experience could be harmed by performance issues that can be caused by poorly optimized Java code. In this blog post, you will come across 12 techniques for improving the performance of Java programming. Techniques to Optimize Java Code Use Efficient Data Structures The choice of a suitable data structure has a significant impact on the effectiveness and speed of Java programming. For instance, choosing a LinkedList over an ArrayList can be advantageous if you frequently add or delete entries from a list because ... Read More

Break Any Outer Nested Loop by Referencing its Name in Java

Neetika Khandelwal
Updated on 12-Jul-2023 12:44:02

2K+ Views

Programming is all about coming up with the best and most efficient ways to solve the real−world problems. There are situations when you want to exit multiple loops simultaneously. This can be accomplished in Java by simply referencing the name of the loop you want to exit. In this tutorial, we'll look at how to break any outer nested loop in Java by referencing its name. Referencing Loop Names in Java You can break out of the Java nested loop by labelling the outer loop. This can be accomplished by using a label before the outer loop followed by a ... Read More

Java program to find length of the longest substring without repeating characters

Prabhdeep Singh
Updated on 24-Jul-2024 11:43:39

2K+ Views

In Java, substrings are part of the string which contains the continuous character of the string of any length from 1 to complete string. We are given a string and we have to find the length of the largest substring from the given string that only contains the unique characters. We will see three types of methods: finding every substring, sliding windows, and two-pointers. Problem Statement Given a string, write a Java program to find length of the longest substring without repeating characters − Input thisisthegivenstring Output The length of the longest substring that contains only unique characters is: ... Read More

Java Program To Write Your Own atoi()

Prabhdeep Singh
Updated on 16-Aug-2024 08:02:41

862 Views

The atoi() function is used in C programming language and used to convert the string which is passed as the parameter to it into an integer value if the string is a valid integer otherwise it shows the undefined behavior. We will implement the atoi() function in the Java programming language. Example Scenario 1: Input: string str = "123" Output: res = 123 We are given a string that represents a number so we have just got the same output. Example Scenario 2: Input: string str = "897c7" Output: res = Invalid Input The given string ... Read More

Java program to find longest common prefix using word by word matching

Prabhdeep Singh
Updated on 29-Sep-2024 02:50:52

918 Views

In this article, we will explore how to find the longest common prefix among a given set of strings using two different approaches in Java. We will first discuss an approach that compares all strings directly to find the longest prefix and then move to a word-by-word matching approach.  Problem Statement We are given a set of strings and we have to find the common prefix among all of them. A prefix is a substring for a string that contains the zero index and could be of any length from 1 to a complete string. Input 1 string arr[] = ... Read More

Java Program to Check if all Rows of a Matrix are Circular Rotations of Each Other

Prabhdeep Singh
Updated on 11-Jul-2023 14:14:22

209 Views

Matrix consists of rows and columns to form a rectangular array. And circular rotations mean rotating the array's elements so that one rotation places the last element in the first position and the rest of the elements to the right. In this problem, we have given a matrix of n * n, and our task is to check if all rows of a matrix are circular rotations of each other then print “YES” otherwise print “NO”. Let's see examples with explanations below to understand the problem in a better way. Input 1 mat = [ [ 1, 5, 6], ... Read More

Java Program for Converting Roman Numerals to Decimal Lying Between 1 to 3999

Prabhdeep Singh
Updated on 11-Jul-2023 14:05:25

655 Views

The characters used in an arrangement of number notation based on the pre-Roman Roman system is called Roman numerals. The letters M, D, C, L, X, V, and I stand for 1000, 500, 1000, 50, 10, 5, and 1, respectively, and will discuss all main symbols in the below section. In this problem, we are given a string of Roman numerals and our task is to convert Roman numerals to decimals in the range of 1 to 3999. Let’s see examples with explanations below to understand the problem in a better way. Input 1 str = "MCMIX" Output 1 1909 ... Read More

Final vs Immutability in Java

Deepti S
Updated on 19-Dec-2024 14:04:21

2K+ Views

The final keyword in Java may be employed to define a constant value as well as prevent a variable, method, or class from being changed or overridden. On the other side, immutability describes an object's characteristic of keeping a constant state across the course of its existence. The values of an object don't change after it is formed. Variables, methods, and classes are constrained by the "final" keyword, but immutability goes a step further by guaranteeing that the object's whole state is preserved. Let us learn the key differences between final vs immutability in this article. Final in Java ... Read More

Few Tricky Programs in Java

Deepti S
Updated on 11-Jul-2023 10:37:08

759 Views

Confounding Java questions stem from loops, multithreading, overloading, overriding, and more, making them challenging to navigate. Occasionally, seemingly simple questions confound us, leading to haphazard code instead of straightforward solutions. With analytical thinking, we can crack these questions even without prior knowledge. Join us as we explore tricky programs in Java. Methods Used Comments that work Named loops Method 1: Comments that work In the realm of programming, Java comments are textual statements within a program that hold no significance in terms of execution by the compiler or interpreter. The purpose behind incorporating comments into code is multifold. ... Read More

Advertisements