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

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

PPTX
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
PPT
String and string manipulation
Shahjahan Samoon
 
PPTX
String in JAVA --------------------------
2003sayanch
 
PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
ShahidSultan24
 
PPT
09slide
Dorothea Chaffin
 
PPT
JavaYDL9
Terry Yoast
 
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
PDF
Java - Strings Concepts
Victer Paul
 
PPTX
String in java, string constructors and operations
manjeshbngowda
 
PPTX
Java String
SATYAM SHRIVASTAV
 
PPTX
Day_5.1.pptx
ishasharma835109
 
PPTX
Java string handling
Salman Khan
 
PPTX
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
mohithn2004
 
PDF
Java Presentation on the topic of string
RajTangadi
 
PPTX
Java string handling
GaneshKumarKanthiah
 
PPT
Java căn bản - Chapter9
Vince Vo
 
PPTX
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
PPT
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
PPT
String Handling
Bharat17485
 
PPTX
String handling
ssuser20c32b
 
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
String and string manipulation
Shahjahan Samoon
 
String in JAVA --------------------------
2003sayanch
 
Strings in javamnjn ijnjun oinoin oinoi .ppt
ShahidSultan24
 
JavaYDL9
Terry Yoast
 
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
Java - Strings Concepts
Victer Paul
 
String in java, string constructors and operations
manjeshbngowda
 
Java String
SATYAM SHRIVASTAV
 
Day_5.1.pptx
ishasharma835109
 
Java string handling
Salman Khan
 
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
mohithn2004
 
Java Presentation on the topic of string
RajTangadi
 
Java string handling
GaneshKumarKanthiah
 
Java căn bản - Chapter9
Vince Vo
 
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
String Handling
Bharat17485
 
String handling
ssuser20c32b
 

Recently uploaded (20)

PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Software Development Company Keene Systems, Inc (1).pdf
Custom Software Development Company | Keene Systems, Inc.
 
PDF
NASA A Researcher’s Guide to International Space Station : Fundamental Physics
Dr. PANKAJ DHUSSA
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
PDF
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pdf
ghjghvhjgc
 
PDF
Next Generation AI: Anticipatory Intelligence, Forecasting Inflection Points ...
dleka294658677
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
Talbott's brief History of Computers for CollabDays Hamburg 2025
Talbott Crowell
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
Manual Testing for Accessibility Enhancement
Julia Undeutsch
 
PDF
Evolution: How True AI is Redefining Safety in Industry 4.0
vikaassingh4433
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Software Development Company Keene Systems, Inc (1).pdf
Custom Software Development Company | Keene Systems, Inc.
 
NASA A Researcher’s Guide to International Space Station : Fundamental Physics
Dr. PANKAJ DHUSSA
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Role_of_Artificial_Intelligence_in_Livestock_Extension_Services.pptx
DrRajdeepMadavi
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pdf
ghjghvhjgc
 
Next Generation AI: Anticipatory Intelligence, Forecasting Inflection Points ...
dleka294658677
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Talbott's brief History of Computers for CollabDays Hamburg 2025
Talbott Crowell
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Manual Testing for Accessibility Enhancement
Julia Undeutsch
 
Evolution: How True AI is Redefining Safety in Industry 4.0
vikaassingh4433
 
Digital Circuits, important subject in CS
contactparinay1
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
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.