[go: up one dir, main page]

Open In App

Java String Programs

Last Updated : 06 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A String in Java is a sequence of characters that can be used to store and manipulate text data and It is basically an array of characters that are stored in a sequence of memory locations. All the strings in Java are immutable in nature i.e. once the string is created we can’t change it. This article provides a variety of programs on strings, that are frequently asked in the technical round in various software engineering interviews including various operations such as reversing, Iteration, palindrome, swapping, and splitting of strings etc.

Here, you practice all basic to advanced programs related to strings and each program comes with a detailed description, Java code, and output. All of the examples have been thoroughly tested on both Windows and Linux systems.

What is String in Java?

A String is an object of the String class, which is part of the java.lang package. A String can be created by using the String literal: 

syntax: String name = "John";

By using the new keyword and the constructor of the String class:

Syntax: String name = new String("John");

Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.

Java String Programs for Practice: Complete List

Here is a complete list of Java String practice programs divided into basic and advance category :

Java String Programs: Basic Strings Questions with Solutions

Java String Programs: Advance Strings Questions with Solutions

Conclusion

In Conclusion, By practising these Java String questions you will get enough confidence to face any string questions in your upcoming Interview. 

Also, feel free to check out our Java interview questions collection – it could come in handy!

Java Strings Programs – FAQs

1. What is string with example?

Strings are used for storing text/characters in a contiguous memory locations. For example, “Geeks for Geeks” is a string of characters.

2. What is insert () in Java?

insert() method of the Java StringBuffer class is used to insert the given string at the specified index. There are various overloaded methods of insert().

3. How to add strings in Java?

In Java, you can combine strings using +, +=, or the concat() method. This is called concatenation.

4. What are the 5 string methods?

Method

Description

indexOf()

Returns the index within the string of the first occurrence of a specified character or substring.

toCharArray()

Converts the string to a character array.

charAt()

Returns the character at the specified index.

concat()

Concatenates one or more strings to the original string.

replace()

Replaces occurrences of a specified character or substring.


Next Article

Similar Reads

Output of Java Programs | Set 53 (String Comparison)
Prerequisite : String Comparison in Java 1. What should be the output of this program? class GeeksforGeeks { public static void main(String args[]) { String GfG1 = "Welcome to GeeksforGeeks"; boolean GfG2; GfG2 = GfG1.startsWith("hello"); System.out.println(GfG2); } } a) true b) false c) 0 d) 1 Output: b) false Explanation: The
3 min read
Output of Java Programs | Set 55 (Java Collections Framework)
Pre-requisites: Java Collection Framework. 1. What is the Output of following Java Program? import java.util.*; class Demo { public static void main(String[] args) { ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(11); arr.add(2); arr.add(3); arr.add(5); arr.add(7); arr.remove(new Integer(7)); arr.remove(2); for (int i = 0; i
6 min read
Java Programs - Java Programming Examples
Java is one of the most popular programming languages today because of its simplicity. Java programming concepts such as control statements, Arrays, Strings, Object-Oriented Programming (OOP), etc. are very important from an interview perspective as well as from exams.  So, whether you are a fresher preparing for job interviews or a beginner who ha
12 min read
Java Exercises - Basic to Advanced Java Practice Programs with Solutions
Looking for Java exercises to test your Java skills, then explore our topic-wise Java practice exercises? Here you will get 25 plus practice problems that help to upscale your Java skills. As we know Java is one of the most popular languages because of its robust and secure nature. But, programmers often find it difficult to find a platform for Jav
7 min read
Java Pattern Programs - Learn How to Print Pattern in Java
In many Java interviews Star, number, and character patterns are the most asked Java Pattern Programs to check your logical and coding skills. Pattern programs in Java help you to sharpen your looping concepts(for loop). Now if you are looking a place to get all the Java pattern exercises with solutions, then stop your search here. Here we have com
22 min read
Output of Java Programs | Set 52 (Strings Class)
Prerequisite : Basics of Strings class in java 1. What is the Output Of the following Program class demo1 { public static void main(String args[]) { String str1 = "java"; char arr[] = { 'j', 'a', 'v', 'a', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' }; String str2 = new String(arr); System.out.println(str1); System.out.prin
5 min read
Output of Java Programs | Set 54 (Vectors)
Prerequisite : Vectors in Java Basics 1. What is the Output Of the following Program Java Code import java.util.*; class demo1 { public static void main(String[] args) { Vector v = new Vector(20); System.out.println(v.capacity()); System.out.println(v.size()); } } Output: 20 0 Explanation: function - int capacity( ) Returns the capacity of the vect
6 min read
Output of Java programs | Autoboxing and Unboxing
Prerequisite - Autoboxing and unboxing in Java 1)What is the output of the following program? class Main { public static void main(String[] args) { Double x1, y1, z1; double x2, y2, z2; x1 = 10.0; y1 = 4.0; z1 = x1 * x1 + y1 * y1; x2 = 10.0; y2 = 4.0; z2 = x2 * x2 + y2 * y2; System.out.print(z1 + " "); System.out.println(z2); } } Options:
3 min read
Compile and Run Java Programs in Sublime Text in Linux
Sublime Text is a free minimalist coding editor developed by Sublime HQ for desktop use. This development and IT program enable you to solely focus on your code, leaving all the other types of eye-candy out. Procedure: Open terminal and the specific command are entered there in order to check for the software is there or not. Existence will be disp
2 min read
How to Write Robust Programs with the Help of Loops in Java?
Here we will discuss how we can write effective codes with the help of loops. It is a general perception that the approach using loops is treated as naive approach to solve a problem statement. But still, there is a huge scope of improvisation here. So basically, a loop statement allows us to execute a statement or group of statements multiple time
4 min read
Practice Tags :