This document introduces the concepts of classes and objects in Java. It defines a class as a template that introduces a new type into the system and defines an object as an instance of a class. The key points covered include:
- Objects are created from classes using the new operator.
- Objects contain fields to store data and methods to manipulate the data.
- When passing primitive data types to methods, the value is passed but when passing objects, the reference is passed.
- Methods determine the messages an object can receive including name, arguments, return type and body.
2. Class Object Object creation Fields Default values for data members Methods Passing data to methods Agenda
3. Introduces a new type into the system Defines a template for an object Ex – Circle, Rectangle class ATypeName { /* Class body goes here */ } ATypeName a = new ATypeName(); Class Rectangle { } Rectangle rectangle = new Rectangle(); Class
4. Software representation of a real world entity Car, Train, Soccer Created from class templates using new operator Object
5. Objects are manipulated using references Rectangle rectangle = null; Objects must always be created Rectangle rectangle = new Rectangle(); rectangle.draw(); Object creation
6. This is the data member of the class It can be a primitive or any other object reference class DataOnly { inti; double d; boolean b; Rectangle rectangle; } DataOnly data = new DataOnly(); data.i = 47; data.d = 1.1; data.b = false; data.rectangle = new Rectangle(); Access – reference.field == discouraged!!! Fields
7. When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it Default values for data members
8. The default values are only what Java guarantees when the variable is used as a member of a class. This ensures that member variables of primitive types will always be initialized (something C++ doesn’t do), reducing a source of bugs This guarantee doesn’t apply to local variables—those that are not fields of a class public static void checkthis() { int x; x = x + 1;//will not compile } Default values for data members
9. Function, procedure – a group of statements which manipulate data and optionally return some value. Methods in Java determine the messages an object can receive the name, the arguments, the return type, and the body Method
10. ReturnTypemethodName( /* Argument list */ ) { /* Method body */ } The return type describes the value that comes back from the method after you call it. The argument list gives the types and names for the information that you want to pass into the method. The method name and argument list (which is called the signature of the method) uniquely identify that method objectName.methodName(arg1, arg2, arg3); int x = a.f(); Method
11. public class MethodPlayer { public boolean play(intvol) { System.out.println(“Play volume -” + vol); } public static void main(String a[]) { MethodPlayer player = new MethodPlayer(); player.play(5); } } Method in Action
12. Write a Java program that has one member field integer and one method that tests if a given input integer is odd or even. If the given input is even change the value of the member field with this one Now run a for loop to test the first 100 integers with this method. Add another method to get the value of the member field Exercise
13. A method uses parameters, A called passes arguments You can pass n number of inputs to a method of varying types Java is always pass by value or pass by copy Passing data to methods
14. public booleanisEven(int value) { if(value % 2 ==0) { return true; //increment and print value = value + 1; System.out.println(“Next odd - ” + value); } else{ return false; } } inti = 55; isEven(i); System.out.println(i); Passing primitives
15. Passing primitives 55 int x isEven(i); Copy the value 55 int value 56 Increment the value
16. The references are copied public void peekAndChange(Rectangle r) { print(r.getLength()); print(r.getBreadth); r.setLength(25); r.setBreadth(30); } public void peekCaller() { Rectangle r = new Rectangle(); r.setLength(15); r.setBreadth(20); print(r.getLength()); print(r.getBreadth); peekAndChange(r); print(r.getLength()); print(r.getBreadth); } Passing reference