Sie sind auf Seite 1von 6

Alistair C.

Rooney 2002 All rights reserved

Java for ABAP programmers Lesson 7


TM

Lesson 7 Strings with Java


Dealing with text in some way, shape or form is fundamental to any programming language. Im not going to cover the Character class here. I believe this is something easily researched on your own. This is different to the char primitive data type and it is one example of a Wrapper class. Strings can be dealt with using three classes, which you need to get familiar with in your APIs. (See sidebar).
The Java API Documentation One of the nicest things about Java is that you can easily see the classes you have available to you and the methods you can use in these classes. Download and unzip the documentation for your JDK release. The documentation is in HTML format and it has a list of all the classes down the left hand side and the class detail, including the methods on the right hand side. The HTML file you need to look at is index.htm in the API directory. Get familiar with this! Youll be using it a lot.

The important thing to note here is that this is the first time we have encountered a Class that will do our dirty work for us. Other languages like ABAP and C use strings as data
Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

Alistair C. Rooney 2002 All rights reserved

types. The bonus with Java is that we can use some very powerful methods to work with strings. Now heres a catch. Strings are immutable in Java. This means that you cannot change a String. Before you throw your hands up in horror let me explain that a little. Ive had students who say to me You lied to me this works fine and they show me the following code: String myString = Howdy Podner; myString = Hey yall; After they have finished swearing at me, I tell them that they have not changed the string myString, they have replaced it entirely, which is a very different thing. Declaring a String Formally our declaration should look like this: String myString = new String(Howdy); This is called instantiation in Java. More on this in later lessons. However, and this is a rather big however, we can declare a string in a shorthand way in Java. We do not have this privilege with other classes in Java. (OK, there are one or two but Im not telling). The shorthand declaration is the one weve seen above: String myString = Howdy Podner; Concatenating Strings Joining or concatenating strings in Java is very easy. Although we may not overload operators in Java, Sun have done one for us. The + sign is used to concatenate two Strings, a char and a String, and just about any other primitive data type. Lets see an example: String helloString = Hello; String worldString = World; System.out.println(helloString + worldString); Or even String myName = Alistair;
Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

Alistair C. Rooney 2002 All rights reserved

System.out.println(myName+ is +41); Which will print out: Alistair is 41. The String methods There are over 50 methods in the String class. Obviously it would be a little silly to cover all of them. Im going to cover a few basics. Youre job is to research the others in the API documentation. Ill introduce each with some code fragments. The charAt method. String myString = Welcome; char firstChar = myString.charAt(3); Important Note: The charAt method requires an integer. This integer is the index of the string. A strings index always starts at 0. As you may have guessed, the charAt method returns a character. We have asked it to return a character at position 3 which will be c. Since this is the first time we have seen a class call, lets direct our attention to the dot operator. This is very similar to => in ABAP. We have the object (reference) myString. We know we want to use the charAt method. So we join the two using a dot. I.e. Object.method(). (Dont forget the brackets). The substring method. String catMat = The Cat sat on the Mat; String catString = catMat.substring(4,7); The catch with this method is that the start index is normal but the end index requires that you add 1. So to return a string Cat we start at 4 and end at 6+1 or 7. As with charAt there is more than one variant of this method depending on the parameters you want to use. The equals method. String first = First String; String second = Next one;

Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

Alistair C. Rooney 2002 All rights reserved

if (first.equals(second)) { System.out.println(they are equal!); } Hold on. Hold on. Why cant I just use first = = second? Well Im glad you asked! Well the answer is, you are not comparing two Strings, you are in fact comparing two object references, which will probably not be equal even if the String values are. So if you compare any two object references you should use the equals method. The length method. The length method is very useful to (surprise!) determine the length of the String. String longOne = This String is longer than the others; If (longOne.length() > 20) { do some code here } You may want to use the length in a loop (we will cover these soon!) but be aware that method calls within loops have a performance overhead. Rather extract the length outside of the loop. Thats all on string methods. I strongly recommend you read through the APIs to see what methods you have available to you. Lets look at the StringBuffer class next.

StringBuffer
When using strings the one drawback is that they are immutable i.e. they cannot be altered in situ. The solution to this problem (on the rare occasion it becomes a problem) is to use the StringBuffer class instead of a string or as well as a string. When using StringBuffer you are free to change the string. Once again I will be looking at a small selection of the available methods and I encourage you to read through the APIs.

Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

Alistair C. Rooney 2002 All rights reserved

The first thing I need to mention is that if there is a String method to do something, then there is probably a StringBuffer method as well. A good example of this is the charAt() method. Let us concentrate on the methods that are peculiar to the StringBuffer class. The append method. The StringBuffer class must be fully instantiated. StringBuffer sb = new StringBuffer(my jolly string); sb = sb.append(s); The character s will be appended to the end of the string and the instance sb will now contain the string my jolly strings. Bear in mind that you can append floats, doubles, integers and other data types. Have a quick peep at the append methods in the APIs. Of course java gives you the ability to insert a data type at a specified position instead of at the end. To do this use : The insert method. Using the same example above and assuming the instance sb has been constructed, we could say: sb = sb.insert(1, e); The first parameter is the offset (from 0), which must be an integer and the second is the character (in this case) that I want to insert. The end result of this operation will have sb containing the string me jolly string. The other methods in StringBuffer are pretty much self explanatory. The reverse method does just that. It reverses a String like abcdefg and changes the buffer to gfedcba. The toString method is handy, but since the String class takes a StringBuffer as an argument in the constructer we could just say: String newString = sb; Finally I want to mention the StringTokenizer class because of its useful characteristics.

Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.

Alistair C. Rooney 2002 All rights reserved

StringTokenizer
I dont want you to spend longer than a few minutes on this class. It is useful and bears mentioning, but it is not used that often. StringTokenizer will allow the program to run in a loop examining the contents of a string and breaking it up into separate strings based on the delimiter that was specified. This is great for parsing XML files and the like. You must loop through your string (more on loops in future lessons) and identify each substring between the delimiters. Heres a quick example: String textExample = #First String#Second String#Third one; StringTokenizer st = new StringTokenizer(textExample,#); while (st.hasMoreTokens()) { String theToken = st.nextToken(); System.out.println(theToken); } Note the usage of the hasMoreTokens() method to check whether we have any left and the nextToken() method to return the next Token (strangely enough!) in the String. Our little code snippet here will return the following: First String Second String Third one Thats all for this Lesson folks! Come back next week where we talk about Control Flow.

Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.