0% found this document useful (0 votes)
2 views

Java-by-telsuko

The document discusses various concepts in Java programming, including object-oriented principles such as classes, objects, constructors, and methods. It explains how to create and manage objects, the significance of the 'new' keyword, and the use of packages and access modifiers. Additionally, it covers advanced topics like method overriding, immutability of strings, and object cloning.

Uploaded by

Shiva Ram Vemula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java-by-telsuko

The document discusses various concepts in Java programming, including object-oriented principles such as classes, objects, constructors, and methods. It explains how to create and manage objects, the significance of the 'new' keyword, and the use of packages and access modifiers. Additionally, it covers advanced topics like method overriding, immutability of strings, and object cloning.

Uploaded by

Shiva Ram Vemula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 62

James Gosling ( sun micro systems)

OAK to JAVA

URL:- https://www.youtube.com/watch?v=WOUpjal8ee4&list=PLGwb7xZHg-
oMv1pOlTHAqAEjw0EPALzlL

Jagged array has uneven number of elements in rows


3 dimensional array

Using for each loop

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

If we mention input parameter as string in main method


It will throw error

7.1 Class and Objects in java

To build a building we need blueprint/structure

Building is object and blueprint is a class

Class defines the structure and working of an object


Object has two parameters

What object knows is variables

What object does is methods

Objects are created using class , to work with the class we need object

Jvm creates object but to create them they require blueprint/class

7.2 Creating object in java

To create an object we use A obj here obj is a reference

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

A obj = new A()

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 .

Obj is a reference which is stored in stack memory

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

The old link is breaked and new link is established


The old object is eligible for garbage collection

7.3 Object instantiation

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

In the same way if you create class reference

A obj is a flat pillow

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

The execution of code by jvm starts with main method ,

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

Contructor will not return anything ,so use public


So everytime if you want to create an object should use constructor , if you don’t define constructor
it will be there inside the class so that’s why they are called default constructor ,

We can also provide default values for variables inside the constructor

Like i=5 ,f=5.5f


As soon as you create the object it will call default constructor block automatically.

And you can also have multiple constructor and this method of having constructors with multiple
parameters is called constructor overloading

A(6) it will print hello


7.5 Constructor hands on

The error because you cannot call constructor explicitly

Whenever we create object then the constructor is automatically 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

7.8 object passing in java


In this video we will talk about how to pass object as a parameter to a method

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,

We have one object and two references.

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 .

7.9 Packages in java

All the class related to networking are placed in network package


why we require package concept , why we cannot put all the classes in one file
if you are building a large project

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

we can import the classes using import command


access modifiers
now both the classes are of same package to you don’t get any error , lets say

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 don’t mention anything it will take default


to call the method from a class you must create an obj

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

only static variables can be used inside static methods


why non-static variables cannot work in static method

Will this code work


for all the objects will share the same value for the num2 and own value for instance variable
Static variables can be used in non static methods

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 .

7.15 count no of objects of class


Here i is a instance variable it will have different values for each instance/object

so lets us take class variable is a variable which is shared across the class

How to create a class variable is by using static keyword

static keyword usage


Suppose if you want to execute some block of code before even the execution of main method then
you can use static keyword

8.11 super keyword in java

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

This is the use of super keyword as a methods


it will call the abc method of super class
final keyword means once the value is assigned it will not change

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

Int value -> here value is a primitive variable of datatype int

A obj -> here obj is a referenced variable of type class A but to call the object we use = new A();

What is happening in the backend

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

A obj = new A()

Assume x=obj -> referenced object

And y = new A()

Where ever there is y we can replace it with x

So in place of new A()

Use x -> new A()

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

9.1 String operations in java


When we print only s1 we get the hash value
package name. classname@hashvalue

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

9.9 String Immutable

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.

String s1 = new String(“Navin”),

Here we are using new keyword which also object and that also

uses string pool memory


Object cloning

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

You might also like