20 10 2022 Class Notes
20 10 2022 Class Notes
-----------------------------------
Introduction to oops(classes and objects)
Types of variables
Division - 1
a. primtive variable
b. reference variable
Division -2
a. instance variable
b. local variable
c. static variable
JVM Area for execution
a. MethodArea(.class data/static data)
b. Heap Area(instance varaibles/ object data)
c. Stack Area(local varaibles)
d. PC-Register
e. Native method area
OOPs
-------
It is actually theory concept,which is implemented by many programming language
like c++,java,python,...
Any real time problem can be solved if we follow oop's principle.
In OOP's, while solving the problem
1. We need to first mark the Objects.
2. Every Object we mark should have 2 parts
a. HAS-Part/fields/attributes (store the information as
variables)
b. Does-Part/behaviours(represent them as methods)
3. To represent an Object, first we need 2 have a blueprint of an Object.
4. we use "new" keyword/reserve word to create an Object for a
blueprint(class).
5. Every Object should always be in constant interaction
6. Useless Object doesn't exists.
What is Object?
Physical existense of any element we say as Object.
eg: book,Car,Computer,Dog,Student,......
Syntax:
ClassName variable=new ClassName();
new -> it is a signal to jvm to create some space for the Object in the heap area.
Tell the className,we inform the classname, JVM create the object and
sends the
"hashCode" to the user.
User should collect the hashCode through "reference variable".
Types of variables
==============
Division 1 : Based on the type of value represented by a variable all variables are
divided into 2 types.
They are:
1. Primitive variables
2. Reference variables
Primitive variables:
Primitive variables can be used to represent primitive values.
Example: int x=10;
Reference variables:
Reference variables can be used to refer objects.
Example: Student s=new Student();
instance variable
If the variable is declared inside the class, but outside the methods such
variables are called as
"instance variables".
or
if the value of the variables changes from object to object then such
variables are called as "instance variables"
eg#1.
Student std1= new Student();//id = 10, name =sachin
Student std2= new Student();//id = 7, name=dhoni
Note: scope of instance variable would be available only when we have reference
pointing to the object,
if the object reference becomes null, then we can't access "instance
varaibles".
eg#1.
public class Test {
boolean b;
public static void main(String[] args) {
Test t=new Test();
System.out.println(t.b);//false
}
}
eg#2
public class Test {
int i=10;//instance variable
public static void main(String[] args) {
System.out.println(i);//CE: instance variable can't be accessed
directly in static context
local varaibles
------------------
1. Variables which are created inside the method are called local variables and
memory for those variables
will be given in the stackarea.
2. During the execution of the method the memory for local variables will be
given,and after the execution of
the method the memory for variables will be taken out from the stack area.
3. Local varaibles default value will not be given by the JVM, programmer should
give the default value.
4. If the programmer doesn't give default value and if he uses the varaible
inside the method then program would
result in "CE".
eg#1.
public class Test {
public static void main(String[] args) {
int i=0;
for(int j=0;j<3;j++)
{
i=i+j;
}
System.out.println(i);//valid
System.out.println(j);//CE: 'j' variable not declared
}
}
eg#2.
class Test {
public static void main(String[] args) {
try{
int i=Integer.parseInt("ten");
}
catch(NullPointerException e){
System.out.println(i);//CE: 'i' not declared
}
}
eg#3.
class Test{
public static void main(String[] args){
int x;
System.out.println("hello");//hello
}
}
Note: code would be compiled becoz variable x is not used anywhere.
eg#4
class Test{
public static void main(String[] args){
int x;
System.out.println(x);//CE: 'x' not initalized
}
}