Object-Orientation - String & Related Classes (in Java)
We are all instruments endowed with feeling and memory. Our senses are so many strings that are struck by surrounding objects and that also frequently strike themselves.
- Overview
- String Class
- Immutability
- Character Class
- StringBuilder Class
- Apache Commons Lang
- Apache Commons Text
- Apache Commons CSV
- Conclusions
Overview
Concept
The term ‘String’, used in programming to indicate a ‘string of characters’, first came into regular usage in the mid 1960’s, as languages such as Lisp allowed textual data processing to flourish. A more general meaning, to order a series of mathematical values, as if beads on a string, dates possibly to the 1930’s.
- We will reframe our existing understanding of Strings in light of our newfound mastery of object-oriented programming.
String Class
Instantiation
Like all objects in Java, Strings can be instantiated using the new
operator and passing the constructor a char
array.
char[] charr = { 'w','o','r','r','y',' ', 'n','o','t' };
String myMessageToYou = new String(charr);
- To make Strings appear more like primitive values, i.e. to simplify their overly verbose usage, Java offers syntactic sugar for their instantiation.
String myMessageToYou = "worry not";
How to get a String object
Rather than directly instantiating a String object, there are many operations and functions in the Java API that instantiate new String objects on our behalf, e.g.
-
myMessageToYou.concat(", weary heads");
// “worry not, weary heads” -
myMessageToYou.substring( 0, myMessageToYou.indexOf(' ') );
// “worry” -
myMessageToYou.toUpperCase();
“WORRY NOT” -
myMessageToYou.toLowerCase();
“worry not” -
myMessageToYou.replace("worry", "fear");
// “fear not” -
String.format( " '%s', he said to the child. ", myMessageToYou )
// “ ‘worry not’, he said to the child. “
How to get a String object (continued)
Of course, the +
operator can be used to concatenate two Strings:
String myMessageToYou = "worry" + " " + "not";
-
This may seem odd… you can’t typically use the
+
operator with object operands. -
Yet again, this is Java syntactic sugar to make up for the burden of dealing with non-primitive Strings. Behind the scenes, Java will use one of the String functions to implement this operation, e.g.
"worry".concat(" ").concat("not")
.toString( ) methods
And every class can optionally declare a method named toString()
that indicates how an object of that class should be converted to a String.
public class Moped {
// ...
public String toString() {
return String.format(
"A moped at %s & %s facing %s",
this.getStreet(),
this.getAvenue(),
this.getOrientation()
)
}
// ...
}
// ... assume we instantiated a Moped object ...
System.out.println( myMoped ); // output it as a String
.toString( ) methods (continued)
Even if you don’t define a .toString()
method in your custom classes, one exists automatically anyway.
-
How does this work?!
-
Every object automatically inherits some properties and methods from the oldest ancestor class in the Java API named
Object
. -
toString()
is one of those. -
Here is
Object
’s implementation of itstoString()
method.
//...
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
//...
What’s inside a String?
There are several useful functions for investigating the internals of a String.
-
int num = myMessageToYou.length();
-
char c = myMessageToYou.charAt(5);
-
char[] charr = myMessageToYou.toCharArray();
-
int num = myMessageToYou.indexOf(" ");
-
boolean isInThere = myMessageToYou.contains("not");
-
boolean startsRight = myMessageToYou.startsWith("worry");
-
boolean endsRight = myMessageToYou.endsWith("not");
Comparing two Strings
Comparing two strings can be done in a few different styles.
-
(myMessageToYou == "worry not")
-
myMessageToYou.equals("worry not")
-
myMessageToYou.equalsIgnoreCase("WORRY NOT")
Immutability
Concept
String objects are immutable - once they have been instantiated, their contents cannot be changed.
-
Creating immutable objects is quite simple in Java.
-
One can create a class with a constructor that sets one or more private properties.
-
So long as there are no public
setter
methods provided, those private properties can be frozen at their initial values.
public class ImmutableDog {
public ImmutableDog( String name, String breed, int age ) {
this.setName(name);
this.setBreed(breed);
this.setAge(age);
}
// ... assume we define private setter methods
}
Character Class
Concept
The Character class is a wrapper class, used to do processing and analysis of char
primitive values, e.g.
-
char[] a = "my, my".toCharArray();
-
Character x = new Character(a[4]);
-
x.equals('m')
-> true, of course -
Character.isDigit(a[3])
-> false, of course -
Character.isLetter(a[3])
-> true, of course -
Character.toUpperCase(a[3])
-> ‘L’ -
Character.isUpperCase(a[3])
-> false
StringBuilder Class
Concept
Since Strings are immutable, concatening many Strings together or modifying the contents of a String tends to require a lot of code and more memory than it should. Enter StringBuilder.
-
StringBuilder is an easy-to-use, memory-efficient class for mutating text.
-
StringBuilder myBuilder = new StringBuilder("Welcome");
-
int num = myBuilder.length();
-
myBuilder.append(" to class!");
// it’s mutable! -
myBuilder.insert( 11, "this wonderful " );
// it’s easy to insert -
myBuilder.delete( 16, 25 );
// it’s easy to delete parts -
myBuilder.reverse();
// because we can
Extracting the String from StringBuilder
Converting the contents of a StringBuilder to a regular String is a facile matter.
-
myBuilder.toString()
-
myBuilder.substring(11, 15)
Apache Commons Lang
Patching the Java API
Java, remarkable though it is, is lacking in many basic functionality today’s programmers might expect. Apache Commons Lang attempts to fill in many of those gaps.
StringUtils is one class in the Apache Commons Lang library that might be of interest to us here.
-
to use it, you can download a compiled (binary) version of the library.
-
unzip it and place the main
.jar
file into your project’slib
directory. -
then in code, import it with
import org.apache.commons.lang3.StringUtils;
StringUtils Examples
// align the text "foo" within 10 spaces
String s1 = StringUtils.center("foo", 10); // -> " foo "
String s2 = StringUtils.leftPad("foo", 10); // -> "foo "
String s3 = StringUtils.rightPad("foo", 10); // -> " foo"
// check whether a string is uppercase or has all alphabetic or numeric characters
boolean isUps = StringUtils.isAllUpperCase("help"); // -> false
boolean isAlphas = StringUtils.isAlpha("abcdef"); // -> true
boolean isNums = StringUtils.isNumeric("12345"); // -> true
boolean isAlphaNums = StringUtils.isAlphanumeric("abcdef12345"); // -> true
// join an array
String[] foo = {"foo", "bar", "baz", "bum"};
String s = StringUtils.join(foo, ","); // -> "foo,bar,baz,bum"
Full documentation of StringUtils here.
Apache Commons Text
Concept
Similar to Commons Lang, Apache’s Commons Text project aims to give developers access to common string algorithms and processes that are not supported by the Java API or the various text-related classes added by Commons Lang.
Classes
Classes include:
-
StringSubstitutor
- a class for substituting variables within a String. -
WordUtils
- a class containing methods specific to text composed of words, for example capitalization changes of the first letter of each word or line-wrapping text based on word boundaries. -
StrTokenizer
- contains methods to tokenize (i.e. parses or split) a string based on delimiters and pattern matching. -
StringEscapeUtils
- a class containing methods to escape and unescape Java, JavaScript, HTML and XML. -
various classes to help calculate similarity or difference between two Strings.
Apache Commons CSV
Concept
Commons CSV is another project by Apache to make dealing with CSV (comma- or character-separated values) text files easier.
-
CSV text files are those plain text files that store data in lines, where the values in each line are separated by a standard separator (often - but not always - a comma
,
character) -
Commons CSV can parse data in files following standard CSV format, Excel format, and other similar formats.
-
As with Commons Lang, the main
.jar
file of Commons Text must be added as a dependency to a project, such as by placing it within thelib
directory.
Conclusions
You now have increased your knowledge of the String and related classes as examples of typical object-oriented Java classes.
- Thank you. Bye.