SlideShare a Scribd company logo
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
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
PPT
String handling session 5
PPTX
Java Strings.pptxJava Strings.pptxJava Strings.pptx
PPT
String classes and its methods.20
PPTX
Strings in Java
PPTX
Java Strings
DOCX
Java R20 - UNIT-5.docx
PPSX
String and string manipulation x
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
String handling session 5
Java Strings.pptxJava Strings.pptxJava Strings.pptx
String classes and its methods.20
Strings in Java
Java Strings
Java R20 - UNIT-5.docx
String and string manipulation x

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

PDF
Java R20 - UNIT-5.pdf
PPTX
Introduction to Java Strings, By Kavita Ganesan
PPT
String and string manipulation
PPTX
String in JAVA --------------------------
PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
PPT
PPT
JavaYDL9
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
PDF
Java - Strings Concepts
PPTX
String in java, string constructors and operations
PPTX
Java String
PPTX
Day_5.1.pptx
PPTX
Java string handling
PPTX
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
PDF
Java Presentation on the topic of string
PPTX
Java string handling
PPT
Java căn bản - Chapter9
PPTX
String Handling, Inheritance, Packages and Interfaces
PPT
Chapter 9 - Characters and Strings
PPT
String Handling
Java R20 - UNIT-5.pdf
Introduction to Java Strings, By Kavita Ganesan
String and string manipulation
String in JAVA --------------------------
Strings in javamnjn ijnjun oinoin oinoi .ppt
JavaYDL9
Charcater and Strings.ppt Charcater and Strings.ppt
Java - Strings Concepts
String in java, string constructors and operations
Java String
Day_5.1.pptx
Java string handling
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
Java Presentation on the topic of string
Java string handling
Java căn bản - Chapter9
String Handling, Inheritance, Packages and Interfaces
Chapter 9 - Characters and Strings
String Handling
Ad

Recently uploaded (20)

PDF
A Day in the Life of Location Data - Turning Where into How.pdf
PDF
Dell Pro 14 Plus: Be better prepared for what’s coming
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
How to Build Crypto Derivative Exchanges from Scratch.pptx
PDF
DevOps & Developer Experience Summer BBQ
PPTX
Belt and Road Supply Chain Finance Blockchain Solution
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
PDF
Event Presentation Google Cloud Next Extended 2025
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
ai-archetype-understanding-the-personality-of-agentic-ai.pdf
A Day in the Life of Location Data - Turning Where into How.pdf
Dell Pro 14 Plus: Be better prepared for what’s coming
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
How to Build Crypto Derivative Exchanges from Scratch.pptx
DevOps & Developer Experience Summer BBQ
Belt and Road Supply Chain Finance Blockchain Solution
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
Transforming Manufacturing operations through Intelligent Integrations
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Smarter Business Operations Powered by IoT Remote Monitoring
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
Event Presentation Google Cloud Next Extended 2025
Chapter 2 Digital Image Fundamentals.pdf
Sensors and Actuators in IoT Systems using pdf
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
Chapter 3 Spatial Domain Image Processing.pdf
ai-archetype-understanding-the-personality-of-agentic-ai.pdf
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.