the string data type string. string (in general) a string is a sequence of characters enclosed...

53
The string data type String

Upload: frank-jefferson

Post on 12-Jan-2016

251 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The string data type String

Page 2: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

String (in general)

• A string is a sequence of characters enclosed between the double quotes "..."

Example:

• Each character in a string is of the type char and uses the Unicode as encoding method

"abc123"

"Hello World"

"Hello, what is your name ?"

Page 3: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The String data type in Java

• The Java language does not provide the String data type

The String data type is a class that is constructed using the char data type

• The Java language does provide the char data type

• The documentation of the class String can be found here: http://download.oracle.com/javase/6/docs/api/java/lang/String.html

Page 4: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The String data type in Java (cont.)

Note:

• We do not have enough knowledge on Java to explain how the String data type is constructed (It uses advanced program language knowledge)

• Right now, we will only learn how to use the String data type

Page 5: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

String literals

• We write string literals between double quotes "..." (as discussed above)

Example:

"Hello World"

Page 6: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

String literals (cont.)

• Escape character:

• Escape character = a special character that allow Java to define the meaning of the following character

• \ (backslash) = the escape character for strings

Page 7: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

String literals (cont.)

• Escape sequence:

• Escape sequence = the escape character \ followed by one character

Example:

• A escape sequence denotes one character

The character denoted by an escape sequence is usually one that you cannot type in with the keyboard

\n

Page 8: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

String literals (cont.)

• Commonly used escape sequences:

  Escape sequence     Denoted character   Example  

\t   Tab   "\t" (string with a TAB character)

\n   New line (NL)   "\n" (string with a NL character)

\\ Backslash (\)   "\\" (string with a \)

\" Double quote (")   "\"" (string with a ")

Page 9: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

String literals (cont.)

• Example:

public class Escape01 { public static void main(String[] args) { System.out.println("He said \t \"Hello\" \nI said \" \\ \" \n"); } }

Page 10: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

String literals (cont.)

• Output:

He said "Hello" I said " \ "

Page 11: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Defining String typed variables

• Syntax to define a String typed variable:

Notes:

String NameOfVariable ;

• The class String announces the variable definition clause

• The NameOfVariable is an identifier which is the name of the variable.

• The variable definition clause is must be ended with a semi-colon ";"

• A String typed variable can store the location (address) of a sequence characters (string)

Page 12: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Defining String typed variables (cont.)

• Example: defining and assigning to a String typed variable

public class String01 { public static void main(String[] args) { String a; a = "Hello World"; // a = location of "Hello World" System.out.println(a); // Print the string at a } }

Page 13: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Defining String typed variables (cont.)

• Example Program: (Demo above code) – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/05/Progs/String01.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac String01.java

• To run:          java String01

Page 14: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

How are strings stored inside the computer

• Challenge to store strings:

• Strings have variable lengths !!!

Example:

• Conclusion:

"Hello" contains 5 characters

"Good-bye" contains 8 characters

• You cannot store a string in a fixed sized variable !!!

Page 15: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

How are strings stored inside the computer (cont.)

• There are 2 techniques in Computer Science for storing strings:

• The location + length method

• The location + sentinel method

Page 16: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The location + length method to store strings

• The location + length method:

• The characters of string is stored (using Unicode encoding) somewhere consecutively inside the RAM memory

• A string variable contains:

• the location of the first character of the string

• the length of the string (total number of characters in the string)

Page 17: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The location + length method to store strings (cont.)

• Example: storing strings "abc" and "12" using location + length

Page 18: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The location + length method to store strings (cont.)

• Explanation:

• The string "abc" is stored beginning at memory location 984 in RAM memory

• The length of this string is 3 characters

• The string variable that helps us find the string "abc" contains:

• The address 984

• The length 3

Page 19: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The location + length method to store strings (cont.)

• Another string variable that helps us find the string "12" contains:

• The address 520 which is the first location of the string "12"

• The length 2

Page 20: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The location + sentinel method to store strings (cont.)

• Sentinel

• Sentinel = a special character (symbol) that denotes the end of a string

• The sentinel used in programming languages to denote the end of a string is usually the NULL character with Unicode value 0.

Page 21: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The location + sentinel method to store strings (cont.)

• The location + sentinel method:

• The characters of string is stored (using Unicode encoding) somewhere consecutively inside the RAM memory

• The sentinel (NULL character) is added after the last character of the string

• A string variable contains:

• the location of the first character of the string

(The end of the string is marked by the sentinel)

Page 22: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The location + sentinel method to store strings (cont.)

• Example: storing strings "abc" and "12" using location + sentinel

Page 23: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The location + sentinel method to store strings (cont.)

• Explanation:

• The string "abc" is stored beginning at memory location 984 in RAM memory

• The sentinel NULL (with Unicode value 0) is added after the string

• The string variable that helps us find the string "abc" contains:

The address 984

(We trace down the memory until we find the sentinel to find the end of the string)

Page 24: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The location + sentinel method to store strings (cont.)

• Another string variable that helps us find the string "12" contains:

The address 520 which is the first location of the string "12"

(We trace down the memory until we find the sentinel to find the end of this string)

Page 25: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Strings in Java

• String representation in Java:

• Obtaining the length information of strings in Java:

• Java uses the location + length method to store String typed data

• Let a be a String typed variable

• The expression a.length() returns the length of the string stored in the string variable a

Page 26: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Strings in Java (cont.)

• Example:

public class String02 { public static void main(String[] args) { String a; a = "abc"; // a = location + length of "abc" System.out.println(a); // Print the string at a System.out.println(a.length()); // Print the length of the string at a } }

Page 27: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Strings in Java (cont.)

• Example Program: (Demo above code) – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/05/Progs/String02.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac String02.java

• To run:          java String02

Page 28: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Representing numbers and numeric strings

• Numeric string:

• Numeric string = a string containing only numbers in Unicode that represent digits

Page 29: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Representing numbers and numeric strings (cont.)

• Important fact:

• Integer numbers and numeric strings look alike in a computer program:

• Inside the computer, integer numbers and numeric strings are complete different (because they use different representation (encoding) techniques)

12 (integer number)

"12" (numeric string)

Page 30: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Representing numbers and numeric strings (cont.)

• Example:

• The numeric string "12" consists of 2 numbers:

• 49 (binary 00110001) which encodes the character '1' using the Unicode scheme

• 50 (binary 00110010) which encodes the character '2' using the Unicode scheme

Page 31: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Representing numbers and numeric strings (cont.)

• The numeric string "12" is stored in the RAM memory as follows:

Page 32: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Representing numbers and numeric strings (cont.)

• The integer number 12 consists of 1 number:

• 12 (binary 00001100) which encodes the value 12 using the Binary number system

Page 33: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Representing numbers and numeric strings (cont.)

• The integer number 12 is stored in the RAM memory as follows:

Page 34: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Representing numbers and numeric strings (cont.)

• Quiz for class:

• Do you still remember how the computer decide what encoding method it must use to interpret a number ?

Page 35: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Representing numbers and numeric strings (cont.)

• Answer:

• Based on the data type information

The data type is the necessary context information that the computer need to decide which encoding method it will use.

Page 36: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Operators on the String data type

• There is only one operator (the + operator) defined for the String data type:

Example:

• string1 + string2 = concatenate the strings string1 and string2

"abc" + "12" returns the string: "abc12"

Page 37: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Operators on the String data type (cont.)

• Example Java program with 4 string concatenation operations:

public class String03 { public static void main(String[] args) { String a, b, c; a = "abc"; b = "12"; c = a + b; // String concatenation System.out.println("a = " + a); // String concatenation !! System.out.println("b = " + b); // String concatenation !! System.out.println("c = " + c); // String concatenation !! } }

Page 38: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Operators on the String data type (cont.)

• Example Program: (Demo above code) – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/05/Progs/String03.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac String03.java

• To run:          java String03

Page 39: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Converting integer and floating point values to strings

• Recall that the integer 12 is stored using Binary number system as the following binary number:

00001100 | | | +--------------> 22 = 4 +------------> 23 = 8 ----- 12

Page 40: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Converting integer and floating point values to strings (cont.)

• On the other hand, the string "12" is stored using the Unicode scheme as 2 binary numbers:

00110001 (code for '1')

00110010 (code for '2')

Page 41: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Converting integer and floating point values to strings (cont.)

• Converting an integer representation to the string representation:

• Converting numbers into strings is an advanced programming problem

• The algorithm to convert an integer representation to the string representation will be discussed in CS255

• Here, I will only show you how to do this in Java:

Page 42: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Converting integer and floating point values to strings (cont.)

• Converting an int to the string representation:

String a; int x;

a = Integer.toString(x) ; // Returns the String representation

// for the integer value in variable x

Page 43: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Converting integer and floating point values to strings (cont.)

• Converting a double to the string representation:

String a; double x;

a = Double.toString(x) ; // Returns the String representation

// for the double value in variable x

Page 44: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Converting integer and floating point values to strings (cont.)

• Converting a float to the string representation:

String a; float x;

a = Float.toString(x) ; // Returns the String representation

// for the float value in variable x

Page 45: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Converting integer and floating point values to strings (cont.)

• Example:

public class String04 { public static void main(String[] args) { String a; int x = 12; double y = 3.14159265358979; a = Integer.toString(x); // Convert int --> String System.out.println(a); a = Double.toString(y); // Convert double --> String System.out.println(a); } }

Page 46: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The + operator on number typed and string typed data

• Java provides an automatic conversion feature when the + operator is used between:

• a number typed data and a string typed data

Page 47: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The + operator on number typed and string typed data (cont.)

• Java's automatic conversion rule for mixed number and string expression:

• When the + operator is used between

Then:

• a number and

• a string

• the number is automatically converted to a string

• The + operator is then applied on 2 strings (concatenation)

Page 48: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The + operator on number typed and string typed data (cont.)

• Example:

public class String05 { public static void main(String[] args) { String a, b; int x; a = "abc"; x = -12; b = a + x; System.out.println(b); // Prints "abc-12" b = x + a; System.out.println(b); // Prints "-12abc" } }

Page 49: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The + operator on number typed and string typed data (cont.)

• Explanation:

a = "abc"; x = -12;

b = a + x; is executed as follows:

a + x = "abc" + (-12) convert number to string = "abc" + "-12" concatenate 2 strings = "abc-12"

b = x + a; is executed as follows:

x + a = (-12) + "abc" convert number to string = "-12" + "abc" concatenate 2 strings = "-12abc"

Page 50: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

The + operator on number typed and string typed data (cont.)

• Example Program: (Demo above code) – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/05/Progs/String05.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac String05.java

• To run:          java String05

Page 51: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Exercise

• What is printed by the following Java program:

public class String06 { public static void main(String[] args) { System.out.println( "a" + 1 + 2 ); System.out.println( 1 + 2 + "a" ); } }

Page 52: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Exercise (cont.)

• Solution:

"a" + 1 + 2 = "a" + 1 + 2 (+ is left associative !) = "a" + "1" + 2 (convert 1 to "1") = "a1" + 2 = "a1" + "2" (convert 2 to "2") = "a12"

1 + 2 + "a" = 1 + 2 + "a" (+ is left associative !) = 3 + "a" (This + is an integer addition !) = "3" + "a" = "3a"

Page 53: The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character

Exercise (cont.)

• Example Program: (Demo above code) – Prog file:

http://mathcs.emory.edu/~cheung/Courses170/Syllabus/05/Progs/String06.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac String06.java

• To run:          java String06