T10: Knowledge Tip Of The Day...
Methods in Java
Programs have methods that
have a name and contain a set of statements or instructions
In programming language, a method is a section of the program that contains a set of instructions or code. In a Java program, similar to a cake recipe, a method has a set of instructions. When the method is called, the set of instructions within the method is executed.
public void addition()
{
System.out.printIn(10+10);
}
In programming language, a method is a section of the program that contains a set of instructions or code. In a Java program, similar to a cake recipe, a method has a set of instructions. When the method is called, the set of instructions within the method is executed.
public void addition()
{
System.out.printIn(10+10);
}
Parameters of a Method
Methods have parameters
(like ingredients) or data that are inputted or passed into the method. The
method uses these parameter values to do the necessary data manipulation and
processing.
public void addition (int x, int y)
{
int z ;
z = x+y;
System.out.printIn( z);
}
The parameters of a method are declared within parentheses following the method name. If there is more than one parameter, they are separated by commas. Both the data type and the variable name (int x, int y) are specified for the parameters.
public void addition (int x, int y)
{
int z ;
z = x+y;
System.out.printIn( z);
}
The parameters of a method are declared within parentheses following the method name. If there is more than one parameter, they are separated by commas. Both the data type and the variable name (int x, int y) are specified for the parameters.
Returning a value from a
Java method
The result returned by the
method is also available for use by the java program to which this method
belongs.
public int addition (int x, int y)
{
int z ;
z = x+y;
return z;
}
When the method is declared, the return type of the method is listed just before the name of the method.
The return statement is a java keyword return followed by the variable name.
public int addition (int x, int y)
{
int z ;
z = x+y;
return z;
}
When the method is declared, the return type of the method is listed just before the name of the method.
The return statement is a java keyword return followed by the variable name.
Methods that do not return
a value
When a method does not
return a value, it is indicated by the keyword void before the name of the
method.
public void addition (int x, int y)
{
int z;
z = x+y;
System.out.printIn( z);
}
public void addition (int x, int y)
{
int z;
z = x+y;
System.out.printIn( z);
}
This method does not return
a value. Instead, it only prints the result of x+y on the screen. We can tell
that this method does not return a value because:
(1) it has no return statement
(2) the method name (addition) is preceded by the keyword void
When a method does not return a value, any variable declared inside the method is not available outside the method.
(1) it has no return statement
(2) the method name (addition) is preceded by the keyword void
When a method does not return a value, any variable declared inside the method is not available outside the method.
Methods without Parameters
If a method has no parameters, the method can still declare
variables, process the variables declared within the method, and return the
results.
public int addition ()
{
int z ;
int x;
int y;
z = x+y;
return z;
}
public int addition ()
{
int z ;
int x;
int y;
z = x+y;
return z;
}
Regards,
javabykiranhr1@gmail.com
#Java #Learn #Upgrade
#Training #Classes #OnlineLecture #OnlineVideos #Pune
#Call Now to book your
seat to JOIN Java Training @ 7219474925, eMail @ javabykiranhr1@gmail.com
#Java
#J2EE
#Core
+#Advance + #Struts +#Hibernate + #Spring + #REST + #OCJP + #OCWCD + #Maven +
#Junit + #Jquery #Ajax + #XML
#Spring
Boot + CI and many more ..
#Selenium
#Complete
#Java + #Selenium + #TestNg + #Jenkin + #Webdriver + #Cucumber +#Maven +
#DataProvider + #Keyword + #Hybrid + #POM End to End integration #Jenkin #Maven
#Testng and many more ..
#JavabyKiran
#Expert #Training #BestInstitute #Java #Python #Selenium #Freshers
#Professionals #IT #Software #Career #Interview #Jobs#Jobopening #BestTrainingInstituteinPune #ITCourses #OnlineVideo #Pune
No comments:
Post a Comment