Java programming
30.6K subscribers
131 photos
12 files
20 links
Download Telegram
You can see in the above example I have used multi-line comments to comment on the lines in a program. That’s how it works. Now, let’s understand what is documentation comments.
Documentation Comments
This type of comments is used generally when you are writing code for a project/ software package. It helps you to generate a documentation page for reference, which can be used for getting information about methods present, its parameters, etc.
What are Java Keywords and reserved words?
Keywords
are predefined which have a unique meaning and functionality in Java programming language. These keywords are also known as reserved keywords which mean they cannot be used as a variable name, class, method or any other identifier. There are 57 reserved keywords in Java. Meanwhile, in this huge list of java keywords, there are some which are not used anymore and few versions do not support a tiny number of keywords.
What is a Constructor in Java?
We create a constructor to initialize an object. They have the same name as the class but have no explicit return type. It can be used to set initial values for object attributes. It is similar to a Java method

At the time of calling the constructor, the memory is allocated for the object. Each class in Java has a constructor. Even if you do not create one, Java implicitly calls a constructor with all data members value set to zero.
When is a Constructor Called?

A constructor is called when an object or an instance is created. It is used to assign values to the data members of the same class.

Rules For Constructors in Java
1 The name of the constructor should be the same as that of the class name.
2 A constructor cannot be declared as final, static, synchronized or abstract.
3 It cannot have an explicit return type.
4 A constructor can have an access modifier to control the access.

You should follow these rules while creating a constructor.

Types of Constructors in Java
There are two types of constructors

1 Default Constructor
2 Parametrized Constructor

Default Constructor
A constructor with no arguments is called a default constructor. If we do not create a constructor of a class, Java creates a default constructor with data members which has values like zero, null, etc.

But, if we specify a constructor with no arguments, it will be a default constructor or a no argument constructor which is another name for default constructor. Following is an example to show how to use a default constructor in Java:
Parametrized Constructor
A constructor which has arguments is called as a parametrized constructor. It is used to assign values to distinct objects. Following is an example to show how we declare a parameterized constructor in java:
Constructor Overloading
Just like method overloading, constructors can be overloaded to create objects in different ways. The compiler differentiates constructors based on how many arguments are present in the constructor and other parameters like the order in which the arguments are passed.

Following is an example of constructor overloading:
Difference Between Method And Constructor
What is the use of Destructor in Java?

What is a Destructor?
A destructor is a special method that gets called automatically as soon as the life-cycle of an object is finished. A destructor is called to de-allocate and free memory. The following tasks get executed when a destructor is called.

- Releasing the release locks
- Closing all the database connections or files
- Releasing all the network resources
- Other Housekeeping tasks
- Recovering the heap space allocated during the lifetime of an object

Destructors in Java also known as finalizers are non-deterministic. The allocation and release of memory are implicitly handled by the garbage collector in Java.

Finalizers in Java have to be implicitly invoked since their invocation is not guaranteed, unlike C# finalizers which get invoked during the .NET run-time.

Let’s take a look at key properties of a destructor:

- Overloading or inheritance is not allowed
- No specification of access modifiers or parameters
- Automatic invocation and no explicit call from the user
- Used in classes but not in structures
- The order of the class varies from the most derived class to the least derived class
- Also called when the object instance is no longer eligible for access
- Used for releasing un-managed resources instead of managed resources held by the object

Garbage Collector
A garbage collector is a program that runs on the Java virtual machine to recover the memory by deleting the objects which are no longer in use or have finished their life-cycle. An object is said to be eligible for garbage collection if and only if the object is unreachable.
Let’s try to understand how garbage collection works in Java:

Garbage collection is mainly the process of marking or identifying the unreachable objects and deleting them to free the memory. The implementation lives in the JVM, the only requirement is that it should meet the JVM specifications. These are the different types of garbage collectors in Java:

- Serial Garbage Collector
- Parallel/Throughput Garbage Collector
- CMS Collector
- G1 Collector

Let’s take a look at a few advantages of garbage collection in Java:

- It automatically deletes the unused objects that are unreachable to free up the memory
- Garbage collection makes Java memory efficient
- It need not be explicitly called since the implementation lives in the JVM
- Garbage collection has become an important and standard component of many programming languages

Let’s try to understand why Destructors are not used in Java.