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 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");
}
}
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
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();
}
}
[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