In order to print some information on the console, in java, we have a method called
System.out.println() method.
Standard syntax:
System.out.println();
>>>class Demo{
int a1=456, a2= 789;
static String name="RAMESHSOFT"
public int addition()
{
int result =a1+a2;
return result;
}
}
Ex:
int a1=456, a2= 789;
static String name="RAMESHSOFT"
Syntax:
1.System.out.println(String message);
System.out.println("Please do practice and it is mandatory");
Whatever we write in double-quotes as it is it will print on the console.
2. To print variable values on the console
System.out.println(variablename);
To print more than one variable
System.out.println(varname1+varname2+varname3+.......);
>>>System.out.println(a1);
output: 456
>>>system.out.println("a1");
Output: a1
>>> System.out.println(a1+a2);
here addition is going to be happen
output: 1245
here + is acting like a addition
System.out.println(a1+name);
output: 456 RAMESHSOFT
concatenation happened.
here + is acting like a concatenation
In java the only one overloading operator is + , sometimes it acts like addition and sometimes it acts like concatenation
>>> System.out.println(a1+name+a2);
Output: 456 RAMESHSOFT 789
>>> System.out.println(a1=a2+name);
Output: 1245 RAMESHSOFT
3. To print some message along with variable value
syntax:
System.out.println(String msg+variablename);
or
System.out.println(variablename+String msg);
>>> System.out.println("The value of a is:"+a1);
>>>System.out.println(a1+" The value of a is");
Output:
The value of a is: 456
456 The value of a is
4. To print morethan one variable with some message
System.out.println(String msg1+variablename1+ String msg2+variablename2);
or
System.out.println(variablename1+String msg1+variablename2+String msg2);
Example:
>>> public class Addition{
public int numberOne=45;
public int numbertwo=47;
public int addition()
{
int result=numberOne+numberTwo;
return result;
System.out.println("result is: "+result);
}
}
Whenever we compile this program we will get a compile-time error because after the return statement we should not write any statements.
A method should not contain more than one return statement.
per method we should write only one statement otherwise we will get a compile-time error.
return statement should be the last statement inside a method.
>>> public class Addition{
public int numberOne=45;
public int numbertwo=47;
public int addition()
{
int result=numberOne+numberTwo;
System.out.println("result is: "+result);
return result;
}
}
It will compile without any errors.
In the above program inside the method, we implemented the business logic of addition between two numbers.
Whenever we call this method it is going to be performed addition between two numbers and return result.
1. We implemented one business logic then we need to testing that it is working correctly or not.
2 in order to write a program in java we need to follow 3 standard steps
2.1 write a program
save with .java extension
2.2. validate/compile
2.3. execute
In order to execute the java program standard syntax is
<javac> <class name> .java
To execute standard syntax is
,java> <classname>
>>> javac Addition_Demo.java
>>> java Addition_Demo
The moment we click on enter here JVM is going to start. once JVM is started in this desktop JVM check this .class file is there or not.
JVM loads the .class file, whenever JVM is loading the .class file automatically all the static variables and static methods are going to be created.
JVM is going to look execution
Whenever u ask the JVM to run the program JVM is going to look for the starting point to execute the program
Ex: to start a car we need a key point
All the static blogs are going to be executed automatically.
1. check for the class file
2. JVM looks for .class file
3. loads the .class file
4. JVM looks for execution point
What is the execution point?
Jvm is always going to starts the execution from the main() method.
the main method is the starting point for the execution
If the main method is not there we will get an error.
What is the main method in java?
In java, the main method is there for two reasons
1. whenever u ask the JVM to run the program, JVM always starts execution from the main method.
why?
JVM is internally configured in such a way that whenever the programmer is asked to run the program always starts the execution from the main method.
if the main method is not there JVM doesn't know where to start the execution that's why it's throwing a compile-time error.
* In order to execute a program, we need a main method
2. Inside the main method what we will do?
We will do testing
How to define the main method:
Standard syntax:
1. public static void main(String[] args)
{
// accessing or testing the implemented services or methods
}
2. static public void main(String[] args)
{
// accessing or testing the implemented services or methods
}
3. public static void main(String... args)
{
// accessing or testing the implemented services or methods
}
In place of the args whatever we want we can write
4. public static void main(String[] RAMESHSOFT)
{
// accessing or testing the implemented services or methods
}
{
// accessing or testing the implemented services or methods
}
/* In place of [] we can write ... three dots
6. strictfp public static void main(String[] args)
{
// accessing or testing the implemented services or methods
}
All the above main methods are valid but commonly we use first method
Main method is bydefault static method.
Where we need to define the main method
1. After starting the class immediately take main method
<as> class <classname> {
public static void main(String[] args)
{
// accessing or testing the implemented services or methods
}
;;;;;;;;;;;;;;;;;;;///defined or implemented part
;;;;;;;;;;;;;;;;;
}
2. Before end of a class take main method
<as> class <classname> {
;;;;;;;;;;;;;;;;;;;///defined or implemented part
;;;;;;;;;;;;;;;;;
public static void main(String[] args)
{
// accessing or testing the implemented services or methods
}
}
3. In between the class take main method
<as> class <classname> {
;;;;;;;;;;;;;;;;;;;///defined or implemented part
;;;;;;;;;;;;;;;;;
public static void main(String[] args)
{
// accessing or testing the implemented services or methods
}
;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;; //implemented or defined part
}
4. Take main method in seperate class
<as> class <classname> {
;;;;;;;;;;;;;;;;;;;///defined or implemented part/services part
;;;;;;;;;;;;;;;;;
}
<as> class <classname> {
}
<as> class <classname> {
public static void main(String [] RAMESHSOFT)
{
// accessing or testing the implemented methods / services
// execution part
}
}
>>> public class Addition_Demo{
public int numberOne=45;
public int numbertwo=47;
public int addition()
{
int result=numberOne+numberTwo;
System.out.println("result is: "+result);
return result;
}
public static void main(String[] RAMESHSOFT)
{
System.out.println("JVM started execution")
}//end of main method
}//end of class
there is nothing to execute
.
When this above method is going to be executed?
r u calling that method?
no
so it won't respond
We implemented one business service we need to call it
>>>javac Addition_Demo.java
>>>java Addition_Demo
public static void main(String[] RAMESHSOFT)
public void main(String[] RAMESHSOFT)
if we remove static we will get a compile-time error.
public static void main(String[] ...) ===> valid
No comments:
Post a Comment