Course Content
Java Basics
Java Basics
New Task Structure
Previously, you wrote code inside the main
method, but now the structure of the tasks will change slightly. You'll be writing code inside a separate method, which will then be called from the main
method. To help you understand how this works, we've prepared a small tutorial that will explain the process.
So, let's first break down what methods are, how they work, and where exactly you should write your solution.
What is a Method?
You can think of a program as a set of instructions or a plan of action. Each instruction is like a separate step. These steps in Java are called methods.
A method is like a small command that we define once and then can use whenever needed. A typical program structure looks like this:
Main
public class Main { public static void main(String[] args) { // Solution goes here } }
Where to Write Your Solution?
It's crucial to understand that you need to write your code inside a method. You cannot just write code anywhere in the class. Everything must be organized within methods; otherwise, the program won't know what to do.
Main
public class Main { public static void main(String[] args) { // Write your solution here } }
Everything you write inside the curly braces { }
of the main
method will be executed by the program.
It's important to remember: your code must always be inside a method. You cannot write code outside of methods; doing so will cause an error.
In some tasks, you will need to write your solution either in the main
method or in a different method created specifically for your code.
Main
public class Main { public static void main(String[] args) { // The main method calls the doSomething method doSomething(); } public static void doSomething() { // Your solution will go here } }
In this example, the main
method calls the doSomething
method, and your task is to implement the logic inside that method.
The main
method can be used to test your solution by calling other methods from it. So, to test how your solution works, you can call the method from main
and pass the necessary data to it.
Method Arguments
Sometimes, a method can take arguments — these are values passed to the method to help it complete its task. Arguments are like "things" the method gets as input, which it will then use within its block.
For example, if the task is to calculate the sum of two numbers, the numbers would be the arguments that the method gets to perform its work.
Example with One Argument
Here's how it looks when a method takes only one argument:
Main
public static void greet(String name) { // The method uses the name argument to perform its task }
In this example, the greet
method takes one argument — name
. The String
next to name
is the type of the argument.
The greet
method uses name
(which is an argument) inside it to perform its task (for example, it might display a greeting message).
Method Has Multiple Arguments
A method can accept multiple arguments. It's like putting several items into a box, and the method will use all of them to complete its task.
Main
public static void addNumbers(int a, int b) { // The method adds two numbers }
In this example, the addNumbers
method takes two arguments: a
and b
. These are the two numbers that the method will add together.
To get the method to run, you need to call it from another method. Since the main
method is automatically run when the program starts, you'll call the other methods from there.
For example, you have a task to implement the addNumbers
method, which takes two integer arguments, adds them together, and outputs the result.
Main
package com.example; public class Main { public static void main(String[] args) { // Call the `addNumbers` method and pass two numbers addNumbers(5, 3); } // Method that adds two numbers public static void addNumbers(int a, int b) { // This method will add the numbers `a` and `b` int result = a + b; System.out.println("Result: " + result); } }
To call a method with arguments, we just use its name (addNumbers
) and pass the arguments in parentheses (5, 3)
. The arguments are passed in the same order as they appear in the method. So in this case:
-
The first argument (5) will be assigned to the variable
a
; -
The second argument (3) will be assigned to the variable
b
.
The addNumbers
method takes two arguments, a
and b
, and these will be used inside the method to perform the required operations.
Understanding these concepts is crucial for successfully solving the tasks you'll encounter. By knowing how methods work, how to pass arguments, and how to structure your code, you'll be able to approach each task with confidence.
Thanks for your feedback!