Java Beginner Guide 1

Java is an object-oriented programming language. Object Oriented Programming concepts are abstraction, encapsulation, inheritance, and polymorphism. First question What is class and object ?

Updater
2 min readFeb 27, 2021

Class

A class is template of an object and object is instance of a class. Methods(Functions) and variables inside a class are called as members of class. It creates a physical reality like a new type of data(In others a new data type).

Object

A object is instance of a class. You can use it how many times you want by creating as many objects.

In this example, myjava is a object(reference) and access all the data from the class. In this declaration new keyword used because it allocates the memory for a object. Declaring a object is same a creating variable and assigning a value to it.

You may feel that in java why don’t int, float and all primitive types aren’t instantiated like this because in java they are two types

  1. Primitive types are int,float,double,byte,char,long,short.(Simple values)
  2. Reference types are complex data like arrays and strings ,etc.

So these all comes under references types.

Constructor

It is automatic initialization of themselves when they are created. It has no return type and not even void, in fact it’s return type is class type of itself. It has the power to instantiate the object so it creates the instance of a class(Kick start).

Example Java1 myjava =new Java1();

Java1() is a constructor that’s why we are using parentheses when we are declaring a object. Once you define your own constructor he default one will be gone. There are also parameterized constructor you can create it for different dimensions of it.

Garbage Collection

As we know new keyword allocates the memory for the object in runtime execution. Once after we used it how it deallocates the memory. In C++, We have to manually deallocate it but in java it will automatically deallocate the memory after its use. It is called as garbage collection. Java executes the program in a way it has two components heap and stack. Like I said all the garbage collection part happens in stack which has nature of first in and last out. We will discuss more about it in next day…

--

--

Updater
0 Followers

Life is to Learn and do as u think...