W 개발 일지

Java 정리 3 본문

JAVA

Java 정리 3

waVwe 2021. 1. 18. 21:36
반응형

 

Using methods : a method is a section of code to which we assign a specific task.

ex )

class Main {
    public static void main(String[] args) {
        hello(); //Hello method is called
    }
 
    public static void hello() {
        System.out.println("Hello World");
    }
}
cs

If you define a method outside of a class, there will be an error.

 

To call a method, just write methodName( )

 

An argument is like an additional information that you give to the method.

To do so, we need to specify a variable or rather, what is more commonly known as a parameter inside the ( ) of public static avoid methodName( )

ex) public static void hello ( String name )

String - dataType      name - variable for receiving an argument

 

To pass an argument to a method, call the method like this

: methodName( argument )

 

Methods with multiple arguments.

ex) public static void printPrice ( String item, int price ) {

        System.out.println( item + "is" + price + "dollars" );

    }

When calling a method, you have to pass the arguments in the same order and same data type as the parameter.

 

Return values : a return value allows us to use the result of a method where it was called.

Methods can send a return value back to the caller with return.

When there is a return value, you need to set its return type.

To do this, change the void part of public static void.

ex) public static int add ( int a, int b ) {

        return a + b;    -> the dataType should match the return value    a+b is integer

    }

void means no return value.

If the parameters of two identically named methods differ in number of parameters or data type, they can have the same name.

 

Defining multiple methods with same same is called Overloading.

Returning boolean value - define the method as a boolean.

Using multiple method.

ex )

class Main {
    public static void main(String[] args) {
        Person.hello()l //call the hello method of the person class
    }
 
}
 
class Person {
    public static void hello() {
        System.out.println("Hello !");
    }
}
cs

 

Defining class : class ClassName, class name should be capitalized. file name should be the same as the class name.

we can give execution role to the main class and the logic role to the person class.

If the executed class does not have a main method, it can not be executed by itself and needs to be called from another class.

 

External libraries : use import to load external libraries.

ex) import java.lang.Math;

 

Console input and Scanner : using a library called scanner to receive the input

To import scanner, write : import java.util.Scanner;

we must initialize a new Scanner instance and assign it to a variable.

Then we can use scanner.next( ) to get String inputs from the console.

ex )

import java.util.Scanner;
 
class Main {
    public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in); //make a new scanner instance
 
       String name = scanner.next(); //read a string
    }
 
}
cs

 

Receiving numerical inputs

nextInt( ) : 정수    scanner.nextInt( )

nextDouble( ) : 실수     scanner.Double( )

 

 

 

출처 : progate.com/dashboard

 

Progate | Learn to code, learn to be creative.

Progate is an online platform to learn programming. Learn to build your own apps and services.

progate.com

 

반응형

'JAVA' 카테고리의 다른 글

Java 정리 4  (0) 2021.01.19
JAVA 정리 2  (0) 2021.01.15
Java 정리 1  (0) 2021.01.14