[Beginner] Java method / class / external library [Note 23]

Java

Method

It is basically defined in the class. It's like a part that summarizes the processing. [Example]

public static void hello();{
 System.out.println("Hello World");
}

Remember once as a fixed phrase up to public ... void.

argument

Additional information given to the method.

In () after hello earlier You can pass arguments such as "(String name)".

When passing multiple arguments, write "(String name, int price)" ... The first is called the first argument and the second is called the second argument.

Return value

It is used when the caller of the method wants to use the result. By the way, the void of "public static void .." that I used so far is Remember, it means no return value. [Example]

....

public static void main(String[]args){
 String name=fullName("Yamada","Taro") /*← Be careful not to forget the full Name here!*/
 printData(name,27);
}

public static void printData(String name,int age){
 System.out.println("my name is"+name);
 System.out.println("My age is"+age);
}

public static String fullName(String firstName,String lastName){
 return firstName+lastName; 
}

class

Until now, I made many methods in parallel with the main method Use classes because there are too many parallels and it's hard to see. Therefore, it is necessary to call the methods of other classes. [Example]

/*Main class*/

class Main{
 public static void main(String[]args){
  Person.hello();
 }
}

/*Person class*/

class Person{
 public static void hello(){
  System.out.println("Hello");
 }
}

Main class → execution class Person class → logic (logic class) It is easy to clarify the role when separated from.

External library

It is possible to use classes created by other people using import. For example, a major example is a class called Math that has mathematical methods. To load it, in class ↑ You need to write "import java.lang.Math;".

[Example] How to write when you want to return the larger of the two arguments

import java.lang.Math;
class Main{
 public static void main(String[]args){
  int max=Math.max(3,8);
  System.out.println("The maximum value is"+max+"is.");
 }
}

[Example] How to write when rounding off after the decimal point of the argument

/*...Omitted...Output part only*/
("BMI"+Math.round(bmi)+"is.");

Scanner It is possible to receive the character string entered in the console.

import java.util.Scanner;
class Main{
 public static void main (String[]args){
  Scanner scanner=new Scanner(System.in);
  /*↑ Scanner initialization*/
  System.out.print("name:");
  /*At this point, execution is temporarily stopped and it becomes a state of waiting for input.*/
  String name=scanner.next();
  /*The scanner here is lowercase! ".Don't forget! Call the character string entered in ↑*/
  System.out.println("Name is"+name+"is");

other An integer with scanner.nextInt () ;. You can receive decimals with scanner.nextDouble () ;.

Looking back

When dividing methods and writing the return value by yourself If you don't write them in order, they will get messed up. You have to understand each one and proceed. There are many parts that I'm not used to yet, so I switched to Progate's dojo mode. I will take some time to challenge myself.

Recommended Posts

[Beginner] Java method / class / external library [Note 23]
Java programming (class method)
[Java SE 11 Silver] Arrays class method summary [Java beginner]
[Beginner] Java Object Oriented / Instance Field / Instance Method / Overload [Note 24]
[Java] Object-oriented syntax --class method / argument
[Java beginner] == operator and equals method
Java beginner design pattern (Factory Method pattern)
Java method
java (method)
java beginner 4
java beginner 3
java beginner
Java method
Class method
[Java] method
[Java] method
How to use Java Scanner class (Note)
[Java] Instance method, instance field, class method, class field, constructor summary
Java class methods
[Java] Class inheritance
Java HashMap class
Java method call from RPG (method call in own class)
[Beginner] Points to be aware of after Java exercises / Inheritance / Abstract method [Note 26]
java (abstract class)
Java8 method reference
[Java] Nested class
Java starting from beginner, class declaration / object generation
Java anonymous class
[Java] forEach method
Java Beginner Exercises
About Java class
Use Java external library for the time being
[java] abstract class
java8 method reference
[Java] Object class
[Java beginner] println method without collection type specification
Java local class
[Java] Random method
[Java] split method
Java Exercise "Beginner"
[Java] How to use compareTo method of Date class
[Beginner] Java variable / logical operator / extended for statement [Note 22]
Java coverage measurement library
Java abstract modifier [Note]
[Java] Internal Iterator Note
About class division (Java)
JAVA DB connection method
About Java StringBuilder class
JAVA object mapping library
Java learning 2 (learning calculation method)
[Java] About Singleton Class
Java learning memo (method)
About Java method binding
[Java ~ Method ~] Study memo (5)
About method splitting (Java)
Java JUnit brief note
Studying Java 8 (see method)
Java inner class review
[java] Java SE 8 Silver Note
Java class type field
[Note] Java: String search