[Java ~ Classes and External Libraries ~] Study Memo (6)

Learning Java. I will make a note so that I can look back on it for review. It's almost my study memo. Please do not excessive expectations.

class

--Class definition

The definition of the class is " class class name ". The first letter of the class name should be uppercase . The file name must be " class name.java ".

.java:person.java


class Person {
  //Define the method here
}


--Call a method of another class

You can call methods of other classes by using class name.method name ().

In the example below There are two classes, Main class and Person class, and in the main method of Main class, Person.hello () and By doing so, the method of Person class is called.

.java:main.java


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

.java:person.java


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

--Supplement

Java executes classes, not files. Also, the main method is called at runtime, but to be precise, only the class that has the main method is executed. Classes that do not have a main method are used by calling them from other classes.

So even if you try to execute the Person class below Since there is no main method, an error will occur.

.java:person.java


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

External library

In Java, you can also use classes created by others. Such a class is called an external library and can be used by loading it into your own program.

Use import to load an external library into your program. To load a class (library), use " import java.lang. Class name " above the class definition.

"Java.lang" is a collection of basic classes that are often used in writing programs in the Java.lang package.

Using the library, try using the max method of a class called Math that has a mathematical method. The max method returns the larger of the two numbers passed as arguments.

Example


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);
  }
}

Output result


The maximum value is 8

Receiving input to the console

So far we have "output" the value to the console, but then we enter the value into the console and You can also use that value in your program.

An external library called Scanner is used to receive input to the console. Scanner loads with " import java.util.Scanner ".

--How to use Scanner (receive string)

① Initialize the Scanner Initialize the Scanner with new Scanner (System.in) ↓ (2) Put the initialized Scanner in a variable  Scanner scanner = new Scanner(System.in) ↓ ③ Enter the value on the console You can use scanner.next () to receive the string entered in the console.

How to use


//Loading the library
import java.util.Scanner;

class Main {
  public static void main (String[] args) {

    //Initialize the Scanner and put it in a variable
    Scanner scanner = new Scanner(System.in);

    // System.out.print is an instruction to output a value without line breaks
    System.out.print("name:");
    
    //Define variable name, receive string from console and assign
    String name = scanner.next();
    
    //Prints "Hello ◯◯-san"
    System.out.println("Hello"+name+"Mr.");
  }
}

--How to use Scanner (receive numerical value)

scanner.next () was receiving the string that was entered, Use the nextInt method to get an integer, You can receive a decimal by using the nextDouble method.

How to use


class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    
    System.out.print("age:");
    //Receive integer input
    int age = scanner.nextInt();
    
    System.out.print("height(m):");
    //Receive decimal input
    double height = scanner.nextDouble();
    
  }
}


Past posts

[Java ~ Variable definition, type conversion ~] Study memo [Java ~ False Value ~] Study Memo 2 [Java ~ Conditional branching / Iterative processing ~] Study memo 3 [Java ~ Array ~] Study Memo 4 [Java ~ Method ~] Study Memo 5

Recommended Posts

[Java ~ Classes and External Libraries ~] Study Memo (6)
[Java ~ Method ~] Study memo (5)
[Java ~ Array ~] Study memo 4
java (classes and instances)
Study Java # 2 (\ mark and operator)
[Java] Generics classes and generics methods
Java and Iterator Part 1 External Iterator
Java Silver Study Method Memo
[Java ~ Boolean value ~] Study memo (2)
Java study memo 2 with Progate
Java abstract methods and classes
[Study session memo] Java Day Tokyo 2017
Java generics (defines classes and methods)
Classes and instances Java for beginners
Java memo
Java libraries
[Java ~ Variable definition, type conversion ~] Study memo
Java study # 3 (type conversion and instruction execution)
Java programming (classes and instances, main methods)
JAVA learning history abstract classes and methods
[Java ~ Conditional branching / Iterative processing ~] Study memo (3)
java anything memo
Let's study Java
Java Silver memo
Effective Java 3rd Edition Chapter 4 Classes and Interfaces
java, maven memo
[Java] Personal summary of classes and methods (basic)
Study session memo: Kansai Java Engineers Association 8/5 --Selenium
Java SE 7 memo
Kantai Collection Java # 1 Classes and Objects [For Beginners]
java anything memo 2
[Java] Classification memo of compilation error and run-time error
Java and JavaScript
[Java] Study notes
How to get and study java SE8 Gold
XXE and Java
Java 8 study (repeatable)
Java study memorandum
Study Java Silver 1
Java specification memo
Java pattern memo
[In-house study session] Java basics-Lambda expression and Stream API- (2017/07/13)
Java classes and instances to understand in the figure
Equivalence comparison of Java wrapper classes and primitive types
Getters and setters (Java)
Java development environment memo
java classes, instances, objects
[Java] Thread and Runnable
HashMap and HashSet classes
Java true and false
Java Silver Study Day 1
java basic knowledge memo
Java Kuche Day memo
About classes and instances
[Java] String comparison and && and ||
[Java] About anonymous classes
java se 8 programmer Ⅰ memo
Java paid private memo
Dot installation study memo 01
Java study # 1 (typical type)
Java learning memo (basic)