Java-by-telsuko
Java-by-telsuko
OAK to JAVA
URL:- https://www.youtube.com/watch?v=WOUpjal8ee4&list=PLGwb7xZHg-
oMv1pOlTHAqAEjw0EPALzlL
Varangs
If user pass one argument then we should call one argument method , if user passess two arguments
, then we should call two argument methods
6.15 Different ways of writing main method in java
Objects are created using class , to work with the class we need object
New keyword is used to initiate memory for obj in heap and how much memory is decided by
constructor A() it is not a method
New A() will create object in heap memory which in java is called instance
Objects will have variables and methods
Inorder to access the data in the object we need to create a reference so that’s why we have A obj .
Every instance in heap memory will have address location called hashcode
Whenever we use obj.show() your reference will fetch the value from stack to heap memory
every time your write new A() , it will create a new obj in location 103
If you want to travel a long distance in train you will book in sleeper class . You will not take your
home pillow in train , you will buy air pillow
When you go to shop , they will give flat pillow , to use we must blow some air, how much air we
should be fill should be of exact amount
We should use new key word , this new keyword will blow air/memory to your reference and how
much will be decided by constructor
Constructor in java
This main method must be static , you cannot call main without object and you cannot create object
without main , it is a deadlock .
Inorder to call main you don’t require the object so we use static keyword .
Constructor:-
Constructor is the member method which has the same name as class name
We can also provide default values for variables inside the constructor
And you can also have multiple constructor and this method of having constructors with multiple
parameters is called constructor overloading
When you define a parameterized constructor , you should also define a default constructor
if we don’t pass any value
it will call default constructor
When ever you pass any object it goes in the form of value called hashcode .
when a object of paper is created ,you will have 101 in heap memory which points to main method
value p which is stored in stack memory.
Now what happens when you pass this object to the printer , we will be having on more object for
the method print and it will have a new reference but the hashcode is same as object is same,
If we change the object using print method reference and accessing the object using first main
reference we will get the updated value
We are not passing the object , but we are passing hashcode so it is called pass by value not pass by
reference .
If you have all the classes in the same location it will be complicated. To make it more manageable
we will move classes inside a package which has similar functionality
Now we get the error . if you don’t mention public class you cannot access it from another package
to access the method outside the package we must make it public
if you don’t want to instantiate obj , you can use static keyword
to call the static method you need class name
we cannot use a non static variable in a static method
But not static variables cannot be used inside static methods , because if we try to increment then it
doesn’t know which num1++ to increment whether for obj1 or obj2 , every obj will have their own
value of num1 .
so lets us take class variable is a variable which is shared across the class
Here we are creating object for class b and the output we are expecting is for constructor b
but the output is something
By default every child class constructor will have super keyword which calls the constructor of parent
class
But the super keyword in parent class A what is the use , which constructor does it call
By default in java every class extends Object class that means if you use super then it calls the default
constructor of object class
Here we have method overriding with parameter i in both class A and class B ,
If we call object with parameter in class B , will it call the default constructor or parametrized
constructor in class A
Every constructor by default have super() method without parameter so we can use parameter in
super method
Even if we don’t assigne in same line , in next also we can assign but once it is assigned we cannot
change it
as b class doesn’t have show method then it takes from show method
method overriding
you cannot override a method with final keyword
Class variable is defined inside class but static type
the value 0of x is o because it is a local variable ,to mention the x as instance variable we have to use
this , this represents the current instance
8.15 Anonymous object
A obj -> here obj is a referenced variable of type class A but to call the object we use = new A();
Obj is created in heap memory whith two fields one takes variables and other takes methods.
but when we create variable constructor it takes memory in stack , but in some scenarios if we call
the variable only once then why to create it in stack memory and waste the memory .
To achieve it use
Obj.show()
New A().show(); -> this is anonymous object it will not use any memory in stack
the GC comes and checks and when there is no reference objects like anonymous it will be eligible
for garbage collection
because new A() creates new obj and by default it s value is 0
In java by default every class extends object class and when you try to print s1
And we try to call toString() method but we don’t have it in our class by default extends object class
It will go to the object class method and fetch the hash code
9.3 user input system.in.read()
It is taking the ascii value and printing it
Mutable means change, if you want to increase the performance of the system , make it immutable
When we create a string and assign it a value , then in side the stringpool of heap it is created with
some address
now we are creating str1 with Navin value same as str , now if we created str = “Reddy” will it change
str1 also ?
that’s not the case , it will create a new value in new location 103.
you are changing the address not the value , that’s why strings are immutable.
Here we are using new keyword which also object and that also
Shallow copy : you have created one object and two references
Heap memory will have one object and stack memory will have to references
Deep copy : you are manually copying the objects one by one
clone : it will create a new object for you . it is a combination of shallow+deep copy
here we are creating two references
Now change