Java programming
30.6K subscribers
131 photos
12 files
20 links
Download Telegram
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.
Java Finalize() Method
It becomes fairly difficult for any developer to force the execution of a garbage collector, but there is an alternative to this. We can use the object.finalize() method which works exactly like a destructor in Java.
An Object.finalize() method is inherited in all Java objects. It is not a destructor but is used to make sure or provide additional security to ensure the use of external resources like closing the file, etc before the program shuts down. You can call it by using the method itself or system.runFinalizersOnExit(true).

The use of finalize() method is highly not recommended since it can be very unsafe and in some cases used incorrectly.

Let’s take a simple example to show how finalize() can be used to call the garbage collector.
What is the right data type to represent a price in Java?
Anonymous Poll
26%
int
10%
decimal
18%
Bigdecimal
29%
double
17%
float
The answer is bigdecimal
public class MyClass {
{
public static void main(String[] args) { {for (int i = 5; i > 0; i--) { System.out.print(i); } } }}
Anonymous Poll
12%
1 2 3 4 5
12%
0 1 2 3 4
69%
5 4 3 2 1
7%
4 3 2 1 0
The answer is 5 4 3 2 1
class Check
{
public static void main(String args[]) { int num = 9; if ( num % 2 == 0 ) System.out.println("output1"); else System.out.println("output2"); } }
Anonymous Quiz
31%
output1
69%
output2
What is a Method?
A method is basically a set of code which is referred to by name and can be called or invoked at any point in a program, just by utilizing the method’s name. Each method is given its own name. When that name is in a program, the execution branches to the body of that method. Suppose the method is completed, control returns to the area of the code from which it was called, and the program continues on to the next line of code.
Methods are basically referred to time savers, which means that they allow you to have the repetition of sections of code without having to retype the code. In addition to this, methods can also be saved and utilized again and again in a freshly developed program. These methods are used to perform certain actions, and they are also called as functions.

Different types of methods in Java
Talking about the different types of methods used, there are two types, which are:

- Standard Library Methods
- User-defined Methods

Let’s understand them in detail.

Standard library methods
The Standard library methods are built-in methods in Java that are readily available for use. These standard libraries come along with the Java Class Library that is present in a Java archive (*.jar) file with JVM and JRE.

How to use the standard libraries?
The file stdlib.jar holds together all standard libraries into one single file. To access these libraries, you must add stdlib.jar to your Java classpath. There are several ways to do so. Here are the two ways through which you can do:

Use the javac-introcs and java-introcs commands. javac-introcs and java-introcs commands are accessible with the command line (for OS X or Linux) or Git Bash (for Windows).
% javac-introcs MyProject.java
% java-introcs MyProject
Use IntelliJ project folders. If you use IntelliJ, the project folders are preconfigured to include stdlib.jar in the classpath.
Some examples of Standard libraries are:

print(): This method comes under java.io.PrintSteam which helps in printing the string that is written within the quotation.
sqrt(): This is a method of Math class that returns the square root of that specific number.
How to create a method?
A method must be declared within a specific class. It is defined with the name of the method, followed by parentheses “()”. Java provides some pre-defined methods, such as System.out.println() and many more.
And the method definition consists of a method header and a method body.

Now, Let’s understand how to create a user-defined Method in Java?

How to create a user-defined method in Java?
Syntax: