DEV Community

Utkarsha114
Utkarsha114

Posted on

Java Concepts

Garbage collection in Java is the automated process of deleting code that's no longer needed or used. This automatically frees up memory space and ideally makes coding Java apps easier for developers. The garbage collection implementation lives in the JVM.
We can also request JVM to run Garbage Collector. There are two ways to do it : 

•Using System.gc() method: System class contain static method gc() for requesting JVM to run Garbage Collector.

•Using Runtime.getRuntime().gc() method: Runtime class allows the application to interface with the JVM in which the application is running. Hence by using its gc() method, we can request JVM to run Garbage Collector.

~ Explain public static void main(String args[]) in Java.

Unlike any other programming language like C, C++, etc. In Java, we declared the main function as a public static void main (String args[]). The meanings of the terms are mentioned below:

• public: the public is the access modifier responsible for mentioning who can access the element or the method and what is the limit.  It is responsible for making the main function globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class.

• static: static is a keyword used so that we can use the element without initiating the class so to avoid the unnecessary allocation of the memory. 

• void: void is a keyword and is used to specify that a method doesn’t return anything. As the main function doesn’t return anything we use void.

• main: main represents that the function declared is the main function. It helps JVM to identify that the declared function is the main function.

• String args[]: It stores Java command-line arguments and is an array of type java.lang.String class.

• There is no difference between String[] args and String args[] in Java. Both are valid declarations of an array of strings that can be used to store command-line arguments passed to the program.

However, the first declaration is more common and is preferred by most Java programmers. This is because it is more consistent with the way that other arrays are declared in Java. For example, you would declare an array of integers as int[], and an array of floats as float[].

The second declaration is a bit more concise, but it can be less clear to someone who is not familiar with Java. This is because the [] at the end of the variable name is not typically used to declare arrays in other programming languages.

Ultimately, the choice of which declaration to use is a matter of personal preference. However, most Java programmers prefer to use the first declaration, String[] args, because it is more consistent and clear.

In short, The two notations are interchangeable and have the same meaning. However, the preferred and more commonly used notation is [code]String[] args[/code], which clearly indicates that [code]args[/code] is an array of type [code]String[/code].

Top comments (0)