Found 9150 Articles for Object Oriented Programming

Java program to sort long Array

karthikeya Boyini
Updated on 30-Aug-2024 11:43:14

2K+ Views

In this article, we will sort a long array in Java. We will be using the Arrays.sort() method of Arrays class which is present in java.util package. It will helps us to organize data in ascending order, making it easier to use. We'll start with an unsorted array, display it, sort it using Java’s built-in method, and then display the sorted array.  Problem Statement Given an unsorted array of long numbers write a Java program to sort the unsorted array. Input Unsorted long array: 987, 76, 5646, 96, 8768, 8767 Output Sorted long array:7696987564687678768 Steps to sort the long Array Following are ... Read More

Intersection of two arrays in Java

Samual Sam
Updated on 30-Jul-2019 22:30:23

2K+ Views

The intersection of the two arrays results in those elements that are contained in both of them. If an element is only in one of the arrays, it is not available in the intersection. An example of this is given as follows − Array 1 = 1 2 5 8 9 Array 2 = 2 4 5 9 Intersection = 2 5 9 A program that demonstrates the intersection of two sorted arrays in Java is given as follows. Example Live Demo public class Example { public static void main(String args[]) ... Read More

Java program to check if binary representation is palindrome

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

4K+ Views

A palindrome is a sequence that is same both forwards and backwards. The binary representation of a number is checked for being a palindrome but no leading 0’s are considered. An example of this is given as follows − Number = 5 Binary representation = 101 The binary representation of 5 is a palindrome as it is the same both forwards and backwards. A program that demonstrates this is given as follows. Example Live Demo public class Example { public static void main(String argc[]) { long num ... Read More

Java Program to concatenate a String and Integers

Samual Sam
Updated on 30-Jul-2019 22:30:23

9K+ Views

To concatenate a String and some integer values, you need to use the + operator. Let’s say the following is the string. String str = "Demo Text"; Now, we will concatenate integer values. String res = str + 1 + 2 + 3 + 4 + 5; The following is the final example. Example Live Demo public class Demo { public static void main(String[] args) { String str = "Demo Text"; System.out.println("String = "+str); String res = str + 1 + 2 + 3 + 4 + 5; System.out.println(res); } } Output String = Demo Text Demo Text12345

Java program to find all duplicate characters in a string

Shriansh Kumar
Updated on 01-Aug-2024 10:46:49

65K+ Views

The duplicate characters in a string are those that occur more than once. In Java, String is a non-primitive datatype that contains one or more characters and is enclosed in double quotes(“ ”). As per the problem statement, we are given a string and our task is to write a Java program to find all the duplicate characters from that string. We can use for loop and nested for loop to solve this given problem. Example Scenario Input: String str = "Apple"; Output: duplicate_char = p; p is a duplicate character as it occurs more than once. Using ... Read More

Java program for removing n-th character from a string

Samual Sam
Updated on 24-Sep-2024 21:44:16

906 Views

In this article, we will learn how to remove the n-th character from a string in Java. The program takes an input string and an index, removes the character at the given index, and returns the modified string and this process will be demonstrated through a simple example, using the substring() method. Problem Statement Write a program in Java that removes the n-th character from a given string. The program will display the original string and the string after the character is removed − Input Original string: Apple Output String with 4th character removed: Appe Steps to remove n-th character from ... Read More

Swapping Characters of a String in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:23

4K+ Views

For swapping characters of a string in Java we can use string builder which is mutable so we need not to take care of new object creation during swapping. In this we would create a method which swap characters of string based on location of swapping characters.This method will take location of swapping characters as its parameters.First store both characters need to be swapped and using set character method of string builder swap the targeted characters. Example Live Demo public class SwapCharacters { public static void main(String[] args) { ... Read More

Swap two variables in one line in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:23

1K+ Views

In order to swap two variable using single expression or in single line we could use bitwise XOR operator of Java. As we now that in Java XOR functions as XOR of two numbers a and b returns a number which has all the bits as 1 wherever bits of a and b differs. So for swapping of two variable we would use this operator as Example Live Demo public class SwapUsingBitwise { public static void main(String[] args) { int a = 8 ; int b = 10; ... Read More

Swap two Strings without using third user defined variable in Java

Arushi
Updated on 30-Jul-2019 22:30:23

281 Views

In order to swap two strings i.e interchange the content of two strings we will use sub string method of string class in Java.First of all get the length of both strings before making any change in any of string.Now modify one string as concatenate both strings and assign to one string. After this use sub string method of String class using begin index as length of new modified string 1 and last index as initial length of string 1.This will give us the swiped string 1 which contain content of string 2. Now to get swiped string 2 again ... Read More

Streams in Java

Fendadis John
Updated on 30-Jul-2019 22:30:23

1K+ Views

Stream is a new abstract layer introduced in Java 8. Using stream, you can process data in a declarative way similar to SQL statements. For example, consider the following SQL statement. SELECT max(salary), employee_id, employee_name FROM Employee The above SQL expression automatically returns the maximum salaried employee's details, without doing any computation on the developer's end. Using collections framework in Java, a developer has to use loops and make repeated checks. Another concern is efficiency; as multi-core processors are available at ease, a Java developer has to write parallel code processing that can be pretty error-prone. To resolve ... Read More

Advertisements