Java Strings
Using Strings in practice
Java String
• Character sequence
• Immutable
• Class defined in the java.lang package
• Instantiate a String Object:
• Using a String literal (series of characters enclosed in double quotes)
String s1 = “Cat”;
• Via the String constructor (see API for different constructors)
String s2 = new String(“Cat”);
Java String – Memory
• Output of the following code?
Java String – Memory
• Output of the following code?
s1 == s2 :true
s1 == s3 :false
Java String – Memory
• Java Memory Heap as a Java
Object
String s2 = new String(“Cat”);
• String pool in the Java Memory
Heap
• String interning
• Flyweight pattern
String s1 = “Cat”;
Java String – API
• Extends java.lang.Object
• E.g.: equals(other: Object) : boolean, hashCode() : int, …
• Implements: Serializable, CharSequence, Comparable<String>
• E.g.: compareTo(other: String) : int, charAt(index: int) : char, …
• String specific API
• E.g.: isEmpty() : boolean, …
StringBuilder
• Mutable
StringBuffer
• Mutable
• Thread-safe
• Slower
Java String – Formatting
• String.format
• Conversion specifiers: %d (decimal integer), %f (floating point), %c (any character), …
• Many more options (see documentation)
String fs;
fs = String.format("Variable 1 (%f) is of type float, while " +
"the 2nd variable (%d) is an integer, " +
"and the 3rd one, %s, is a string...",
floatVar, intVar, stringVar);
• Formatter class (java.util)
StringBuilder sbuf = new StringBuilder();
Formatter fmt = new Formatter(sbuf);
fmt.format("PI = %f%n", Math.PI);
System.out.print(sbuf.toString());
// you can continue to append data to sbuf here.
Use cases
• String instantiation
• String concatenation
• String duplication
• String manipulation
• …
=> Questions:
• Performance?
• Thread-safe?
• …
Use cases – Instantiation
• Benchmarking:
• String Object: new (with various constructors), literal
• StringBuffer constructor(s)
• StringBuilder constructor(s)
Use cases – Concatenation
• Benchmarking:
• String Object: Operator +, concat method
• StringBuffer append
• StringBuilder append
• …
Conclusion
• Creating Java String objects
• Concatenating strings
Discussion / Questions?
References
• Java String
• https://docs.oracle.com/javase/tutorial/java/data/strings.html
• Java String – Memory:
• https://www.journaldev.com/797/what-is-java-string-pool
• https://en.wikipedia.org/wiki/String_interning
• https://en.wikipedia.org/wiki/Flyweight_pattern
• https://www.javatpoint.com/how-to-optimize-java-string-creation
• Java String – API
• https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
References
• Java String - Formatting
• https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html
• https://dzone.com/articles/java-string-format-examples
• StringBuilder
• StringBuffer

More Related Content

PPTX
Java String
PPTX
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
PPTX
Module-5-hjhjhjkhdjkhjhjhhhjhjhhhhj.pptx
DOCX
Fundamental classes in java
PPTX
Java String Handling
PPSX
String and string manipulation x
PPTX
PPT on Python - illustrating Python for BBA, B.Tech
ODP
Dynamic Python
Java String
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
Module-5-hjhjhjkhdjkhjhjhhhjhjhhhhj.pptx
Fundamental classes in java
Java String Handling
String and string manipulation x
PPT on Python - illustrating Python for BBA, B.Tech
Dynamic Python

Similar to Java Strings - using Strings in practice (20)

PPT
07slide
PDF
Java R20 - UNIT-5.pdf
PDF
Scala in Places API
PDF
The Swift Compiler and Standard Library
PPTX
Strings in Java
PPT
String and string manipulation
PPT
String Handling
PPTX
Java Strings.pptxJava Strings.pptxJava Strings.pptx
DOCX
Java R20 - UNIT-5.docx
PPT
String classes and its methods.20
KEY
Static or Dynamic Typing? Why not both?
PPTX
DAY_1.3.pptx
PDF
Introduction to Scala
PPTX
Scala for curious
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
PDF
Spark schema for free with David Szakallas
PDF
Programming Android Application in Scala.
PPT
Text processing
PPTX
In the given example only one object will be created. Firstly JVM will not fi...
PDF
07slide
Java R20 - UNIT-5.pdf
Scala in Places API
The Swift Compiler and Standard Library
Strings in Java
String and string manipulation
String Handling
Java Strings.pptxJava Strings.pptxJava Strings.pptx
Java R20 - UNIT-5.docx
String classes and its methods.20
Static or Dynamic Typing? Why not both?
DAY_1.3.pptx
Introduction to Scala
Scala for curious
Charcater and Strings.ppt Charcater and Strings.ppt
Spark schema for free with David Szakallas
Programming Android Application in Scala.
Text processing
In the given example only one object will be created. Firstly JVM will not fi...
Ad

Recently uploaded (20)

PPTX
The various Industrial Revolutions .pptx
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
STKI Israel Market Study 2025 version august
PPTX
Tartificialntelligence_presentation.pptx
PDF
A novel scalable deep ensemble learning framework for big data classification...
DOCX
search engine optimization ppt fir known well about this
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PDF
Five Habits of High-Impact Board Members
PDF
Architecture types and enterprise applications.pdf
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PPT
What is a Computer? Input Devices /output devices
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
The various Industrial Revolutions .pptx
Developing a website for English-speaking practice to English as a foreign la...
Taming the Chaos: How to Turn Unstructured Data into Decisions
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
STKI Israel Market Study 2025 version august
Tartificialntelligence_presentation.pptx
A novel scalable deep ensemble learning framework for big data classification...
search engine optimization ppt fir known well about this
Final SEM Unit 1 for mit wpu at pune .pptx
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Module 1.ppt Iot fundamentals and Architecture
Univ-Connecticut-ChatGPT-Presentaion.pdf
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
Five Habits of High-Impact Board Members
Architecture types and enterprise applications.pdf
1 - Historical Antecedents, Social Consideration.pdf
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
What is a Computer? Input Devices /output devices
A contest of sentiment analysis: k-nearest neighbor versus neural network
Ad

Java Strings - using Strings in practice

  • 2. Java String • Character sequence • Immutable • Class defined in the java.lang package • Instantiate a String Object: • Using a String literal (series of characters enclosed in double quotes) String s1 = “Cat”; • Via the String constructor (see API for different constructors) String s2 = new String(“Cat”);
  • 3. Java String – Memory • Output of the following code?
  • 4. Java String – Memory • Output of the following code? s1 == s2 :true s1 == s3 :false
  • 5. Java String – Memory • Java Memory Heap as a Java Object String s2 = new String(“Cat”); • String pool in the Java Memory Heap • String interning • Flyweight pattern String s1 = “Cat”;
  • 6. Java String – API • Extends java.lang.Object • E.g.: equals(other: Object) : boolean, hashCode() : int, … • Implements: Serializable, CharSequence, Comparable<String> • E.g.: compareTo(other: String) : int, charAt(index: int) : char, … • String specific API • E.g.: isEmpty() : boolean, …
  • 9. Java String – Formatting • String.format • Conversion specifiers: %d (decimal integer), %f (floating point), %c (any character), … • Many more options (see documentation) String fs; fs = String.format("Variable 1 (%f) is of type float, while " + "the 2nd variable (%d) is an integer, " + "and the 3rd one, %s, is a string...", floatVar, intVar, stringVar); • Formatter class (java.util) StringBuilder sbuf = new StringBuilder(); Formatter fmt = new Formatter(sbuf); fmt.format("PI = %f%n", Math.PI); System.out.print(sbuf.toString()); // you can continue to append data to sbuf here.
  • 10. Use cases • String instantiation • String concatenation • String duplication • String manipulation • … => Questions: • Performance? • Thread-safe? • …
  • 11. Use cases – Instantiation • Benchmarking: • String Object: new (with various constructors), literal • StringBuffer constructor(s) • StringBuilder constructor(s)
  • 12. Use cases – Concatenation • Benchmarking: • String Object: Operator +, concat method • StringBuffer append • StringBuilder append • …
  • 13. Conclusion • Creating Java String objects • Concatenating strings
  • 15. References • Java String • https://docs.oracle.com/javase/tutorial/java/data/strings.html • Java String – Memory: • https://www.journaldev.com/797/what-is-java-string-pool • https://en.wikipedia.org/wiki/String_interning • https://en.wikipedia.org/wiki/Flyweight_pattern • https://www.javatpoint.com/how-to-optimize-java-string-creation • Java String – API • https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
  • 16. References • Java String - Formatting • https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html • https://dzone.com/articles/java-string-format-examples • StringBuilder • StringBuffer

Editor's Notes

  • #3: https://www.journaldev.com/1321/java-string-interview-questions-and-answers#java-string https://docs.oracle.com/javase/tutorial/java/data/strings.html
  • #6: When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with the same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool. When we use the new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.
  • #7: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html char : charAt(int index) Returns the char value at the specified index. String : concat(String str) Concatenates the specified string to the end of this string. boolean : contains(CharSequence s) Returns true if and only if this string contains the specified sequence of char values. int : indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. String : intern() Returns a canonical representation for the string object. Boolean : isEmpty() Returns true if, and only if, length() is 0. …
  • #9: The string is immutable and final in Java, so whenever we do String manipulation, it creates a new String. String manipulations are resource consuming, so java provides two utility classes for String manipulations – StringBuffer and StringBuilder. StringBuffer and StringBuilder are mutable classes. StringBuffer operations are thread-safe and synchronized where StringBuilder operations are not thread-safe. So in a multi-threaded environment, we should use StringBuffer but in the single-threaded environment, we should use StringBuilder. StringBuilder performance is fast than StringBuffer because of no overhead of synchronization.