Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character."— Presentation transcript:

1 The string data type String

2 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 ?"

3 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/lan g/String.html

4 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

5 String literals We write string literals between double quotes "..." (as discussed above) Example: "Hello World"

6 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

7 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

8 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 ")

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

10 String literals (cont.) Output: He said "Hello" I said " \ "

11 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)

12 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 }

13 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

14 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 !!!

15 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

16 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)

17 The location + length method to store strings (cont.) Example: storing strings "abc" and "12" using location + length

18 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

19 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

20 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.

21 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)

22 The location + sentinel method to store strings (cont.) Example: storing strings "abc" and "12" using location + sentinel

23 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)

24 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)

25 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

26 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 }

27 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

28 Representing numbers and numeric strings Numeric string: Numeric string = a string containing only numbers in Unicode that represent digits

29 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)

30 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

31 Representing numbers and numeric strings (cont.) The numeric string "12" is stored in the RAM memory as follows:

32 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

33 Representing numbers and numeric strings (cont.) The integer number 12 is stored in the RAM memory as follows:

34 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 ?

35 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.

36 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"

37 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 !! }

38 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

39 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 | | | +--------------> 2 2 = 4 +------------> 2 3 = 8 ----- 12

40 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')

41 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:

42 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

43 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

44 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

45 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); }

46 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

47 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)

48 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" }

49 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"

50 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

51 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" ); }

52 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"

53 Exercise (cont.) Example Program: (Demo above code) –Prog file: http://mathcs.emory.edu/~cheung/Courses170/Syllabus/05/Progs/S tring06.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


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

Similar presentations


Ads by Google