LECTURE 2 (Gaya College of Engineering) 1) CHARACTERISTICS OF OBJECTS: Object is an instance of a class. So, it is an active entity. Objects have three basic characteristics. They are- State: An object contains attributes or features. These attributes or features can be represented as the instance variables of an object. State can be defined as the values assigned to the attributes of an object. class Test int a; int b; void func_print() System.out.println( We are in the print function ); public static void main( String[ ] args) Test t = new Test(); t.a = 10; t.b = 20; System.out.println( The fisrt value : + t.a) System.out.println( The second value: + t.b) In the above program the object "t" of the class "Test have variables assigned to the values a = 10 and b = 20. The object ( or instance ) with these two values is called one state of the object. When we change the values of "a" and "b" then that would be another state of the object "t". Dhruba Jyoti Kalita Page 1
Identity: When an object is created in object oriented environment then unique identification number gets associated with the object which is called as Identity of the object. By using this Identity JVM can reference the object that it wants to access. In Garbage Collection Mechanism in java the unused objects in memory can be referred using the identity of the objects. Behavior: The processes by which objects acts to change its state to accomplish certain task is called as behavior of an object. In general the methods invoked by the object in an Object Oriented Environment is called as Behavior of that object. class Bank. float Calculate_Interest( float amount) float interest; float rate = 10; float time_period = 12 ; interest = (amount * rate* time_period) /100 return interest; public static void main(string[] args) Bank b = new Bank(); Dhruba Jyoti Kalita Page 2
float I; I = b. Calculate_Interest(10,00000); System.out.println( The interest is : +I) In the above java program the method Calculate_Interest( float amount) is the behavior of the object. 2) AN EXAMPLE OF A JAVA CODE : class Test public static void main(string[] args) String str = Gaya Engineering College ; System.out.println(str); The above code is a very basic code of java. The following are the points to be known to write a java program- Everything we do in java we do inside a class. ( Since java is a purely object oriented language) The execution of a java program starts from the main method. public It is an access specifier which makes the main method visible to the external sources. static- When static keyword is used in front of a method in java then that method can be accessed without creating object of the class to which it belongs. void void is a primitive data type. When we use this in front of a method then that function is not going to return anything. main main is the name of the function. Strings[] args This is argument list. When we send some arguments from the command line console to the program that is going to be consumed inside this array of arguments of type String. Following is the example which shows the use of this class Test public static void main(string[] args) Dhruba Jyoti Kalita Page 3
System.out.println( The first argument is : + args[0]) System.out.println( The second argument is : + args[1]) It is to be noted that when we send some arguments from the command line console it is going to be consumed in string format. Now if we execute the above program and pass some arguments to the program it will print those arguments. For example- // Assuming that the above program has been saved as test.java javac test.java java test gce gaya For the above execution it is going to pringt- gce gaya In the above code snippet by - String str = Gaya Engineering College ; We are assigning a value Gaya Engineering College to the variable str of type String. This is a static way of assigning a value to a java program. There are many other ways of assigning values in java dynamically during run time which we will learn in the next section. System.out.println(str); System is a class. out is an object. println() is a method Note: In the above out is an object of the class java.io.printstream. This out is an attribute of the class System and println() is a method of the class java.io.printstream. In the following the interpretation has been shown. Dhruba Jyoti Kalita Page 4
------- To be continued Dhruba Jyoti Kalita Page 5
Dhruba Jyoti Kalita Page 6