Home > Java > The Joys of String (Part 1)

The Joys of String (Part 1)

It is a sad fact of the programming world that almost invariably the first thing that you learn to do when learning a new language is engage in writing a program of the ‘Hello world’ variety. If you know Java, you’ll know that the bit that does the work in this will be a line which reads something like:

System.out.println(“Hello World”);

Somehow this saddens me. String handling and manipulation in Java is a thing of joy (albeit one which can be from time to time a curiously sado-masochistic one). How much more fun to do something like

String sMyHelloString = "Hello World";
sMyHelloString = sMyHelloString.reverse();
sMyHelloString = sMyHelloString.reverse();

It lacks the concision of a rudimentary Java Hello World, but it does illustrate something very important: a String variable in Java is a class implementation and the nice manual says this explicitly-

The String class represents character strings. All string literals in Java programs, such as “abc”, are implemented as instances of this class.

Once you understand this, string handling as a concept in Java becomes (conceptually, at least) a snip. You should note also that the Java String class is final. This means that you cannot extend it, i.e. you cannot inherit from it and add your own custom methods or class variables or method overloads. However, Java being the language that it is, this does not prevent you from creating your own class

public class StringMaster
private java.lang.String sString;
// empty constructor for method only access....
public StringMaster()
this.sString = "";
// constructor taking the string you wish to work on....
public StringMaster(String sString)
this.sString = sString;

// setter for String passing....
public void setString(String sTmp)
sString = sTmp;

// and here the new methods to manipulate the String are added... In  this case we'll convert a random string
public StringMaster doTitle(StringMaster sm)
sString = sm.getString().toLowerCase();
int intFirstChar = (int)sString.charAt(0) - 32;
char[] arr = sString.toCharArray();
arr[0] = (char)intFirstChar;
sString = String.valueOf(arr);
return sm;

// and of course a getter to pull the string out again
public String getString()
return sString;


All that is really needed for a framework for this sort of String handling class is a private String variable and a constructor capable of taking a String argument to set this variable plus a getter and a setter. The rest is really down to what you need to do with it. And of course, unlike the String class, it is open to inheritance.

To use this example we could take a phrase like “iN tHE Beginning”

String sExample = "iN tHE Beginning" ;
StringMaster sm = new StringMaster(sExample);
sm = sm.doTitle(sm);

Or for a bit of before and after fun:

String sExample = "iN tHE Beginning" ;
StringMaster sm = new StringMaster(sExample);



  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: