A quick review of Java learned in class part3

Introduction

Let's see after reading parts 1 and 2. Suku. From this time, I will touch on difficult-to-understand parts such as object-oriented programming and classes, so let's get enthusiastic **

Separate files

Java uses object-oriented programming, which is a barrier for beginners to understand. From the outline, the idea is "all things in this world are objects, right?" To give a concrete example, ** "food" is an abstract concept and there are many kinds of things **, but ** "curry rice" generally exists as a fixed thing **. They are just called "objects".

Then how do you write

To describe object orientation, first describe how to handle multiple files in Java. This is because I want to roughly divide what is described ** for each file. The following is a program that calculates the numbers read by the keyboard (standard input), and the files are separated and only the calculation process is saved in a separate file.

java/Main.java


//A file that describes rough processing
import java.io.*;

public class Main
{
    public static void main(String[] args)throws IOException //Exception handling
    {
        BufferedReader br = //You don't have to break
        new BufferedReader(new InputStreamReader(System.in));

        int num1, num2; //Variables to which numbers will be assigned later
        int ans; //Variable to assign the answer

        System.out.println("Please enter the two numbers you want to add");

        System.out.print("The first one>"); //println breaks, but print does not
        String str1 = br.readLine();
        num1 = Integer.parseInt(str1);

        System.out.print("Second>");
        String str2 = br.readLine();
        num2 = Integer.parseInt(str2);

        System.out.println("mathematics.Java call\n"); // \n is a line break code
        ans = math(num1, num2); //Leave the calculation to another file and substitute
        System.out.println("Calculation result>" + ans);
        br.close(); //Declaration of termination of br
    }
}

From here, processing with another file

java/mathematics.java


class mathematics
{
    int math(int num1, int num2) //See below for details. Defining a math function
    {
        int ans;
        ans = num1 + num2;
        return ans; //return is the statement to send to the original file
        //Collect these three lines and "return"(num1 + num2);"Is fine.()It may not be necessary, but it is easy to understand, so describe it
    }
}

When you do this, you should see the result of calculating the two numbers. This time, when separating the files, I didn't write the syntax "public static void main (String [] args)" in mathematicals.java, because it represents a ** file dedicated to execution **. Main.java is specified because it is a file to be executed.

Then, the calculation is passed to the file, but it can be executed by calling the part "int math (int num1, int num2)" written in another file in the Main file. By writing the type and name of the type you want to call (different names are possible, not recommended), such as "int num" in a separate file, you can call it in the math function. This is called ** argument **

And "return" at the end of the math function is the process of returning the answer to the original Main function and moving the process to Main. This value is called ** return value **. The first int of "int math (int ...)" written in mathematics.java means ** return value to int **.

Look back again and look at the two files. I think the syntax is more "visible" than when you first saw it.

Summary here

--Play Java with multiple files --What is an argument? --What is return? --What if you want to specify an int type return value?

Object-oriented programming

That's the main subject. An object is the curry I mentioned earlier. Even udon and tea are all objects. Not limited to Java, ** objects are objects that allow you to create free types and enter free values in the same way as functions, which is very convenient because you can create a wide range of processes. First from the syntax. From this point on, there is basically more than one Java file.

java/Car.java


public class Car
{
    String carName; //Vehicle type
    String companyName; //company name
    double weight; //weight
    int number; //number

    Car(String carName, String companyName, double weight, int number) //There are four arguments
    {
        this.carName = carName; //More on this later.
        this.companyName = companyName;
        this.weight = weight;
        this.number = number;
    }
}

So far, a java file that creates a "car" type. Next, create a Main file that can be executed.

java/Main.java


public class Main
{
    public static void main(String[] args)
    {
        System.out.println("Now call the "car" type to create the NISSAN Leaf");

        String carName = "Leaf";
        String companyName = "NISSAN";
        double weight = 1600.0;
        int number = 1234;

        Car car1 = new Car(carName, companyName, weight, number); 
                //Car in Car File(4 arguments)

        System.out.println(car1.Name + "Called"); 
                //After the variable name, ".Write the item you want to call with ""
    }
}

This time, I prepared a syntax to confirm that the Nissan Leaf was created and called. Let's look at each one.

I will explain from the syntax written in Car.java. The item you want to prepare is described after class Car. The important thing is after this, the part that describes Car () describes what is called "this". It has a ** reference to itself ** and is used to distinguish whether CarName is an argument or a variable prepared in Car.java. In this case, ** this is attached to the variable ** prepared in Car.java. ** The argument disappears without being saved when the processing of the function is completed **, so it is saved in the function.

Let's take a look inside the Main function. This time, a new syntax "new Car (omitted)" has appeared. This is the "object" that I have repeatedly mentioned. Explaining this time, the syntax is ** "The name and number of the Car type car1 is decided by Car (omitted)!" **. And ** the moment it is called, the assignment is done in Car.java, the process is saved and it returns to the Main function **.

And there is a pitfall here: "If you don't ** write in order **, it will be assigned to different variables." For example, the moment you write "new Car (companyName, carName, ...)" in Main, the company name and car model are swapped. It must be described in the same order in Car.java. Furthermore, ** at this stage, "only cars with four arguments can be created" **. For example, you can't say, "I don't care about the weight of the car! I won't write it!" The only way to avoid this is to ** write a syntax with three additional arguments **.

... but it's annoying, isn't it? I purposely write the contents of the whole street.

If it is only the contents, it can be skipped to some extent. Banzai It's easy to do. See the syntax below.

java/Car.java


//abridgement

//The syntax I wrote earlier
    Car(String carName, String companyName, double weight, int number) //There are four arguments
    {
        this.carName = carName; //More on this later.
        this.companyName = companyName;
        this.weight = weight;
        this.number = number;
    }

    Car(String carName)//Syntax to decide only the car model for the time being
    {
        this(carName, "undefined", 0.0, 0); //Pass processing to four-argument syntax
    }

In this way, even if there is only one argument of Car, you can enjoy it by calling the part of 4 arguments inside (and reduce mistakes) A ** "type-determining function" like this one is called a "constructor" **, and calling one of the constructors with two or more combinations is called an "overload" **. ..

Summary here

--What is object-oriented? --Free type, free processing --What is this? --Constructor

Next time we'll cover methods, interfaces, and inheritance. It's really difficult

Recommended Posts

A quick review of Java learned in class part4
A quick review of Java learned in class part3
A quick review of Java learned in class part2
A quick review of Java learned in class
Creating a matrix class in Java Part 1
A quick explanation of the five types of static in Java
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 10)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 7)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 3)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 9)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 6)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 4)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 5)
Handle business logic for a set of Entity in Java class
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 2)
What is a class in Java language (2 /?)
Creating a matrix class in Java Part 2-About matrices (linear algebra)-
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 11)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 12)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 8)
GetInstance () from a @Singleton class in Groovy from Java
Measure the size of a folder in Java
What I learned in Java (Part 2) What are variables?
NIO.2 review of java
Review of java Shilber
Java inner class review
NIO review of java
Cause of is not visible when calling a method of another class in java
Write a class that can be ordered in Java
Browse class objects in Kotlin (instead of Java class name.class)
What I learned in Java (Part 3) Instruction execution statement
What I learned when building a server in Java
Use of Abstract Class and Interface properly in Java
Summarize the additional elements of the Optional class in Java 9
[Java] Comparator of Collection class
Find a subset in Java
Summary of Java Math class
Implementation of gzip in java
Implementation of tri-tree in Java
Activate Excel file A1 cell of each sheet in Java
[MQTT / Java] Implemented a class that does MQTT Pub / Sub in Java
I tried to make a client of RESAS-API in Java
Let's create a TODO application in Java 4 Implementation of posting function
Various methods of Java String class
What I have learned in Java (Part 1) Java development flow and overview
3 Implement a simple interpreter in Java
I created a PDF in Java.
Let's create a TODO application in Java 6 Implementation of search function
[Note] What I learned in half a year from inexperienced (Java) (1)
Let's create a TODO application in Java 8 Implementation of editing function
Sort a List of Java objects
Basic usage of java Optional Part 1
StringBuffer and StringBuilder Class in Java
Mechanism and characteristics of Collection implementation class often used in Java
A simple sample callback in Java
List of members added in Java 9
Cast an array of Strings to a List of Integers in Java
[Note] What I learned in half a year from inexperienced (Java) (3)
Examine the list of timezone IDs available in the Java ZoneId class
Get the public URL of a private Flickr file in Java
A brief description of JAVA dependencies