SlideShare uma empresa Scribd logo
1 de 4
Baixar para ler offline
Core Java

Debasish Pratihari

String:


Strings are sequence of character



In java strings are object of
String,StringBuffer or StringBuilder class.



Strings in Java are 16-bit Unicode.



The Java language provides special support for
the string concatenation operator ( + ).



All String representation classes of java are final

Type of String :


Immutable






Mutable




They are read-only, once created can’t
be changed
The String class implements immutable
character Strings.
All string literals in Java programs are
immutable strings
Are dynamic character string. i.e they
can be changed unlike mutable string.
StringBuffer and StringBuilder class
in java implements Mutable String.

Example String :










System.out.println("Lakshya");
String place = "Bhubaneswar";
System.out.println("Lakshya " + place);
String c = "Lakshya".substring(3,6);
String d = place.substring(3, 6);
String name= new String(“Debasish”);
String s1 = new String();
String s3 = new String( name );
char lan[] = { 'j', 'a', 'v',’a’ };
String s4 = new String( lan );

Lecture/core/String/07

Page #1

feel the Technology…
Core Java

Debasish Pratihari

String Literal Pool :


String allocation, like all object allocation, proves costly in
both time and memory. The JVM performs some trickery
while instantiating string literals to increase performance
and decrease memory overhead. To cut down the number
of String objects created in the JVM, the String class
keeps a pool of strings.



Each time your code create a string literal, the JVM
checks the string literal 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. Java
can make this optimization since strings are immutable
and can be shared without fear of data corruption.

String concatenation:


Java Strings can be concatenated (joined) using
the + and += operators to create new Strings.



Every time an operation modifies a String
object, a new read-only String object is created.
String language = "Java";
String course = "Introduction to " + language;
course += ", @ Lakshya";

Comparing String :
Java provides a variety of methods to compare String
objects.


public int compareTo( String str )



public int compareToIgnoreCase( String str )



public boolean equals( Object obj )



public boolean equalsIgnoreCase( String str )



Use of the == operator

Lecture/core/String/07

Page #2

feel the Technology…
Core Java

Debasish Pratihari

Useful String methods :









public
public
public
public
public
public
public
public

char charAt( int index )
String concat( String str )
int length()
String toUpperCase()
String toLowerCase()
String trim()
String substring(int beginIndex)
static String valueOf( xxx b )

String representation of Objects :


All Java objects implement the method toString() which
returns a String object that best "describes" that object.



you do not even need to explicitly call toString(). The
println() method has a version that takes an Object as its
argument. This version will implicitly call toString() for
you and print the result.
Color color = new Color( 255, 0, 0 );
String colorStr = color.toString();
System.out.println( colorStr );
This will printout:
java.awt.Color[r=255,g=0,b=0]

String Interning:





It ensures that no two objects encapsulates the
same string.
This means that if two string variables reference
strings that are identical, the reference must be
identical.
The intern() method will check a string
reference against all the String objects currently
in existence.
If it already exists, the current object will
discarded, and reference to the existing object
will be given.

Lecture/core/String/07

Page #3

feel the Technology…
Core Java

Debasish Pratihari

Creating Mutable String :



StringBuffer str= new StringBuffer(“Exlore java
@ Lakshya”);



StringBuffer duplicate= new StringBuffer(str);



StringBuffer str= new StringBuffer(20);

Manipulating StringBuffer Object:
25%





Setting a Length
o Str.setLength(30);
Adding to existing String
o str.apppend(“new string”);
Finding Position of a substring
o str.lastIndexOf(“new”);
Replacing substring
o str.replace(0,4, “java”);

Things to Remember:


A String object stores a fixed character string
that can’t be changed.



StringBuffer and StringBuilder objects can store
a string of character that you can modify.



Number of character present in a string can be
found using length().



A String object can be created from StringBuffer
by using toString().

Lecture/core/String/07

Page #4

feel the Technology…

Mais conteúdo relacionado

Mais procurados

String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
Ravi Kant Sahu
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
myrajendra
 

Mais procurados (20)

What is String in Java?
What is String in Java?What is String in Java?
What is String in Java?
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
Java String
Java StringJava String
Java String
 
Java Strings
Java StringsJava Strings
Java Strings
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Strings
StringsStrings
Strings
 
Java strings
Java   stringsJava   strings
Java strings
 
Java string handling
Java string handlingJava string handling
Java 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
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Fundamental classes in java
Fundamental classes in javaFundamental classes in java
Fundamental classes in java
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
 

Destaque

Niver Caio - 17.06.07
Niver Caio - 17.06.07Niver Caio - 17.06.07
Niver Caio - 17.06.07
Jubrac Jacui
 
'E-learning for tackling Dementia' Ed Shrager, Alzheimer’s Society
'E-learning for tackling Dementia' Ed Shrager, Alzheimer’s Society'E-learning for tackling Dementia' Ed Shrager, Alzheimer’s Society
'E-learning for tackling Dementia' Ed Shrager, Alzheimer’s Society
Paul McElvaney
 
Coral Exodo - 10.06.07
Coral Exodo - 10.06.07Coral Exodo - 10.06.07
Coral Exodo - 10.06.07
Jubrac Jacui
 
SOFRIMENTO / SUFFERING
SOFRIMENTO / SUFFERINGSOFRIMENTO / SUFFERING
SOFRIMENTO / SUFFERING
Jatsoc .
 
Niver tonight - 19.05.07
Niver tonight - 19.05.07Niver tonight - 19.05.07
Niver tonight - 19.05.07
Jubrac Jacui
 
Wikis google docs
Wikis google docsWikis google docs
Wikis google docs
Irena Nančovska Šerbec
 

Destaque (20)

Selecting A Cms 101
Selecting A Cms 101Selecting A Cms 101
Selecting A Cms 101
 
ZFS - Zettabyte File System
ZFS - Zettabyte File SystemZFS - Zettabyte File System
ZFS - Zettabyte File System
 
Sexuality and disabilites
Sexuality and disabilitesSexuality and disabilites
Sexuality and disabilites
 
A Creative Manifesto: Why the Place You Choose to Live is the Most Important ...
A Creative Manifesto: Why the Place You Choose to Live is the Most Important ...A Creative Manifesto: Why the Place You Choose to Live is the Most Important ...
A Creative Manifesto: Why the Place You Choose to Live is the Most Important ...
 
Niver Caio - 17.06.07
Niver Caio - 17.06.07Niver Caio - 17.06.07
Niver Caio - 17.06.07
 
How... Do you know?
How... Do you know?How... Do you know?
How... Do you know?
 
Scmad Chapter05
Scmad Chapter05Scmad Chapter05
Scmad Chapter05
 
 
Ucapan gb
Ucapan gbUcapan gb
Ucapan gb
 
'E-learning for tackling Dementia' Ed Shrager, Alzheimer’s Society
'E-learning for tackling Dementia' Ed Shrager, Alzheimer’s Society'E-learning for tackling Dementia' Ed Shrager, Alzheimer’s Society
'E-learning for tackling Dementia' Ed Shrager, Alzheimer’s Society
 
Scmad Chapter03
Scmad Chapter03Scmad Chapter03
Scmad Chapter03
 
Coral Exodo - 10.06.07
Coral Exodo - 10.06.07Coral Exodo - 10.06.07
Coral Exodo - 10.06.07
 
SOFRIMENTO / SUFFERING
SOFRIMENTO / SUFFERINGSOFRIMENTO / SUFFERING
SOFRIMENTO / SUFFERING
 
ADMIRÁVEL/ADMIRABLE
ADMIRÁVEL/ADMIRABLEADMIRÁVEL/ADMIRABLE
ADMIRÁVEL/ADMIRABLE
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
刚好
刚好刚好
刚好
 
Niver tonight - 19.05.07
Niver tonight - 19.05.07Niver tonight - 19.05.07
Niver tonight - 19.05.07
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Learning Pool CELEB event
Learning Pool CELEB eventLearning Pool CELEB event
Learning Pool CELEB event
 
Wikis google docs
Wikis google docsWikis google docs
Wikis google docs
 

Semelhante a Lecture 7

javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string class
fedcoordinator
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
Ravi_Kant_Sahu
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
Ravi_Kant_Sahu
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
phanleson
 

Semelhante a Lecture 7 (20)

Day_5.1.pptx
Day_5.1.pptxDay_5.1.pptx
Day_5.1.pptx
 
package
packagepackage
package
 
String handling
String handlingString handling
String handling
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsOCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIs
 
javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string class
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
 
Module-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdfModule-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdf
 
8. String
8. String8. String
8. String
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
07slide
07slide07slide
07slide
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and Interfaces
 
In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...
 
Fileoperations.pptx
Fileoperations.pptxFileoperations.pptx
Fileoperations.pptx
 
Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
Strings.ppt
Strings.pptStrings.ppt
Strings.ppt
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
Strings in java
Strings in javaStrings in java
Strings in java
 

Mais de Debasish Pratihari (18)

Lecture 24
Lecture 24Lecture 24
Lecture 24
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Lecture 21
Lecture 21Lecture 21
Lecture 21
 
Lecture 20
Lecture 20Lecture 20
Lecture 20
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
Lecture 16
Lecture 16Lecture 16
Lecture 16
 
Lecture 14
Lecture 14Lecture 14
Lecture 14
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture25
Lecture25Lecture25
Lecture25
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Lecture 7

  • 1. Core Java Debasish Pratihari String:  Strings are sequence of character  In java strings are object of String,StringBuffer or StringBuilder class.  Strings in Java are 16-bit Unicode.  The Java language provides special support for the string concatenation operator ( + ).  All String representation classes of java are final Type of String :  Immutable     Mutable   They are read-only, once created can’t be changed The String class implements immutable character Strings. All string literals in Java programs are immutable strings Are dynamic character string. i.e they can be changed unlike mutable string. StringBuffer and StringBuilder class in java implements Mutable String. Example String :          System.out.println("Lakshya"); String place = "Bhubaneswar"; System.out.println("Lakshya " + place); String c = "Lakshya".substring(3,6); String d = place.substring(3, 6); String name= new String(“Debasish”); String s1 = new String(); String s3 = new String( name ); char lan[] = { 'j', 'a', 'v',’a’ }; String s4 = new String( lan ); Lecture/core/String/07 Page #1 feel the Technology…
  • 2. Core Java Debasish Pratihari String Literal Pool :  String allocation, like all object allocation, proves costly in both time and memory. The JVM performs some trickery while instantiating string literals to increase performance and decrease memory overhead. To cut down the number of String objects created in the JVM, the String class keeps a pool of strings.  Each time your code create a string literal, the JVM checks the string literal 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. Java can make this optimization since strings are immutable and can be shared without fear of data corruption. String concatenation:  Java Strings can be concatenated (joined) using the + and += operators to create new Strings.  Every time an operation modifies a String object, a new read-only String object is created. String language = "Java"; String course = "Introduction to " + language; course += ", @ Lakshya"; Comparing String : Java provides a variety of methods to compare String objects.  public int compareTo( String str )  public int compareToIgnoreCase( String str )  public boolean equals( Object obj )  public boolean equalsIgnoreCase( String str )  Use of the == operator Lecture/core/String/07 Page #2 feel the Technology…
  • 3. Core Java Debasish Pratihari Useful String methods :         public public public public public public public public char charAt( int index ) String concat( String str ) int length() String toUpperCase() String toLowerCase() String trim() String substring(int beginIndex) static String valueOf( xxx b ) String representation of Objects :  All Java objects implement the method toString() which returns a String object that best "describes" that object.  you do not even need to explicitly call toString(). The println() method has a version that takes an Object as its argument. This version will implicitly call toString() for you and print the result. Color color = new Color( 255, 0, 0 ); String colorStr = color.toString(); System.out.println( colorStr ); This will printout: java.awt.Color[r=255,g=0,b=0] String Interning:     It ensures that no two objects encapsulates the same string. This means that if two string variables reference strings that are identical, the reference must be identical. The intern() method will check a string reference against all the String objects currently in existence. If it already exists, the current object will discarded, and reference to the existing object will be given. Lecture/core/String/07 Page #3 feel the Technology…
  • 4. Core Java Debasish Pratihari Creating Mutable String :  StringBuffer str= new StringBuffer(“Exlore java @ Lakshya”);  StringBuffer duplicate= new StringBuffer(str);  StringBuffer str= new StringBuffer(20); Manipulating StringBuffer Object: 25%     Setting a Length o Str.setLength(30); Adding to existing String o str.apppend(“new string”); Finding Position of a substring o str.lastIndexOf(“new”); Replacing substring o str.replace(0,4, “java”); Things to Remember:  A String object stores a fixed character string that can’t be changed.  StringBuffer and StringBuilder objects can store a string of character that you can modify.  Number of character present in a string can be found using length().  A String object can be created from StringBuffer by using toString(). Lecture/core/String/07 Page #4 feel the Technology…