0% found this document useful (0 votes)
8 views

Java 9

The document discusses the main method in Java, including its signature and modifiers. It also covers command line arguments and how to access them in the main method. Finally, it discusses System.out.println() and how it is used to output text in Java programs.

Uploaded by

umamucheli34
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java 9

The document discusses the main method in Java, including its signature and modifiers. It also covers command line arguments and how to access them in the main method. Finally, it discusses System.out.println() and how it is used to output text in Java programs.

Uploaded by

umamucheli34
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Main method

============
Our program contains main method or not.Either it is properly declared or not.It is
not a responsibility of a compiler to check.It is a liability of a JVM to look for
main method.

JVM always look for main method with following signature.


ex:
public static void main(String[] args)

If we perform any changes in above signature then we will get runtime error called
main method not found.

public
-----
JVM wants to call this method from anywhere.

static
------
JVM wants to call this method without using object reference.

void
----
Main method does not return anything to JVM.

main
----
It is an identifier given to main method.

String[] args
----------
It is a command line argument.

We can perform following changes in main method.

1) Order of modifiers is not important because instead of public static we can


take static public also.
ex:
static public void main(String[] args)

2) We can change String[] in following acceptable formats.


ex:
public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])

3) We can change String[] with var-arg parameter.


ex:
public static void main(String... args)

4) We can replace args with any java valid identifier.


ex:
public static void main(String[] ihub)

5) Main method will accept following modifiers.


ex:
synchronized
strictfp
final
Command line arguments
======================
Arguments which we are passing through command prompt such type of arguments are
called command line arguments.

In command line argument we need to pass our inputs at runtime command.

ex:
javac Test.java

java Test 101 raja M 1000.0


| | | |____ args[3]
| | |__________ args[2]
| |_______________ args[1]
|____________________ args[0]

ex:
---
class Test
{
public static void main(String[] args)
{
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
System.out.println(args[3]);

}
}

Q)Write a java program to accept one name and display it?

class Test
{
public static void main(String[] args)
{
System.out.println("Enter the Name :");
String name=args[0];
System.out.println("Welcome :"+name);
}
}

javac Test.java

java Test LinusTorvalds

System.out.println()
====================
It is a output statement in java.

If we want to display any userdefined statements and data then we need to use
output statement.

syntax:
-----
static variable
|
System.out.println();
| |
predefined predefined method
final
class

Diagram: java9.1

ex:
---
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");
System.out.print("stmt2");
System.out.printf("stmt3");
}
}

Various ways to display the data


------------------------------------
1)
System.out.println("Hello World");

2)
int i=10;
System.out.println(i);
System.out.println("The value is ="+i);

3)
int i=10,j=20;
System.out.println(i+" "+j);
System.out.println(i+" and "+j);

4)
int i=1,j=2,k=3;
System.out.println(i+" "+j+" "+k);

EditPlus Editor
==============
Download link : https://www.editplus.com/download.html

Fully Qualified Name


====================
Full qualified name will increase readability of our code.

We will declare class name and interface name along with package name.

ex:
java.lang.System
java.lang.String
java.lang.Object

ex:
---
class Test
{
public static void main(String[] args)
{
java.util.Date d=new java.util.Date();
System.out.println(d);
}
}

You might also like