[Go to site: main page, start]

0% found this document useful (0 votes)
4 views3 pages

Java Notes

The document provides an overview of Java methods, including their definitions, return types, and how to call them. It explains concepts such as method overloading, parameters, arguments, and return values, along with practical examples of using loops and conditional statements. Additionally, it covers the use of arrays and HashMaps in Java programming.

Uploaded by

vallestonying
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Java Notes

The document provides an overview of Java methods, including their definitions, return types, and how to call them. It explains concepts such as method overloading, parameters, arguments, and return values, along with practical examples of using loops and conditional statements. Additionally, it covers the use of arrays and HashMaps in Java programming.

Uploaded by

vallestonying
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Methods Example: can use a primitive data type (such as

 myMethod() is the name of the int, char, etc.) instead of void, and use
method the return keyword inside the method.
 static means that the method
belongs to the Main class and not public class Main {
an object of the Main class. You static int myMethod(int x) {
will learn more about objects and return 5 + x;
how to access methods through }
objects later in this tutorial. public static void main(String[] args) {
 void means that this method [Link](myMethod(3
does not have a return value. You ));
will learn more about return }
values later in this chapter }
// Outputs 8 (5 + 3)
Call A Method in Java, write the
method's name followed by two Java Method Overloading
parentheses () and With method overloading, multiple
a semicolon; methods can have the same name with
different parameters.
public class Main {
static void myMethod() {
[Link]("I just got static int plusMethodInt(int x, int y) {
executed!"); return x + y;
} }
public static void main(String[] args) { static double plusMethodDouble(double
myMethod(); x, double y) {
} return x + y;
} }
// Outputs "I just got executed!" public static void main(String[] args) {
int myNum1 = plusMethodInt(8,
Java Method Parameters and 5);
Arguments double myNum2 =
public class Main { plusMethodDouble(4.3, 6.26);
static void myMethod(String fname, int [Link]("int: " +
age) { myNum1);
[Link](fname + " is " + [Link]("double: " +
age); myNum2);
} }
public static void main(String[] args) {
myMethod("Liam", 5); Java Methods
myMethod("Jenny", 8); public class Main
myMethod("Anja", 31); {
} public static void main(String[] args) {
} [Link](printHelloWorld(
When a parameter is passed to the ));
method, it is called an argument. [Link]("sum of two
So, from the example above: fname is a integers: " + sumOfTwoNumbers(5, 6));
parameter, while Liam, Jenny [Link]("sum of two
and Anja are arguments. double: " + sumOfTwoNumbers(5.4,
7.3));
Java Return Values double exponentResult =
- The void keyword, indicates that the exponent(2, 5);
method should not return a value. If you [Link]("The exponent
want the method to return a value, you is " + exponentResult);
} }
static String printHelloWorld() { int time = 22;
//this method prints Hello print if (time < 10) {
Hello printHello [Link]("Good morning.");
return "Hello World"; } else if (time < 18) {
} [Link]("Good day.");
static int sumOfTwoNumbers(int } else {
num1, int num2) { [Link]("Good evening.");
int sum = num1 + num2; }
return sum; // Outputs "Good evening."
} //Short Hand if...else
static double int time = 20;
sumOfTwoNumbers(double num1, String result = (time < 18) ? "Good
double num2) { day." : "Good evening.";
double sum = num1 + num2; [Link](result);
return sum;
} Java For Loop Real-Life Examples
static double exponent(double base, 1. To demonstrate a practical example
double exp){ of the for loop, let's create a program
double result = [Link](base, that counts to 100 by tens:
exp); for (int i = 0; i <= 100; i += 10) {
return result; [Link](i);
} }
} [Link] this example, we create a program
//Output: that only print even values between 0
Hello World and 10:
sum of two integers: 11 for (int i = 0; i <= 10; i = i + 2) {
sum of two double: 12.7 [Link](i);
The exponent is 32.0 }
[Link] in this example, we create a
Java Conditions and If Statements program that prints the multiplication
Less than: a < b table for a specified number:
Less than or equal to: a <= b int number = 2;
Greater than: a > b // Print the multiplication table for the
Greater than or equal to: a >= b number 2
Equal to a == b for (int i = 1; i <= 10; i++) {
Not Equal to: a != b [Link](number + " x " + i
+ " = " + (number * i));
Java has the following conditional }
statements: Loop Through a Multi-Dimensional Array
Use if to specify a block of code to be You can also use a for loop inside
executed, if a specified condition is another for loop to get the elements of
true a two-dimensional array (we still have
Use else to specify a block of code to to point to the two indexes):
be executed, if the same condition is int[][] myNumbers = { {1, 2, 3, 4}, {5,
false 6, 7} };
Use else if to specify a new condition for (int i = 0; i < [Link]; +
to test, if the first condition is false +i) {
Use switch to specify many alternative for (int j = 0; j < myNumbers[i].length;
blocks of code to be executed ++j) {
[Link](myNumbers[i][j]);
if (20 > 18) { }
[Link]("20 is greater than }
18");
Or you could just use a for-each loop, [Link](fname + " is " +
which is considered easier to read and age);
write: }
int[][] myNumbers = { {1, 2, 3, 4}, {5, public static void main(String[] args) {
6, 7} }; myMethod("Liam", 5);
for (int[] row : myNumbers) { myMethod("Jenny", 8);
for (int i : row) { myMethod("Anja", 31);
[Link](i); }
} }
} //Outputs"1 2 3 4 5 6 7" (vertically) // Liam is 5
// Jenny is 8
Lowest Age // Anja is 31
public class Main {
public static void main(String[] args) { Java HashMap
// An array storing different ages import [Link];
int ages[] = {20, 22, 18, 35, 48, 26,
87, 70}; public class Main
float avg, sum = 0; {
// Get the length of the array public static void main(String[] args)
int length = [Link]; {
// Create a 'lowest age' variable and HashMap<Integer, String> names
assign the first array element of ages to = new HashMap<Integer, String>();
it
int lowestAge = ages[0]; [Link](1, "John Wick");
// Loop through the elements of the [Link](2, "Jose Maria");
ages array to find the lowest age [Link](3, "Charles-kun");
for (int age : ages) {
// Check if the current age is smaller for (int name : [Link]()){
than the current 'lowest age' [Link]([Link](n
if (lowestAge > age) { ame));
// If the smaller age is found, }
update 'lowest age' with that element }
lowestAge = age; }
}
}
// Output the value of the lowest age
[Link]("The lowest age in
the array is: " + lowestAge);
}
}

Loop through Array


String[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
for (String i : cars) {
[Link](i);
}

Parameters and Arguments - Multiple


Parameters
public class Main {
static void myMethod(String fname, int
age) {

You might also like