Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 17, 2024 03:22 am GMT

Java Concepts

Garbage collection in Java isthe 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 :

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

UsingRuntime.getRuntime().gc()method:Runtime classallows 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 doesnt return anything. As the main function doesnt 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 betweenString[] argsandString 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 asint[], and an array of floats asfloat[].

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].


Original Link: https://dev.to/utkarsha114/java-concepts-4gjo

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To