Presentation is loading. Please wait.

Presentation is loading. Please wait.

String Objects & its Methods

Similar presentations


Presentation on theme: "String Objects & its Methods"— Presentation transcript:

1 String Objects & its Methods
String variables are actually objects. The String class is unique because String objects can be created by simply declaring a String variable. The reference variable knows the address of the String object and the object is assigned a value of whatever characters make up the String literal.

2 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference variable String title; No object is created with this declaration An object reference variable holds the address of an object The object itself must be created separately A variable can hold either a primitive value or a reference to an object Like variables that hold primitive types, a variable that holds an object reference must be declared. A class is used to define an object (remember that a class is like the blueprint for the object) and the class name can be thought of as the type of an object. The declaration of object references are structured like the declaration of primitive variables.

3 Creating an object is called instantiation
Generally, we use the new operator to create an object title = new String ("Java Software Solutions"); This calls the String constructor, which is a special method that sets up the object Creating an object is called instantiation An object is an instance of a particular class

4 title = "Java Software Solutions";
The String Class Because strings are so common, we don't have to use the new operator to create a String object title = "Java Software Solutions"; This is special syntax that works only for strings Once an object has been instantiated, we can use the dot operator to invoke its methods title.length() Strings in Java are objects represented by the String class. Once a String object is created, its value cannot be lengthened or shorted, nor can any of its characters change. Thus we say that a String object is immutable. We can, however, create new String objects that have the new version of the original string’s value.

5 String Methods The String class has several methods that are useful for manipulating strings Many of the methods return a value, such as an integer or a new String object Methods are the actions of our objects and the string class has several methods that are useful for manipulating strings. A lot of the methods return something such as a value or even a new string object.

6 Make a String object & Assign a value
String variables are actually objects. String myName = new String (“Mrs. Rush”); // myName is Mrs. Rush As state previously, string variables are actually objects. Here is an example of a new string instantiation. The type of object is “String”. The name of the object is “myName”. It is new string that contains the string Mrs. Rush

7 Create a null string String theirName; //theirName is null
Create a String reference but not a String object Assigned value is null (there isn’t an address of a String object for it to hold) A null string is a string that has: No value No length No address A null string is a string that has no value and therefore no length. It does not hold an address of a String object. You cannot perform a method on a null string but you can perform a method on an empty string. Any attempt to execute a method on a null string will result in a run-time error called a NullPointerExpection.

8 Create an empty string String otherName = new String(“”); //otherName is an empty string Uses an empty constructor from the String class A value exists It is not null There are no characters in the String literal Has a value of “” It has a length of 0 characters An empty string is different from a null string. An empty string is a string that has a value of “” and its length is zero. There is no space inside of the double quotation marks because the string is empty. It may seems like an null string and an empty string are the same thing, but they are not. The difference is subtle. The empty string has a value and the null string does not. Furthermore, you can perform a method on an empty string, but you cannot perform a method on a null string. Any attempt to execute a method on a null string will result in a run-time error called a NullPointerException.

9 String Object String myName = “Mrs. Rush”;
The numbers represent the index of each character in the String object. M r s . R u h 1 2 3 4 5 6 7 8 This is a visual representation of what a string looks like in memory. The numbers below the characters are called the indices (plural of index) of the string. Each index represents the location of where each of the characters lives with the string. Notice that the first index is zero. An example of how we read this is: the character at index 5 of myName is the character “R”. Also, spaces count as characters. You can see that the character at index 4 is the space character.


Download ppt "String Objects & its Methods"

Similar presentations


Ads by Google