Java String
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.
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.
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".
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).
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.
4 static String format(Locale l, String format, Object... args) It returns formatted string
locale.
Next →
Feedback
Splunk
SPSS
Swagger
Transact-SQL
Tumblr
ReactJS
Regex
Reinforcement Learning
R Programming
RxJS
React Native
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
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
OUR WEBSITES
Javatpoint.comHindi100.comLyricsia.comQuoteperson.comJobandplacement.com
OUR SERVICES
Website Development
Android Development
Website Designing
Digital Marketing
Summer Training
Industrial Training
CONTACT
Ctrl+M
Explain
Quick Actions
Summarize
Grammar
Explain
Explain Codes
Rewrite
Translate
Q&A
Expand
Disable float button
EMAIL (OPTIONAL)
Report This Ad
X