class StringDemo { public static void main(String args[]) { // Four versions that do the same thing. String myString1 = new String("Hello there"); // Declare and create String myString2 = "Hello there"; // Declare and create String myString3; // Declare myString3 = "Hello there"; // Create String myString4; // Declare myString4 = new String("Hello there"); // Create System.out.println("First string is - " + myString1); System.out.println("Second string is - " + myString2); System.out.println("Third string is - " + myString3); System.out.println("Fourth string is - " + myString4); String david = "David Langan"; // Declare and create. String davidLangan = null; // Declare reference and set to null. // Just had the declaration for davidLangan but have not set it to // refer to a specific object yet. We can test to see if it is "null"; if (davidLangan == null) System.out.println("Yes... the object reference is not set yet."); else System.out.println("What?? I got set somehow?"); davidLangan = david; // Have reference refer String dL = new String(david); // Create new object System.out.println("First version is - " + david); System.out.println("Second version is - " + davidLangan); System.out.println("Third version is - " + dL); if (david == davidLangan) System.out.println("david & davidLangan do refer to same object"); else System.out.println("david & davidLangan do NOT refer to same object"); if (david == dL) System.out.println("david & dL do refer to same object"); else System.out.println("david & dL do NOT refer to same object"); dL = "David D. Langan"; // Create new String and have dL refer to it. // Old "David Langan" object is garbage and // its space will automatically be reclaimed. System.out.println("New dl holds : " + dL); System.out.println("Length of 'Hello there' is : " + "Hello there".length()); System.out.println(" Me trimmed is:" + " Me ".trim()); System.out.println("This is a test".substring(2)); System.out.println("This is a test".substring(2,6)); System.out.println("Hello there".charAt(0)); System.out.println("Hello there".charAt(2)); System.out.println("Hello there".charAt(10)); // Try uncommenting the next line to see the error // that results if you try to execute it. The Java interpreter (java) // catches this error and gives you a runtime error message. // One might have thought that with the 11 sitting right there in the // code itself that the compiler might have caught this error before // we even got to runtime, but that was not the case. // System.out.println("Hello there".charAt(11)); System.out.println("Hello there".replace('e','i')); System.out.println("This is 4 a TEST".toLowerCase()); System.out.println("This is 4 a TEST".toUpperCase()); String hello1 = new String("Hello"); String hello2 = new String("Hello"); if (hello1 == hello2) System.out.println("String references are equal."); else System.out.println("String references are NOT equal."); // Prints this! if ( hello1.equals(hello2) ) System.out.println("String contents are equal."); // Prints this! else System.out.println("String contents are NOT equal."); if ( "Hello There".equalsIgnoreCase("heLLo THEre") ) System.out.println("Yes two versions of hello there were the same."); else System.out.println("No, two versions of hello there were NOT " + " the same."); if ("This is a test".compareTo("This is b test") < 0) System.out.println("The 'a' version is less than the 'b' version"); else System.out.println("The 'a' version is NOT less than the 'b' version"); System.out.println("Hello there".indexOf('H')); System.out.println("Hello there".indexOf('e')); System.out.println("Hello there".indexOf('x')); System.out.println("Hello there".indexOf('H',5)); System.out.println("Hello there".indexOf('t')); System.out.println("Hello there".indexOf("ll")); System.out.println("Hello there".indexOf("ll",5)); System.out.println("Hello there".indexOf("he")); if ( "Hello there".startsWith("He") ) System.out.println("'Hello there' does start with 'He'"); else System.out.println("'Hello there' does not start with 'He'"); if ( "Hello there".startsWith("Hx") ) System.out.println("'Hello there' does start with 'Hx'"); else System.out.println("'Hello there' does not start with 'Hx'"); if ( "Hello there".startsWith("ll",2) ) System.out.println("'Hello there' does have an 'll' starting at 2"); else System.out.println("'Hello there' doesn't have an 'll' starting at 2"); System.out.println(String.valueOf(25)); System.out.println(String.valueOf('a')); System.out.println(String.valueOf(true)); System.out.println(String.valueOf(3.14)); } // End of function main() }