SlideShare una empresa de Scribd logo
1 de 11
IDEAL EYES BUSINESS COLLEGE
A
PRESENTATION
STRING IN
JAVA LANGUAGE
1
PERSENTED BY:- VIVEK KUMARPERSENTED TO:- PANKAJ SIR
String
 Generally string is a sequence of characters. But in java,
string is an object.
 String class is used to create string object.
 string is a 16-bit Unicode character .
There are two ways to create String object:
1. By string literal 2. By new keyword
string literal
For example:-
String s1="Welcome";
String s2="Welcome"; //no new object will be created
Note:- String objects are stored in a special memory area known as
string constant pool inside the Heap memory.
String literal is create by double quote.
Each time you create a string literal, the JVM checks the string
constant pool first. If the string already exists in the pool, a
reference to the pooled instance returns. If the string does not
exist in the pool, a new String object instantiates, then is
placed in the pool
In the above example only one object will be created . First time JVM will find no string object with
the name "Welcome" in string constant pool , so it will create a new object . Second time it will find
the string with the name "Welcome" in string constant pool , so it will not create new object
whether will return the reference to the same instance.
By string literal
class String _literal
{
public static void main(String args[])
{
String x ="java";
String x1="java";
System.out.println( "x="+x);
}
}
By new keyword
In such case, JVM will create a new String object in normal (
nonpool ) Heap memory and the literal "Welcome" will be
placed in the string constant pool . The variable s will refer to
the object in Heap( nonpool ).
String s=new String("Welcome");
//creates two objects and one reference variale
For example:-
By new keyword
1. class new_keyword
2. {
3. public static void main(String args[])
4. {
5. String x ="java";
6. String s=new String("Welcome");
7. System.out.println( "x="+s);
8. System.out.println( "x="+x);
9. }
10. }
Methods of String class
java.lang.String class provides a lot of methods to work on string. Let's see the
commonly used methods of String class.
1. charAt() Returns the character located at the specified index
2. concat() Appends one String to the end of another ( "+" also works)
3. equalsIgnoreCase() Determines the equality of two Strings, ignoring case
4. length() Returns the number of characters in a String
5. replace() Replaces occurrences of a character with a new character
6. substring() Returns a part of a String
7. toLowerCase() Returns a String with uppercase characters converted
8. toString() Returns the value of a String
9. toUpperCase() Returns a String with lowercase characters converted
10.trim() Removes whitespace from the ends of a String
IMPORTANT METHODS IN THE STRING CLASS
charAt() method
class charAt
{
public static void main(String args[])
{
String s="Sachin";
System.out.println(s.charAt(5));
System.out.println(s.charAt(3));
}
}
toUpperCase() and toLowerCase() method
class low_upper
{
public static void main(String args[])
{
String s="Sachin";
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
System.out.println(s);
}
}
JAVA IS EVERYWHERE

Más contenido relacionado

La actualidad más candente (20)

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Java threads
Java threadsJava threads
Java threads
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Python strings
Python stringsPython strings
Python strings
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Python list
Python listPython list
Python list
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Data types in java
Data types in javaData types in java
Data types in java
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 

Destacado

String java
String javaString java
String java774474
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi_Kant_Sahu
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanKavita Ganesan
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part VHari Christian
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in javaHitesh Kumar
 
Arrays string handling java packages
Arrays string handling java packagesArrays string handling java packages
Arrays string handling java packagesSardar Alam
 
[Curso Java Basico] Exercicios Aulas 16 17
[Curso Java Basico] Exercicios Aulas 16 17[Curso Java Basico] Exercicios Aulas 16 17
[Curso Java Basico] Exercicios Aulas 16 17Loiane Groner
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with JavaJussi Pohjolainen
 

Destacado (13)

Java strings
Java   stringsJava   strings
Java strings
 
String java
String javaString java
String java
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
String Handling
String HandlingString Handling
String Handling
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part V
 
8. String
8. String8. String
8. String
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Arrays string handling java packages
Arrays string handling java packagesArrays string handling java packages
Arrays string handling java packages
 
Java 06 Strings Arrays
Java 06 Strings ArraysJava 06 Strings Arrays
Java 06 Strings Arrays
 
[Curso Java Basico] Exercicios Aulas 16 17
[Curso Java Basico] Exercicios Aulas 16 17[Curso Java Basico] Exercicios Aulas 16 17
[Curso Java Basico] Exercicios Aulas 16 17
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 

Similar a String in java

What is String in Java?
What is String in Java?What is String in Java?
What is String in Java?RAKESH P
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings ConceptsVicter Paul
 
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Edureka!
 
Java Presentation on the topic of string
Java Presentation  on the topic of stringJava Presentation  on the topic of string
Java Presentation on the topic of stringRajTangadi
 
Introduction java string
Introduction java stringIntroduction java string
Introduction java stringsambhajimeher
 
javaimplementation
javaimplementationjavaimplementation
javaimplementationFaRaz Ahmad
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvmIsaias Barroso
 
Joshua bloch effect java chapter 3
Joshua bloch effect java   chapter 3Joshua bloch effect java   chapter 3
Joshua bloch effect java chapter 3Kamal Mukkamala
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)Akshay soni
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVMRafael Winterhalter
 
Basics of JSON (JavaScript Object Notation) with examples
Basics of JSON (JavaScript Object Notation) with examplesBasics of JSON (JavaScript Object Notation) with examples
Basics of JSON (JavaScript Object Notation) with examplesSanjeev Kumar Jaiswal
 

Similar a String in java (20)

What is String in Java?
What is String in Java?What is String in Java?
What is String in Java?
 
Day_5.1.pptx
Day_5.1.pptxDay_5.1.pptx
Day_5.1.pptx
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
 
Java String
Java StringJava String
Java String
 
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Java Presentation on the topic of string
Java Presentation  on the topic of stringJava Presentation  on the topic of string
Java Presentation on the topic of string
 
Introduction java string
Introduction java stringIntroduction java string
Introduction java string
 
javaimplementation
javaimplementationjavaimplementation
javaimplementation
 
Core_java_ppt.ppt
Core_java_ppt.pptCore_java_ppt.ppt
Core_java_ppt.ppt
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
 
Advance Java
Advance JavaAdvance Java
Advance Java
 
Joshua bloch effect java chapter 3
Joshua bloch effect java   chapter 3Joshua bloch effect java   chapter 3
Joshua bloch effect java chapter 3
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
 
Dynamic Python
Dynamic PythonDynamic Python
Dynamic Python
 
Basics of JSON (JavaScript Object Notation) with examples
Basics of JSON (JavaScript Object Notation) with examplesBasics of JSON (JavaScript Object Notation) with examples
Basics of JSON (JavaScript Object Notation) with examples
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 

Último

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 

Último (20)

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 

String in java

  • 1. IDEAL EYES BUSINESS COLLEGE A PRESENTATION STRING IN JAVA LANGUAGE 1 PERSENTED BY:- VIVEK KUMARPERSENTED TO:- PANKAJ SIR
  • 2. String  Generally string is a sequence of characters. But in java, string is an object.  String class is used to create string object.  string is a 16-bit Unicode character . There are two ways to create String object: 1. By string literal 2. By new keyword
  • 3. string literal For example:- String s1="Welcome"; String s2="Welcome"; //no new object will be created Note:- String objects are stored in a special memory area known as string constant pool inside the Heap memory. String literal is create by double quote. Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object instantiates, then is placed in the pool
  • 4. In the above example only one object will be created . First time JVM will find no string object with the name "Welcome" in string constant pool , so it will create a new object . Second time it will find the string with the name "Welcome" in string constant pool , so it will not create new object whether will return the reference to the same instance.
  • 5. By string literal class String _literal { public static void main(String args[]) { String x ="java"; String x1="java"; System.out.println( "x="+x); } }
  • 6. By new keyword In such case, JVM will create a new String object in normal ( nonpool ) Heap memory and the literal "Welcome" will be placed in the string constant pool . The variable s will refer to the object in Heap( nonpool ). String s=new String("Welcome"); //creates two objects and one reference variale For example:-
  • 7. By new keyword 1. class new_keyword 2. { 3. public static void main(String args[]) 4. { 5. String x ="java"; 6. String s=new String("Welcome"); 7. System.out.println( "x="+s); 8. System.out.println( "x="+x); 9. } 10. }
  • 8. Methods of String class java.lang.String class provides a lot of methods to work on string. Let's see the commonly used methods of String class. 1. charAt() Returns the character located at the specified index 2. concat() Appends one String to the end of another ( "+" also works) 3. equalsIgnoreCase() Determines the equality of two Strings, ignoring case 4. length() Returns the number of characters in a String 5. replace() Replaces occurrences of a character with a new character 6. substring() Returns a part of a String 7. toLowerCase() Returns a String with uppercase characters converted 8. toString() Returns the value of a String 9. toUpperCase() Returns a String with lowercase characters converted 10.trim() Removes whitespace from the ends of a String IMPORTANT METHODS IN THE STRING CLASS
  • 9. charAt() method class charAt { public static void main(String args[]) { String s="Sachin"; System.out.println(s.charAt(5)); System.out.println(s.charAt(3)); } }
  • 10. toUpperCase() and toLowerCase() method class low_upper { public static void main(String args[]) { String s="Sachin"; System.out.println(s.toUpperCase()); System.out.println(s.toLowerCase()); System.out.println(s); } }