Compare Two Objects in Java

Shriansh Kumar
Updated on 31-Jul-2024 19:55:42

1K+ Views

To compare objects in Java, check the properties or the hash value of given objects. If the properties of both objects matches, then they are equal. If either property is different, it indicate that the objects are not equal. NOTE: Don't use equality operator (==) to compare objects as it checks only their memory location. Objects comparison should be done based on the properties not location. How to Compare Objects in Java? To compare two objects in Java, use the following approaches − By overriding equals() method Without overriding equals() method ... Read More

Find Frequency of a Character in a Given String in Java

Shriansh Kumar
Updated on 31-Jul-2024 19:54:06

7K+ Views

For a given a string, write a Java program to find the frequency of a particular character. In Java, String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). Finding Frequency of a Character in a String To find the Frequency of a character in a given String, follow the below ways − Using for Loop Using Stream API Using for loop In this approach, use the for loop to compare each character in the given string with the character whose ... Read More

String Interpolation in Java

Shriansh Kumar
Updated on 31-Jul-2024 19:53:44

1K+ Views

String interpolation is a concatenation technique used to display output text dynamically or effectively by replacing the placeholder characters with variables (or strings in the subsequent examples). It restructures the code and prevents the need to repeatedly use variables to produce the output. It makes it efficient to write lengthy variable names or text just by replacing the placeholder with the variable names allocated to strings. String Interpolation in Java There are several ways in Java to perform String Interpolation with the help of the concatenation operator and library function or class. Here, we will discuss four different methods ... Read More

Print Prime Numbers Below 100 in Java

Shriansh Kumar
Updated on 31-Jul-2024 19:53:03

2K+ Views

Any whole number which is greater than 1 and has only two factors i.e. 1 and the number itself, is called a prime number. Other than these two number it has no positive divisor. In this article, we will explain Java programs to print prime numbers between 1 to 100. Few examples of prime numbers are − 2, 3, 5, 7, 11 etc. Steps to Print Prime numbers below 100 Follow the steps below to print prime numbers below 100 in Java − Take integer variable, let's say "A" Divide the variable A with (A-1 to 2) If A ... Read More

Swap First and Last Characters of Words in a Sentence in Java

AmitDiwan
Updated on 31-Jul-2024 17:51:50

3K+ Views

Problem Statement Given a sentence, create a  program in Java that swaps the first and last characters of each word efficiently as given below − Input That is a sample Output The string after swapping the last characters of every word is : thaT si a eampls Steps to swap first and last characters of words in a sentence Below are the steps to swap first and last characters of words in a sentence − Convert string to character array. Iterate through the character array using while loop to identify the ... Read More

Remove Items from Set in Java

karthikeya Boyini
Updated on 31-Jul-2024 17:51:18

4K+ Views

A set in Java is modelled after the mathematical set and it cannot contain duplicate elements. The set interface contains the methods that are inherited from Collection. The method remove() removes the specified items from the set collection. A program that demonstrates the removal of items from Set using remove() is given as follows − Problem Statement Given a Set, write a Java program to remove the elements from the Set − Input [115, 20, 5, 70, 89, 10, 30, 111] Output [115, 20, 5, 70, 10, 30, 111] ... Read More

Delete Duplicate Characters from a Given String in Java

karthikeya Boyini
Updated on 31-Jul-2024 17:49:38

3K+ Views

The Set interface does not allow duplicate elements, therefore, create a set object and try to add each element to it using the add() method in case of repetition of elements this method returns false − If you try to add all the elements of the array to a Set, it accepts only unique elements so, to find duplicate characters in a given string. Problem Statement Given a string, write a program in Java to delete duplicate characters from a given string − Input TUTORIALSPOINT Output Indices of the duplicate characters in the given string :: Index :: ... Read More

Get System IP Address in Windows and Linux Machine

Shiva Keerthi
Updated on 31-Jul-2024 17:49:04

3K+ Views

IP Address, also known as the Internet Protocol Address is a unique identifier that is assigned to a device in a network to identify the devices in the network and establish a connection between them. In this section, we will learn how to find the IP Address of a Windows and Linux Machine using Java Code. The IP Address can be represented in two formats. One format is IPv4 which is a 32 format containing decimals and dots. It has two parts, one part is network ID which identifies the network and the other is host ID which identifies ... Read More

Verify Element Existence in Array using Java

Maruthi Krishna
Updated on 31-Jul-2024 17:29:29

1K+ Views

Given an array and one of its element as an input, write a Java program to check whether that element exists in given array or not. You can find any element from an array using search algorithms. In this article, we will use linear search and binary search algorithms. Using Linear Search Algorithm In this approach, follow the steps below to verify whether a given element exists in an array − Iterate through the array using for loop. Compare each element with the required element. If found return the index. Example The following Java program shows how ... Read More

Java strictfp Keyword

Shriansh Kumar
Updated on 31-Jul-2024 17:27:39

2K+ Views

The strictfp keyword is a modifier that stands for strict floating point. As the name suggests, it ensures that floating-point operations give the same result on any platform. This keyword was introduced in Java version 1.2. In Java, the floating point precision may vary from one platform to another. The strictfp keyword solves this issue and ensures consistency across all platforms. With the release of Java 17 version, the strictfp keyword is no longer needed. Regardless of whether you use this keyword, the JVM now provides consistent results for floating-point calculations across different platforms. When to use Java strictfp keyword? ... Read More

Advertisements