

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to the string type in java, explaining that it is a class type consisting of a sequence of characters. It covers the use of double quotes to create string literals, the concept of catenation (concatenation) to join two strings, and the use of escape characters to represent special characters. Additionally, it introduces several functions for working with strings, including length, charat, substring, and equality tests.
Typology: Exams
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Type String Type String is different from primitive types int and double. String is a class type. We explain what this means later, after we have explained what a class is. Each value of type String is a sequence of characters. We write a value by enclosing the sequence of characters within double quotes ". You can see in the interactions pane of DrJava that these literals evaluate to themselves. The second example contains a capital letter and three occurrences of the space, or blank character, which is a character. The last example shows the empty string , the string containing no characters. "first" "The price is $25.00." "5 <= 20" "" Use a new-line character "\n" to indicate a jump to a new line. For example, notice how evaluation of "123\n4" appears on two lines. The new-line character begins with the so-called escape character, the backslash. Use the escape character to write the backslash itself, as well as the double quote character. For example, note how the String literal "1\2"3" evaluates to a String containing can contain the characters
The position or index of a character Two functions are used to get at subparts of a String, To understand these, you have to know that the first character of a String is at position 0, the second is at position 1, ..., and so on. So, numbering of the characters begins at 0 and not at 1! The position is more frequently called the index of the character. charAt. For a String s, s.charAt(i) gives the character at position i of s. Here are examples. "Truth".charAt(0) // yields 'T' "Truth".charAt(4) // yields 'h' For t.charAt(i) to be legal, i must be a valid position number, or index. If not, execution aborts with an error message. For example, "T".charAt(2) causes a StringIndexOutOfBoundsException, giving the invalid index. So be careful. Whenever you write a function call charAt(i), make sure i is in the appropriate range. substring. Function substring extracts a substring of a string. For example, "0123456".substring(1,4); extracts the string consisting of the characters at positions 1, 2, and 3, but not 4. The first argument says where to start. The second argument gives the index of the character after the last one to be extracted. If you leave off the second argument, the result contains every character from the first argument to the end of the string:” "harmony".substring(2); // yields "rmony" A substring call with equal arguments yields the empty string "". "harmony".substring(2,2) is "" You can even ask for an empty substring of the empty string: "".substring(0,0) yields "". equality. Please don’t use s1 == s2 to test whether strings s1 and s2 are the same, because it won’t work the way you think —because String is a class type. Again, you’ll have to wait until we discuss classes to understand this. Here’s an example to show that == doesn’t work: "abc" == "ab" + "c" // is false The proper way to test equality of strings is to use function equals: "abc".equals("ab" + "c") // is true 2