Spaces:
Running
Running
File size: 1,595 Bytes
f7bf95f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
public static void printLarger(int num1, int num2) {
if (num1 > num2) {
System.out.println(num1);
} else {
System.out.println(num2);
}
}
public static void main(String[] args) {
printLarger(10,4);
printLarger(111, 11111);
printLarger(5 * 5, 3 * 9);
}
Program outputs:
10
11111
27
====================
parameter data type
eg double
public class Example{
public static void main(String[] args) {
double a;
a = 4.0;
a = 24;
float f = 23.32f;
a = f;
}
}
========================================
public class Example {
public static void main(String[] args) {
printSquare(3.5); //accept double
printSquare(10); //accept int
printSquare(1.5f);//accept float
}
public static void printSquare(double num) {
System.out.println(num * num);
}
}
Program outputs:
12.25
100.0
2.25
========================================
Method parameters can also be of different types, for example:
public class Example {
public static void main(String[] args) {
tempBetween(10, 30, 25.5);
tempBetween(-5, 5, -15.25);
}
public static void tempBetween(int min, int max, double temp) {
if (temp >= min && temp <= max) {
System.out.println("Temperature is between the given values!");
} else {
System.out.println("Temperature is not between the given values.");
}
}
}
Program outputs:
Temperature is between the given values!
Temperature is not between the given values.
|