String Objects & its Methods

Slides:



Advertisements
Similar presentations
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Advertisements

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
10-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
28-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
29-Jun-15 Using Objects. 2 Classes and objects The type of an object is the class that describes that object If we say int count, the type of count is.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
CS0007: Introduction to Computer Programming Introduction to Arrays.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.
Chapter 7: Characters, Strings, and the StringBuilder.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
CSE 1201 Object Oriented Programming Using Classes and Objects.
SEEM Java – Basic Introduction, Classes and Objects.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Class Fundamentals BCIS 3680 Enterprise Programming.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
1 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.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Sections 10.1 – 10.4 Introduction to Arrays
Variables in Java A variable holds either
Java Primer 1: Types, Classes and Operators
Foundations of Programming: Arrays
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Creating Objects & String Class
Java Review: Reference Types
Introduction to Objects
Objects First with Java CITS1001
Lecture 4 Using Classes Richard Gesick.
More Object Oriented Programming
String Objects & its Methods
Object Oriented Programming (OOP) LAB # 8
Introduction to Classes
Using Objects 21-Nov-18.
Variables ICS2O.
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Java Classes Aliases & Null & This
Packages & Random and Math Classes
Outline Creating Objects The String Class The Random and Math Classes
Arrays 2-May-19.
Introduction to Objects
Presentation transcript:

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.

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.

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

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.

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.

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

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.

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.

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.