The String class is contained in the java.lang package which is automatically called for all classes (so that it does not need to be imported).

The String class produces String objects in a class.

Using strings and threads together, you can make yourself a lovely java jumper.

Since Strings are objects, the normal way to set up a string in a class is to declare it and instantiate it using new.

Eg String string1; //declaration section
string1 = new String (“Hello”); //init or other

However, Strings provide some short cuts:
You can initialise the String when you declare it:
String string1 = “Hello”; //declaration

You can give it a value directly after declaring it
string1 = “Hello”; //init or other

String methods
A number of the string methods are illustrated in the above class. These are:

len = s1.length(); //returns the number of characters in the string
s2 = s1.trim(); //s2 is a copy of s1 with all white characters removed from the
//beginning and end.
s2 = s1.toUpperCase; //s2 is a copy of s1 with all characters in upper case
s2 = s1.toLowerCase; //s2 is a copy of s1 with all characters in lower case.
s2 = s1.replace(‘H’, ‘h’); //s2 is a copy of s1 with all ‘H’ characters replaced by
//the ‘h’ character.

Example 1

This example demonstrates the use of a number of string methods.

import java.applet.Applet;
import java.awt.*;

// This simple class introduces simple string ideas.
// Strings are objects and must be declared,instantiated and given values.
// However, there are some short cuts in setting up a string in a class.

public class StringDemo extends Applet

String s1, s2, s3, s4,
s5 = "How are you ."; //Strings can be initialised when declared

public void init()

s1 = new String (); //null string
s2 = new String ("Hello"); //one way to initialise with a value
s3 = new String (s2); //another way to initialise
s4 = " Hello "; //still another way.

public void paint (Graphics g)

//The following line illustrates concatenation of strings with +
g.drawString(s2 + " What a nice day.", 25,25);

//The following line illustrates the string method length
g.drawString("s2 equals " + s2, 25, 50);
g.drawString("Length of s2 is " + s2.length() + " characters", 25,75);

//Uppercase and lowercase methods. Here we have not
//declared a name for the new strings, so that the new string is
//lost as soon as the drawString method is over.
g.drawString("s2 to UpperCase is " + s2.toUpperCase(), 25, 100);
g.drawString("s2 to LowerCase is " + s2.toLowerCase(), 25,125);

//Trim removes the white spaces on either side of the string.
//In this example we have named the new strings
//so that the new string remains after the drawString method.
s1 = s4.trim();
g.drawString("The original s4 is:" + s4, 25, 150);
g.drawString("After trimming s4 is:" + s1, 25,175);

//replace changes characters
s1 = s2.replace('H', 'h');
g.drawString("s2 after a replace is " + s1, 25, 200);


Example 2

This code uses the indexOf method to find the position of the decimal point in a string.

import java.awt.*;
import java.applet.Applet;

public class StringDemo1 extends Applet

String number;
int place;
public void init()

number = new String("345.67");

public void paint(Graphics g)

g.drawString("The decimal point is character "+place,50,50);



There are a number of other string methods. You will find these in the Java documentation at

1. Java Booklet class notes, Launceston College, Tasmania, private communication.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License