What is a String? String s = "compsci"; s c o m p s i

Slides:



Advertisements
Similar presentations
Chapter 9 Moving Beyond Robots to Objects. 9.1 Objects Objects are electronic things Objects can do stuff –can remember stuff –can talk to other objects.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Introduction to Programming Lecture 39. Copy Constructor.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Strings In Java, strings are contained in an object that is an instance of the String class. String in java is the name of a class. That’s why it starts.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
String Manipulation. Java String class  The String class represents character strings “Tammy Bailey”  All strings (arrays of characters) in Java programs.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
ROUND 1 Name a method associated with class String 1.) 15 compareTo() 26 indexOf() 34 length() 2.) 3.) 4.) 3 toUpper() 7 substring() 11 charAt() 5.)
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Chapter 7: Characters, Strings, and the StringBuilder.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Programming in Java (COP 2250) Lecture 9 Chengyong Yang Fall, 2005.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Strings and ArrayLists April 23, 2012 ASFA AP CS.
1 Using Objects Chapter 3 Spring 2006 CS 101 Aaron Bloomfield.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
SEEM Java – Basic Introduction, Classes and Objects.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
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.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
The Methods and What You Need to Know for the AP Exam
Strings A String is a sequence of letters
Java String Methods - Codehs
Computer Programming ||
Java: Base Types All information has a type or class designation
String class.
Announcements 2nd homework is due this week Wednesday (October 18)
A+ Computer Science AP Review 2018 AP CS A EXAM
Lecture 4 Using Classes Richard Gesick.
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Multiple Choice -answer the easiest question 1st
Summary of what we learned so far
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Announcements 3rd homework is due this week Wednesday (March 15)
CEV208 Computer Programming
16 Strings.
Lecture 10 Strings CSE /26/2018.
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Chapter 3 Spring 2006 CS 101 Aaron Bloomfield
CS2011 Introduction to Programming I Strings
Announcements HW1 is due TODAY.
More on iterations using
String Objects & its Methods
Unit-2 Objects and Classes
Presentation transcript:

What is a String? String s = "compsci"; 0 1 2 3 4 5 6 s c o m p s i 0 1 2 3 4 5 6 s c o m p s i A String is a group of characters. Strings are used to store words, which can consist of letters, numbers, and symbols. A string is a group of characters. The first character in the group is at spot 0.

String champ = new String("uilstate"); String Constructors String s = "compsci"; String champ = new String("uilstate"); s is a String reference. s is storing the location / memory address of the String Object "compsci"; champ is a String reference. champ is storing the location / memory address of the String Object "uilstate"; reference variable object instantiation

What is a String? String s = "compsci"; "compsci" 0xF5 0xF5 "compsci" s is a String reference. s is storing the location / memory address of the String Object "compsci"; A reference variable stores the memory address of an object.

What is a String? String s = new String("compsci"); "compsci" 0xF5 0xF5 "compsci" s is a String reference. s is storing the location / memory address of the String Object "compsci"; A reference variable stores the memory address of an object.

Strings Return Methods

Methods provide / grant access to an object’s data / properties. length( ) instance variables / data / properties substring( ) String indexOf( ) toString( )

frequently used methods String frequently used methods Name Use substring(x,y) returns a section of the string from x to y not including y substring(x) returns a section of the string from x to length-1 length() returns the # of chars charAt(x) returns the char at spot x indexOf(c) returns the loc of char c in the string, searching from spot 0 to spot length-1 lastIndexOf(c) returns the loc of char c in the string, searching from spot length-1 to spot 0 String is an immutable Object. String cannot be changed. All of the String methods are accessor method. All of the String methods are return methods.

length() String s = "compsci"; int len = s.length(); OUTPUT7 System.out.println( len ); OUTPUT7 The String length() method returns the character count. length() looks at the String Object and returns back the number of characters contained. compsci contains 7 characters so a call to length() would return 7. 0 1 2 3 4 5 6 s c o m p s i

Return Methods Return methods perform some action and return a result back. .length() is a return method. String s = "compsci"; int len = s.length(); System.out.println( len ); Return methods typically perform some action then send back a value. Return methods are also used as get methods to retrieve a value from an Object. length() returns an integer back to the calling location. The value returned is then assigned to variable len.

charAt() String s = "compsci"; OUTPUT c m i out.print(s.charAt(0) + " "); out.print(s.charAt(2) + " "); out.println(s.charAt(6)); OUTPUT c m i The String charAt() method returns the character at the specific spot. charAt(0) would return the character at spot 0. charAt(2) would return the character at spot 2. 0 1 2 3 4 5 6 s c o m p s i