March 29th Odds & Ends CS 239
Re: instance variables Public class DoSomething { int count; // count is an instance variable FACT 1: when a class is instantiated instance variables are initialized FACT 2: variables of primitive types within a method MUST be initialized by the programmer
Look back at ChangeMaker trace The detailed trace in the key only handles the j = 1 case in the initial call. Still needs to handle the j=2 case in the initial call.
Overloading When you overload a method the method names are the same but the parameter lists are different.
Strings Are immutable When you run the code on the next slide, you are not changing the value at the original storage location, you are being given a new storage location for the new String. There is no way to get the address of a String or any other java Object.
String example public class TestString { public static void main (String [] args) String animal = “cat”; System.out.println (animal); animal = animal.concat(“bird”); } // end main } // end class
Addresses in Java Java doesn’t want you to see addresses When we printed out numbers in class, what we were seeing was not an address. It was a hash code.
TestParameterPassing.java public class TestParameterPassing { public static void main (String [] args) { Integer c,d; ParameterPassing myParameterPassing; myParameterPassing = new ParameterPassing(); // c = new Integer(7); // d = new Integer (13); c = 7; d = 13; System.out.println (" c = " + c + " d = " + d); // before call to swap myParameterPassing.swap(c,d); System.out.println (" c = " + c + " d = " + d); // after call to swap } // end main } // end class In the current version of java, using c = 7; and d = 13; is equivalent to the commented out lines above. In either case, the swap method with two Integer parameters is the one that will be called.