Unformatted text preview:

Introduction to Computers and Programming Strings Professor: Evan Korth New York UniversityRoad mapreviewCharacters and StringsString variablesString concatenationString's charAt() methodString's length() methodPassing Strings to methodsReturning Strings from methodsIntroduction to Computers and ProgrammingStringsProfessor: Evan KorthNew York UniversityRoad map•Strings–String variables–Concatenation–length()–charAt()–Strings and methodsreview•What does scope refer to in Java?•What is method overloading?•What does it mean when we say "pass by value"?Characters and Strings•We have studied the char data type with can hold one character.•The String is another data type (it is actually a Class) we can use in Java.–A String is a series of chars.–We can treat the series as one unit–There are methods in the Java API we can use on strings–We can pass Strings to methods and return Strings from methods.String variables•String variables are unlike other variables we have used in this class.•String variables are called reference variables because they hold a reference to where the string is stored in memory.–The rest of the variables we discussed in this class are called primitive variables•Java hides the details from us.•Example:String s = "A string of characters";String concatenation•In Java, we can add two Strings together using the + operator. That process is called concatenation.•The following statements:String s1 = "This looks ";String s2 = "familiar";String s3 = s1 + s2;•assigns the String "This looks familiar" to the variable s3.String's charAt() method•String also has a method called charAt() which can be used to return any single character in a String.–Note: The first letter in a string is considered to be in position zero.•For example, if we have:–String s = "Hello";–s.charAt (0) will return 'H'–s.charAt (4) will return 'o'–s.charAt (5) will return StringIndexOutOfBoundsExceptionString's length() method•All Strings know their own length.•To find out the length of a string, we use String's length() method.•For example:String s = "Hello";int length = s.length();•Will place the value 5 in variable length.Passing Strings to methods•We can also pass a string to a method by using String types in a parameter list.•For example, the following method header would represent a method which accepts a String as a parameter and does not return a value: public static void printVertical (String s)Returning Strings from methods•It is possible to write a method that creates and returns a String.•For example, the following method header would represent a method which accepts no parameters and returns a String: –public static String getMonth(int


View Full Document

NYU CSCI-UA 0002 - Strings

Download Strings
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Strings and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Strings 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?