CISC-124 20180215 These notes are intended to summarize and clarify some of the topics that have been covered recently in class. The posted code samples also have extensive explanations of the material. Please be sure to download and run the code samples, and experiment with them by running them, modifying them, and writing your own similar methods. Please also make use of the textbook as a learning resource. Passing Parameters A Java method cannot change the value of any of the arguments passed to its parameters. public static void main(string[] args){ int y = 3; System.out.println( y = +y); methoda(y); System.out.println( y = +y); void methoda(int x){ System.out.println( x = +x); x = 12; System.out.println( x = +x); In this example the output is y = 3 x = 3 x = 12 y = 3
The point is that when we change the value of x, it doesn t change the value of y Now what if the parameter is a reference (link) to an Object? public static void main(string[] args){ Dog dog1 = new Dog( Astro ); System.out.println( dog1.name = +dog1.getname()); methodb(dog1); System.out.println( dog1.name = +dog1.getname()); void methodb(dog d){ System.out.println( d.name = +d.getname()); d.setname( Scoobydoo ); System.out.println( d.name = +d.getname()); The output is dog1.name = Astro d.name = Astro d.name = Scoobydoo dog1.name = Scoobydoo The methodb method was able to change the name attribute of the Dog - so it looks like the method was able to change the value of the argument it was given. It really didn t though, because the argument given is the link (reference) to that Dog object, and that link didn t get changed the method is not able to make dog1 refer to a different Dog object, just as methoda was not able to change the value of y. However, methodb is able to change the contents of the Dog object that the argument links (refers) to.
There is an ever-lasting argument among Java programmers about the proper terminology to describe how parameters are passed. Some say it is passing by reference because the parameter refers to the same Object that the argument refers to. Others say it is passing by value because the the parameter is always given the value of the argument and that value is either a primitive type or it is a link (reference) to an object. As you read about and study Java programming, you will encounter both descriptions. It really doesn t matter which terminology you use, so long as you understand exactly how it works. No matter which description you use, you will eventually find someone who disagrees with it! Exceptions We use exceptions to respond to problems that occur during runtime. Exceptions are simply Objects that we can use to hold information that identifies the problem. Java has many predefined Exceptions such as FileNotFoundException(), and we can also define our own (see the most recent project that includes the Dog class). If one of our methods (or a pre-defined method) detects a problem that it doesn t know how to solve such as an invalid parameter it can create and throw an Exception. Creating the Exception is done with the new keyword, just as for any Object. Throwing an exception terminates the method immediately no value is returned. Suppose methoda calls methodb, and methodb throws an exception. The exception is thrown to the point in methoda at which methodb is called. Then methoda must either deal with the exception (this is where we use a try/catch structure) or throw it back to whatever method called methoda this is done by putting throws... in the methoda header. Every exception that your methods can encounter or create must be caught. It may be thrown, and thrown, and thrown again all the way up the chain of methods that called other methods, but somewhere along the line some method has to catch it. The main method is not allowed to throw any exceptions so main is the final point at which exceptions can be caught.
Inheritance When we declare that class ClassB extends ClassA, we are stating that objects of type ClassB inherit all attributes and methods of ClassA. ClassB objects can also have attributes and methods that do not come from ClassA. If an attribute of ClassA is private, it is still inherited by ClassB but ClassB objects cannot see it. However, ClassB objects can access private attributes using public accessor methods If an attribute of ClassA is protected, it is inherited by ClassB and ClassB objects can see it. ClassB can executed the constructor method from ClassA using the super command. ClassB can override methods from ClassA, which is just a fancy word for redefine. We introduced the instanceof keyword that lets us determine if an object is an instance of a particular class. Note that if an object is an instance of ClassB, it is also an instance of ClassA, and of any class above ClassA. Every class in Java is an extension of the basic Object class, and inherits some methods from that class. One of those is equals() - which means that we can try to compare any two objects to see if they are equal... however in practice in any class where we need to test objects for equality we should override the default equals() method with a method that is appropriate for the particular class. Usually we determine equality of two objects of the same type by comparing the values of their attributes.
Javadoc Javadoc is a tool that we can use to produce web-pages that document our Java programs. When we run javadoc.exe on our programs, it produces a web-page for each of our classes each web-page lists the methods and attributes of the class, as well as other useful information. We can provide comments that will be included in the web-page by including them in comment blocks that look like this /** * This method does something useful * @param name String * @param age int * @returns double */ placed just above the header of the method. Eclipse will do a lot of the work of generating these comment blocks for us.