Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string length will be at least 2. middleTwo("string") → "ri" middleTwo("code") → "od" middleTwo("Practice") → "ct“ public String middleTwo(String str) { } string ri middle 6/2 = 3 Index 3 is i So we need 1 before middle and the middle Now write the substring method to get the middle two
Java is a pass by value ONLY When x is passed to the change() method, a copy of value of x (a reference) is passed. The method change() creates another object "cd" and it has a different reference. It is the variable x that changes its reference(to "cd"), not the reference itself. public class StringReference { public static void change(String x) x = "cd"; System.out.println(x); } public static void main(String[] args) String x = new String("ab"); change(x);
String is a Class: When an object is created it creates an Object Reference When an object is created a reference to that object is stored in memory. X would not store the actual value just a reference to it. public class Reference { public static void main(String[]args) Reference r = new Reference(); System.out.println(r); } Reference@83e35b memory location for object
References and Strings You can create Strings two different ways. Using the new keyword to create a String creates a different reference that creating a String without the new keyword. String s = new String(“mom”); String s2 = new String(“mom:”); String s3 = s2; String s4 = “mom”; String s5 = “mom:;
Storing String objects Java has 2 types of memory for Strings. 1st is the POOL Objects without the keyword new are stored in a Pool. Objects created this way that contain the same “String” information and also share the same reference. This saves space in memory. There is one reference and they all share it. String s = “Sun”; String s2 = “Sun”; s == s2 True (same reference ) s.equals(s2) True (same String “Sun”) Sun
Java has 2 types of memory for Strings. HEAP Objects created with the keyword new are stored in a Heap. Objects created this way that contain the same “String” information also share the same reference. However, they do not share the same reference. Each object created this way has its own reference. This saves space in memory. There is one reference and they all share it. String s = new String(“Sun”); String s2 = new String(“Sun”); s == s2 false (not same reference ) s.equals(s2) True (same String “Sun”) s reference Sun s2 reference
POOL HEAP String s1 = "Sun"; String s3 = “Sun”; When created this way, Java looks in the pool to see if there is already a String with the same name. If so points to it and has the same reference as other objects with that content. When created WITH THE WORD NEW objects go on the heap. Each object is brand new and has its own reference. It not shared. String s1 = "Sun"; String s3 = “Sun”; String s4 = new String(“Sun“); String s5 = new String (“Sun”); There is only one reference created. All objects share the same reference to the object “Sun”. These are created as new objects and do not have the same reference. Every object created is unique and has its own reference. s1 It is safe for immutable objects to copy references rather than the contents. Cloning (creating copies of objects) is not required. This may save time when Strings are stored in lists, etc. String reference s4 String reference “Sun" "Sun" s5 String reference s3 POOL HEAP
Alias You can assign a String to another String; however and it will contain the same reference. String s = new String(“Sun”); s2 = s; Now there is only one object. s and s2 are aliases of each other. They now share the same reference.
String s1 = "Hello"; // String literal String s3 = s1; // same reference String s4 = new String("Hello"); // String object String s5 = new String("Hello"); // String object
Why does this matter? String comparison: There are three ways to compare String objects: By equals() method (inherited from Object) By == operator By compareTo() method
By equals() method: Equals() method compares the original content of the String. Does it have the same content. public boolean equals(Object another) public boolean equalsIgnoreCase(String another)
Equality using equals(Object o) String n = "Computer"; String s = "Computer"; String t = new String("Computer"); String u = new String("Computer"); String v = new String (“computer”); boolean b = n.equals(s); true boolean b = n.equals(t); true boolean b = n.equals(u); true boolean b = n.equalsIgnoreCase(v); true
By == operator String n = "Computer"; String s = "Computer"; String t = new String("Computer"); String u = new String("Computer"); n t String reference “Computer" String reference u s String reference HEAP “Computer" POOL
== equality String n = "Computer"; String s = "Computer"; String t = new String("Computer"); String u = new String("Computer"); String v = u; n == s n == t t == u v==u
compareTo method parameters int compareTo(Object o) // Object o is the object to be compared or int compareTo(String anotherString) // String to be compared What is returned if the two strings are equal to each other returns 0 . if argument is > than String return value < 0 (negative number) if the argument is < than the string return > 0 (positive number) .
Lexicographic order: Lexicographic order is a generalization of alphabetical order. In this ordering, numbers come before letters and capital letters come before lower case letters. Lexicographic comparison is like alphabetizing the Strings. numbers uppercase lower case http://ss64.com/ascii.html
Ascii table Notice the Ascii table assigns a decimal value for each character, symbol, uppercase and lowercase letter. This is used in the compareTo method.
Lexicographical Upper case characters are regarded as less than lower case characters. "APPLE".compareTo("apple") returns a negative integer. -32 Argument is greater than String A ascii code of 65 < a ascii code of 97 “apple”.compareTo(“APPLE”) returns positive 32 argument less than String a ascii code of 97 > A ascii code of 65 “apple”.compareTo(“apple”) returns 0 equal to each other RULES FOR compareTo if the two strings are equal to each other returns 0 . if argument is > than String return value < 0 (negative number) if the argument is < than the string return > 0 (positive number
-32 32 5 “ABC to “abc argument is > so negative -32 Return Value : if the two strings are equal to each other returns 0 . if argument is greater than String return value < 0 (negative number) if the argument is less than the string return > 0 (positive number) String str1 = "Abc"; String str2 = "abc"; String str3 = "year"; String str4 = "table"; String str5 = "abc"; System.out.println(str1.compareTo(str2)); System.out.println(str2.compareTo(str1)); System.out.println(str3.compareTo(str4)); System.out.println(str5.compareTo(str2)); “ABC to “abc argument is > so negative -32 “abc to “Abc” argument is < so positive 32 “year” to “”table” argument is < so positive 5 “abc” to “abc” equal returns 0 -32 32 5
public class Compare { public static void main(String[]args) String a = "APPLE"; String a2 = "apple"; String a3 = "Apple"; String a4 = "banana"; String a5 = "APPLE"; String word1 = "a"; String word2 = "2"; System.out.println("apple and APPLE =" + a2.compareTo(a)); System.out.println(" APPLE and apple =" + a.compareTo(a2)); System.out.println("APPLE and Apple =" + a.compareTo(a3)); System.out.println("APPLE and APPLE = " + a.compareTo(a5)); System.out.println("a and 2 =" + word1.compareTo(word2)); System.out.println("banana and APPLE =" + a4.compareTo(a)); System.out.println("APPLE and banana =" + a.compareTo(a4)); } Will each print a positive number negative number or 0