Method parameters are variables that are defined in the method's signature and are used to pass data into the method when it is called. In Java, method parameters are enclosed in parentheses after the method name.
public class Main {
public static void printSomething(String x) {
System.out.println(x);
}
public static void simpleCalc(int x, int y) {
int result = x + y;
System.out.println("Add: " + result);
}
public static void printChar(char x, char y) {
System.out.println(x + " " + y);
}
public static void main(String[] args) {
//printSomething("John Snow");
//printSomething("Samantha");
//simpleCalc(5, 5);
printChar('Z', 'B');
}
}
Here is a block by block explanation of the code:
public class Main {
This line declares a new public class called "Main".
public static void printSomething(String x) {
System.out.println(x);
}
This block defines a static method called "printSomething" that takes a String argument "x" and prints it to the console using the "println" method from the "System.out" object.
public static void simpleCalc(int x, int y) {
int result = x + y;
System.out.println("Add: " + result);
}
This block defines another static method called "simpleCalc" that takes two integer arguments "x" and "y", adds them together, and prints the result to the console.
public static void printChar(char x, char y) {
System.out.println(x + " " + y);
}
This block defines a third static method called "printChar" that takes two character arguments "x" and "y" and prints them to the console separated by a space.
public static void main(String[] args) {
//printSomething("John Snow");
//printSomething("Samantha");
//simpleCalc(5, 5);
printChar('Z', 'B');
}
}
This block defines the "main" method which is the entry point for the program. It contains four lines of code, but only one of them is not commented out. The uncommented line calls the "printChar" method with the characters 'Z' and 'B' as arguments.
No comments:
Post a Comment