Java String
In Java, string is basically an object that represents sequence of char values. An array of characters w
Java string. For example:
1. char[] ch={'j','a','v','a','t','p','o','i','n','t'};
2. String s=new String(ch);
is same as:
1. String s="javatpoint";
Java String class provides a lot of methods to perform operations on strings such as compare(), conc
split(), length(), replace(), compareTo(), intern(), substring() etc.
The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.
PlayNext
Unmute
Current Time 0:00
Duration 18:10
Loaded: 0.37%
Â
Fullscreen
Backward Skip 10sPlay VideoForward Skip 10s
CharSequence Interface
The CharSequence interface is used to represent the sequence of
String, StringBuffer and StringBuilder classes implement it. It means, we can create strings in Java by
three classes.
The Java String is immutable which means it cannot be changed. Whenever we change any string, a ne
created. For mutable strings, you can use StringBuffer and StringBuilder classes.
We will discuss immutable string later. Let's first understand what String in Java is and how to crea
object.
What is String in Java?
Generally, String is a sequence of characters. But in Java, string is an object that represents a
characters. The java.lang.String class is used to create a string object.
How to create a string object?
There are two ways to create String object:
1. By string literal
2. By new keyword
1) String Literal
Java String literal is created by using double quotes. For Example:
1. String s="welcome";
Each time you create a string literal, the JVM checks the "string constant pool" first. If the string already
pool, a reference to the pooled instance is returned. If the string doesn't exist in the pool, a new strin
created and placed in the pool. For example:
1. String s1="Welcome";
2. String s2="Welcome";//It doesn't create a new instance
In the above example, only one object will be created. Firstly, JVM will not find any string object w
"Welcome" in string constant pool that is why it will create a new object. After that it will find the st
value "Welcome" in the pool, it will not create a new object but will return the reference to the same in
Note: String objects are stored in a special memory area known as the "string constant pool".
Why Java uses the concept of String literal?
To make Java more memory efficient (because no new objects are created if it exists already in the st
pool).
2) By new keyword
1. String s=new String("Welcome");//creates two objects and one reference variable
In such case, JVM will create a new string object in normal (non-pool) heap memory, and the literal "W
be placed in the string constant pool. The variable s will refer to the object in a heap (non-pool).
Java String Example
StringExample.java
1. public class StringExample{
2. public static void main(String args[]){
3. String s1="java";//creating string by Java string literal
4. char ch[]={'s','t','r','i','n','g','s'};
5. String s2=new String(ch);//converting char array to string
6. String s3=new String("example");//creating Java string by new keyword
7. System.out.println(s1);
8. System.out.println(s2);
9. System.out.println(s3);
10. }}
Test it Now
Output:
java
strings
example
The above code, converts a char array into a String object. And displays the String objects s1, s
console using println() method.
Java String class methods
The java.lang.String class provides many useful methods to perform operations on sequence of char va
No. Method Description
1 char charAt(int index) It returns char value for the part
2 int length() It returns string length
3 static String format(String format, Object... args) It returns a formatted string.
4 static String format(Locale l, String format, Object... args) It returns formatted string
locale.
5 String substring(int beginIndex) It returns substring for given be
6 String substring(int beginIndex, int endIndex) It returns substring for given
and end index.
7 boolean contains(CharSequence s) It returns true or false after m
sequence of char value.
8 static String join(CharSequence delimiter, CharSequence... It returns a joined string.
elements)
9 static String join(CharSequence delimiter, Iterable<? extends It returns a joined string.
CharSequence> elements)
10 boolean equals(Object another) It checks the equality of strin
given object.
11 boolean isEmpty() It checks if string is empty.
12 String concat(String str) It concatenates the specified str
13 String replace(char old, char new) It replaces all occurrences of t
char value.
o How to create an immutable class?
o What is string constant pool?
o What code is written by the compiler if you concatenate any string by + (string concatenation o
o What is the difference between StringBuffer and StringBuilder class?
What will we learn in String Handling?
o Concept of String
o Immutable String
o String Comparison
o String Concatenation
o Concept of Substring
o String class methods and its usage
o StringBuffer class
o StringBuilder class
o Creating Immutable class
o toString() method
o StringTokenizer class
Next Topic Immutable String
Next →
For Videos Join Our Youtube Channel: Join Now
Feedback
Help Others, Please Share
Learn Latest Tutorials
Splunk
SPSS
Swagger
Transact-SQL
Tumblr
ReactJS
Regex
Reinforcement Learning
R Programming
RxJS
React Native
Python Design Patterns
Python Pillow
Python Turtle
Keras
Preparation
Aptitude
Reasoning
Verbal Ability
Interview Questions
Company Questions
Trending Technologies
Artificial Intelligence
AWS
Selenium
Cloud Computing
Hadoop
ReactJS
Data Science
Angular 7
Blockchain
Git
Machine Learning
DevOps
B.Tech / MCA
DBMS
Data Structures
DAA
Operating System
Computer Network
Compiler Design
Computer Organization
Discrete Mathematics
Ethical Hacking
Computer Graphics
Software Engineering
Web Technology
Cyber Security
Automata
C Programming
C++
Java
.Net
Python
Programs
Control System
Data Mining
Data Warehouse
Report this ad
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more
information about given services.
o Website Designing
o Website Development
o Java Development
o PHP Development
o WordPress
o Graphic Designing
o Logo
o Digital Marketing
o On Page and Off Page SEO
o PPC
o Content Development
o Corporate Training
o Classroom and Online Training
o Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop,
PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
LEARN TUTORIALS
Learn JavaLearn Data StructuresLearn C ProgrammingLearn C++ TutorialLearn C#
TutorialLearn PHP TutorialLearn HTML TutorialLearn JavaScript TutorialLearn jQuery
TutorialLearn Spring Tutorial
OUR WEBSITES
Javatpoint.comHindi100.comLyricsia.comQuoteperson.comJobandplacement.com
OUR SERVICES
Website Development
Android Development
Website Designing
Digital Marketing
Summer Training
Industrial Training
College Campus Training
CONTACT
Address: G-13, 2nd Floor, Sec-3
Noida, UP, 201301, India
Contact No: 0120-4256464, 9990449935
Contact UsSubscribe UsPrivacy PolicySitemap
About Me
© Copyright 2011-2021 www.javatpoint.com. All rights reserved. Developed by JavaTpoint.
Ctrl+M
Explain
Quick Actions
Summarize
Grammar
Explain
Explain Codes
Rewrite
Translate
Q&A
Expand
Disable float button
Why do you want to report this ad?
EMAIL (OPTIONAL)
Report This Ad
X