1
+ A class is a template for an object, and an object is an instance of a class.
2
+ A class creates a new data type that can be used to create objects.
3
+
4
+ When you declare an object of a class, you are creating an instance of that class.
5
+ Thus, a class is a logical construct. An object has physical reality. (That is, an object occupies space in memory.)
6
+
7
+ Objects are characterized by three essential properties: state, identity, and behavior.
8
+ The state of an object is a value from its data type. The identity of an object distinguishes one object from another.
9
+ It is useful to think of an object’s identity as the place where its value is stored in memory.
10
+ The behavior of an object is the effect of data-type operations.
11
+
12
+
13
+ In object-oriented programming, the dot operator is used to link the name of an object with the name of an instance variable or method of that object.
14
+ It is also commonly referred to as the dot notation or dot syntax.
15
+
16
+
17
+
18
+
19
+ The 'new' keyword dynamically allocates(that is, allocates at run time)memory for an object & returns a reference to it.
20
+ This reference is, more or less, the address in memory of the object allocated by new.
21
+ This reference is then stored in the variable.
22
+ Thus, in Java, all class objects must be dynamically allocated.
23
+
24
+
25
+
26
+
27
+ Box mybox; // declare reference to object
28
+ mybox = new Box(); // allocate a Box object
29
+ The first line declares mybox as a reference to an object of type Box. At this point, mybox does not yet refer to an
30
+ actual object. The next line allocates an object and assigns a reference to it to mybox. After the second line executes,
31
+ you can use mybox as if it were a Box object. But in reality, mybox simply holds, in essence, the memory address of the
32
+ actual Box object.
33
+ The key to Java’s safety is that you cannot manipulate references as you can actual pointers.
34
+ Thus, you cannot cause an object reference to point to an arbitrary memory location or manipulate it like an integer.
35
+
36
+ A Closer Look at new:
37
+ classname class-var = new classname ( );
38
+ Here, class-var is a variable of the class type being created. The classname is the name of the class that is being
39
+ instantiated. The class name followed by parentheses specifies the constructor for the class. A constructor defines
40
+ what occurs when an object of a class is created.
41
+
42
+ You might be wondering why you do not need to use new for such things as integers or characters.
43
+ The answer is that Java’s primitive types are not implemented as objects.
44
+ Rather, they are implemented as “normal” variables.
45
+ This is done in the interest of efficiency.
46
+
47
+ It is important to understand that new allocates memory for an object during run time.
48
+
49
+ Box b1 = new Box();
50
+ Box b2 = b1;
51
+ b1 and b2 will both refer to the same object. The assignment of b1 to b2 did not allocate any memory or copy any part
52
+ of the original object. It simply makes b2 refer to the same object as does b1. Thus, any changes made to the object
53
+ through b2 will affect the object to which b1 is referring, since they are the same object.
54
+ When you assign one object reference variable to another object reference variable, you are not creating a copy of the
55
+ object, you are only making a copy of the reference.
56
+
57
+ int square(int i){
58
+ return i * i;
59
+ }
60
+ A parameter is a variable defined by a method that receives a value when the method is called. For example,
61
+ in square( int i), i is a parameter. An argument is a value that is passed to a method when it is invoked.
62
+ For example, square(100) passes 100 as an argument. Inside square( ), the parameter i receives that value.
63
+
64
+ NOTE:
65
+ Bus bus = new Bus();
66
+ lhs(reference i.e. bus) is looked by compiler & rhs (object i.e. new Bus()) is looked by jvm
0 commit comments