// Exercise 2 // // every class in Java is also a type: // you can define and use a variable whose type is a certain class // // a very famous example is the class String, consider this: // // String s = "chris"; // // a Java programer would say "s is a variable that holds a reference // to an object of class String" or more simply "s is a String object" // // you can use a lot of predefined classes from the standard library // (such as String) or you can define your own // // type, compile and run this programm too and observe what you can // do with objects! public class DemoStrings { public static void main(String[] a) { // define a few String objects // and perform the + operation (concatenation) on them String str1 = "I'm a"; String str2 = "student"; String phrase = str1 + str2; String nice_phrase = str1 + " " + str2; System.out.println(phrase); System.out.println(nice_phrase); // contrary to built-in types, classes have methods // that you can call by using a dot . and round brackets () System.out.println(nice_phrase.toUpperCase()); // method toUpperCase System.out.println(nice_phrase.substring(0, 3)); // method substring // can you figure out what these two methods do? // String is a class // note that the name String starts with an upper case letter, // as opposed to built-ins such as int and double } }