[Java] The String Class

Since we have been dealing with different types of variables, we might as well take a brief look at the String class. So let’s recall from last lesson, almost all of the primitive data types have to deal with numbers. It’s either variables that store whole numbers, or variables that store decimal numbers. The special one is char, you can store letters / symbols in it according to the ACSII code chart.

However, a char variable can only hold one character. What if you want to store a whole line? Like “I Love Cheese”? It is stupid if we have to do something like:

public class Calc {

    public static void main(String[] args) {

        char i = 'i';

        //Yes, you can define a bunch of variables of the same type using this method
        char l = 'l', o = 'o', v = 'v', e = 'e';

        char c = 'c';
        char h = 'h';
        char s = 's';

        char space = ' ';

        //Why do we have to use "" in front of all the char variables?
        //Why if we don't use "" it print out a number?
        //Does this have something to do with casting type?



Yeah, that is definitely not the right way to do it. So here’s where the String class comes in.

String Class? So what is a String? Well, in the CompSci world, a String is an array of character. We haven’t talked about array yet. But you can think of array  as a collection of something. A String is a collection of characters. By combining all the characters together, you get a complete line. So to define the message we have before, you simply do:

 String message = "i love cheese"; 

See? It’s not all that different than defining a primitive type variable. So now, why don’t we try this in a program?

public class Main {

    public static void main(String[] args) {

    System.out.println("I Love Cheese!");

    String message = "I Love Cheese!";



Run it, and you should get two output of the same thing.

Hey, is it just me? Or does it seems that line 5 and line 8 is mathematically the exact same thing? If you substitute the message variable with the codes it equals to, it is the same thing.

Oh I see now, so the double quotation marks, ” “, actually represent a String data. When we do println, we are actually given a choice to print String or numbers. That is why if we throw a char variable in the method without the ” “, it will display a number; a char variable is represented by ASCII code number. On the other hand, if we include the ” “, the system converts the char into a String, thus it can display the actual character or symbols.

So anyway, back to String, like a primitive type variable, it is possible to add Strings together. You can do it by something like this:

String firstName = "John";

String lastName = "Smith";

String fullName = firstName + " " + lastName;

So there you have it. Easy enough? Let’s move on.

Remember we refer to something call the String Class earlier? What is that exactly? What is a class?

We will go more details into what classes are later on. However, for starter, you can think of Class as a synonym for the word Species in our real life. So if we have Species, then we have to have individual, it is call variable. So when we define a String, we did something similar to:

 String firstName; 

Take a look at this, it translate into: we are having an individual call firstName of Species String. Or if you prefer, we can play around with an imaginary class.

 Cat Einstein; 

So what do we have here? We have a Cat call Einstein!

Now of course, when we wrote our codes, we did:

 String firstName = "John"; 

The “John” part, we, or at least I , call that a property of the variable firstName. It is what defined firstName after all. Just like the cat, you can give it a name, age, gender, etc. We will go over that later, String is a different class than most others.

Alight, alright, we got it, let’s move on! Remember that when we print something to screen, we do System.out.println( )? Well let me give you a hint, System is a class.

Oh, so class contain functions or method? Exactly! Some useful methods of the String class are demonstrated below:

//Be Careful not to Name Your Class as String, for it will Override the Built-in String Class
//And you will have a mess!

public class StringExample {

    public static void main(String[] args) {

    //Combining Strings
    String firstName = "John";
    String lastName = "Smith";
    String fullName = firstName + " " + lastName;

    //String Search
    //The indexOf method return the location of the String you are searching for.
    //In an array, index # start at 0, then count off by one
    //-1 will be returned if it can't find the String.
    int firstNameLocation = fullName.indexOf("John");
    int lastNameLocation = fullName.indexOf("Smith");
    int dontExistLocation = fullName.indexOf("This String Actually Don't Exist");
    System.out.println("The First Name is located at index "+firstNameLocation+" of the fullName String!");
    System.out.println("The Last Name is located at index "+lastNameLocation+" of the fullName String!");
    System.out.println("The String that Don't Exist is located at index "+dontExistLocation+" of the fullName String!");

    //Returning a single Character at Index
    char letter = firstName.charAt(1);

    //Taking part of the original String
    //There's 2 way to do a subString, with either 1 or 2 arguments
    //With 1, it mark the beginning index, and it you return a String starting at that index to the end.
    //With 2, it mark the beginning and ending index, and will return that chunk of the original String.
    String tempFirst = fullName.substring(0,4);
    String tempLast = fullName.substring(lastNameLocation);
    System.out.println( tempFirst + " " + tempLast);



And you should get:

John Smith

The First Name is located at index 0 of the fullName String!
The Last Name is located at index 5 of the fullName String!
The String that Don't Exist is located at index -1 of the fullName String!


John Smith

So above are just a couple methods that I found very useful in the String class. I am actually writing a production labor management program at GE Aviation right now that centers around that couple methods. They are useful, so learn it 😀

To learn more about the different methods in the String class, or any other classes, you can define a String variable in Netbeans, then type that variable name follow by a period. A menu should pop up with all the available methods and what arguments to put in. Arguments are basically information that you past on to a method for it to do its job. For example you pass a string to System.out.println( ) for it to print the string tot he screen.

Another way, you can visit the Java Doc, it is a very useful resource from the Java Developers.



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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s