Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.

Similar presentations


Presentation on theme: "The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to."— Presentation transcript:

1 The Class String

2 String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to store and process strings of characters.  The quoted string “Enter an integer.” is an example of a string.

3 String Constants and Variables (cont’d)  Variables can be declared to be of type String. E.g., String greeting;  Variables of type String can be assigned values using an assignment statement. E.g., greeting = “Hello!”;  Declarations and assignments can be combined. E.g., String greeting = “Hello!”;

4 Concatenation of Strings  Two strings can be connected using the + operator  Connecting two strings together to obtain a larger string is called concatenation.  Example: String greeting = “Hello”; String sentence; sentence = greeting + “ my friend”; System.out.println(sentence);  The above example outputs: Hello my friend

5 Concatenation of Strings (cont’d)  Any number of String objects can be concatenated using the + operator.  In fact, String objects can be concatenated to objects of any type using the + operator.  Example: String solution = “The answer is “; solution = solution + 42; System.out.println(sentence);  The above example outputs: The answer is 42;

6 Classes  In Java, a class is a type whose values are objects.  Objects are entities that store data and can take actions.  The actions that an object can take are called methods.  Most of the methods for the class String return, or produce, some value.

7 Classes (cont’d)  Objects of the class String store data consisting of strings of characters, such as “Hello”.  A method, like length(), returns a value, which in this case is the number of characters in the String object.  For example, “Hello”.length() returns 5, which can be stored in an int variable as follows: int n = “Hello”.length();

8 Classes (cont’d)  A method is called into action by writing a name for the object, followed by a dot, followed by the method name and ending with parentheses.  The act of calling a method into action is said to be invoking or calling the method  The object before the dot is known as the calling object.

9 Classes (cont’d)  Although a method can be called with a constant object, it is more common to use a variable as the calling object.  Example: String greeting = “Hello”; int n = greeting.length();  Information for the method invocation is given in the parentheses. There is nothing inside the parentheses in this case because the length method does not require additional information.

10 Classes (cont’d)  The information inside the parentheses following the method’s name is called the argument (or arguments).  Class types in Java are different from primitive types in that they have methods.  Primitive types are spelled using only lower case letters, but, by convention, class types are spelled with their first letter capitalized, as in String.

11 String Methods  length()  equal(Other_String)  equalsIgnoreCase(Other_String)  toLowerCase()  toUpperCase()  trim()  charAt(Position)

12 String Methods (cont’d)  substring(Start)  substring(Start,End)  indexOf(A_String)  indexOf(A_String,Start)  lastIndexOf(A_String)  compareTo(A_String)

13 String Methods (cont’d)  Many of the methods for the class String depend on counting positions in the string.  Positions are counted starting with 0 not with 1.  Example: In the string “Hi Mom”, ‘H’ is in position 0, ‘i’ is in position 1, the blank character is in position 2, etc.  A position is usually referred to as an index.

14 Escape Characters  In order to have a quotation mark ( “) appear in a string we must precede the quotation mark with a backslash ( \ ).  For example the statement: System.out.println ( “The word \”Java\” names a language, not just a drink!”); will print The word “Java” names a language, not just a drink!

15 Escape Characters (cont’d)  The combination, \”, is treated as a single character, not two characters.  It is called an escape sequence or an escape character.  Since the backslash is used for escape sequences, including a backslash in a string requires placing a double backslash, \\, where a single backslash is wanted.

16 Escape Characters (cont’d)  Similarly, to define a single quote mark ( ‘ ) as a character, an escape sequence is needed.  For example, char singleQuote = ‘\’’;  However, there is no problem using single quotes within strings.

17 The Java Escape Characters \” Double quote \’ Single quote \\ Backslash \n New line. Go to the beginning of the next line. \r Carriage return. Go to the begining of the current line. \t Tab. Add whitespace up to the next tab stop.


Download ppt "The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to."

Similar presentations


Ads by Google