Java Strings - using Strings in practice

jorisschelfaut 21 views 16 slides Jul 11, 2024
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

Short overview of how String objects can be used in Java


Slide Content

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