70 likes | 213 Views
0. str. str. Java Strings. String Constructors. Empty String. String str = new String( );. With message. String str = new String (“Java Strings’);. Object. Reference. Note! String str = “Java Strings”; produces the same result. I like dogs. str1. I prefer cats. str2.
E N D
0 str str Java Strings String Constructors Empty String String str = new String( ); With message String str = new String(“Java Strings’); Object Reference Note! String str = “Java Strings”; produces the same result
I like dogs. str1 I prefer cats. str2 String Objects String objects are immutable -- they cannot be changed once they have been created. References to string objects may be changed. String str1 = new String (“I like dogs.”); String str2 = new String(“I prefer cats.”); str1 = str2; //reassign reference Automatic garbage collection will reclaim unreferenced objects
str1 This is the winter str2 of our discontent This is the winter of our discontent Note! The same result occurs for str1 = str1 + str2; Concatenation Declare and construct two strings, then concatenate str2 to the end of str1 String str1 = new String(“This is the winter “); String str2 = new String(“of our discontent”); str1 = str1.concat(str2); //message to str1 to concat str2 to its end Automatic garbage collection will reclaim unrferenced objects
str1 A string str2 Same Object / Same Reference The String class has two methods boolean equals(String anotherString) and boolean equalsIgnoreCase(String anotherString) To test whether two string objects are the same (two strings have the same value) The relational operator == is used to test whether two Strings reference the same object.
false true true false true Same Object / Same Reference String s1 = new String (“I am a string”); String s2 = “ a string”; String s3 = s1.substring(0,4); String s4 = “ a string”; String s5 = s3 + s2; if (s5 == s1) { } if (s1.equals(s5)) { } if (s2 == s4) { } if (s5 == s3 + s4) { } if (s5.equals(s3+s4)) { } The result of op. is stored in temporary string A new object is created when 2 strings are concatenated If the object already exists, it will be used with a duplicate ref. by new string
Using class Scanner for user input Step 1. class Scanner is found in the package java.util -- it must be imported into the program – you may import just the class or the entire package import java.util.Scanner; // or import java.util.* Step 2. Declare an object of class Scanner Scanner console; Step 3. Allocate memory for this object (create the object) and pass as a parameter the input steam to be scanned. (input from the keyboard here) Note! Steps 2 and 3 can be combined in a single statement console = new Scanner(System.in);
Methods in class Scanner public static Scanner console = new Scanner(System.in); //declared in the class containing the main method (but outside the method) console.next( ) -- retrieves the next input as a string – all the input preceding the next white space (blank, tab, end of line) console.nextLine( ) -- retrieves the next input as a string – all the input preceding the next end of line character. console.nextInt( ) – if the next input can be interpreted as an integer, it is read in as an integer. console.nextDouble( ) – if the next input can be interpreted as a floating point number, it is read in as a double.