- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
String class in Java
Strings, which are widely used in Java programming, are a sequence of characters. The String class is immutable, so that once it is created a String object cannot be changed. If there is a necessity to make a lot of modifications to Strings of characters, then you should use String Buffer & String Builder Classes.
Let us now see how to create strings in Java −
String str = “John”;
The new keyword is used in creating arrays and even strings. Let us see how −
String str = new String(“John”);
String class has a lot of methods in Java to manipulate strings, finding length, format strings, concatenate, etc. Let us see some examples −
Here, we will find the length of strings and concatenate them −
Example
public class Main { public static void main(String args[]) { String str1 = "This is "; String str2 = "it!"; System.out.println("String1 = "+str1); System.out.println("String2 = "+str2); int len1 = str1.length(); System.out.println( "String1 Length = " + len1 ); int len2 = str2.length(); System.out.println( "String2 Length = " + len2 ); System.out.println("Performing Concatenation..."); System.out.println(str1 + str2); } }
Output
String1 = This is String2 = it! String1 Length = 8 String2 Length = 3 Performing Concatenation... This is it!
- Related Articles
- Java String Class Tutorial
- Difference between String class and StringBuffer class in Java
- What is Java String Class?
- What is special about string class in Java?
- Difference Between String and StringBuffer Class in Java
- What is the package for String Class in Java?
- Why String class is immutable or final in Java?
- Check whether String is an interface or class in Java
- StringJoiner Class vs String.join() Method to Join String in Java
- Why String class is popular key in a HashMap in Java?
- When can we use intern() method of String class in Java?
- String class in C#
- Convert Long into String using toString() method of Long class in java
- What are new methods added to the String class in Java 9?
- Java Program to create String to super class type mapping

Advertisements