Found 9326 Articles for Object Oriented Programming

Java Program to concatenate a String and Integers

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

8K+ 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

karthikeya Boyini
Updated on 31-May-2024 12:49:52

41K+ Views

The duplicate characters in a string are those that occur more than once. These characters can be found using a nested for loop. An example of this is given as follows − String = Apple In the above string, p is a duplicate character as it occurs more than once. A program that demonstrates this is given as follows. Example Live Demo public class Example { public static void main(String argu[]) { String str = "beautiful beach"; char[] carray = str.toCharArray(); System.out.println("The string is:" + str); System.out.print("Duplicate Characters in above string are: "); for (int i = 0; i

Java program for removing n-th character from a string

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

527 Views

The n-th character from a string can be removed using the substring() function. An example of this is given as follows − String = Happy The 3rd character is removed = Hapy A program that demonstrates this is given as follows − Example Live Demo public class Example { public static void main(String args[]) { String str = "Apple"; System.out.println("Original string: " + str ); System.out.println("String with 4th character removed: " + removeChar(str, 4)); ... Read More

Swapping Characters of a String in Java

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

3K+ 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

210 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

Static methods vs Instance methods in Java

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

2K+ Views

In Java as we know that the behavior of any variable/method is defined by the keyword that is used in front of its declaration name. So one of the non-access modifiers is Static which can be used along with method as well as with variable. Static methods as name states defined at the class level and could be accessed on the class name i.e no need of class object creation in order to access/call the static methods. While on another hand if we do not uses the static keyword with variable/method than it belongs or categorized as instance method which ... Read More

Stack in Java Programming

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

180 Views

Stack is a subclass of Vector that implements a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own. Stack( ) Apart from the methods inherited from its parent class Vector, Stack defines the following methods − Sr.No. Method & Description 1 boolean empty() Tests if this stack is empty. Returns true if the stack is empty, and returns false if the stack contains elements. 2 Object peek( ) Returns the element ... Read More

Sorting a HashMap according to values in Java

Rishi Raj
Updated on 30-Jul-2019 22:30:23

515 Views

As we know that Hash map in Java does not maintain insertion order either by key or by order.Also it does not maintain any other order while adding entries to it. Now in order to sort a hash map according to the values mapped to its corresponding keys we first need to get all values of map considering that hash map has unique values only.Now put all the values in a list and sort this list with the comparator or comparable interface of Java. As we get sorted list of unique values now get corresponding keys from the map and ... Read More

Advertisements