A quick review of Java learned in class

Introduction

This is a review of what I learned in class, as well as a notebook-like post where I write down ** help and improvement measures for my classmates. You can't make a practical application using Java, or you can't get the knowledge like "This function behaves like this!" Like the amazing people. Not bad. Don't swallow 100% more.

--Recommended environment: Visual Studio Code `Because Eclipse is heavy. ``

It's a good idea to include "terminal" and "Language Support for Java (TM) by Red Hat" as extensions. Other than that, I like it.

First step

First of all, the customary Hello World!

java/hello.java


public class Hello
{
    public static void main(String[] args)
    {
        System.out.println("hello World!");
    }
}

At the beginning of Java, "public class Hello" is a required syntax. I thought it was magic. The important thing this time is "System.out.println (" Hello World! ");". You can use this ** println function to print characters to the console **. When handling a character string, it is necessary to enclose it in "" ". By the way, there is also a print function. As you can see by changing the syntax and re-executing, the difference is whether or not to start a new line after outputting Hello World !. Also, in Java ** you need to enter ";" at the end of the syntax **. If you do not enter it, you will not be able to convert the executable file and you will just get an error and exit, so hit it.

After entering the above syntax, open terminal (right-click to open it with VScode) and enter "javac Hello.java" on the terminal. Hit where the file is. When Hello.class is created, enter "java Hello". You don't have to hit .class. If you can say hello at the terminal safely, let's move on.

Summary here

--println function --print function

variable

Not limited to Java, when writing code, first write variables (declaration). The same thing comes out in almost every language.

java/Dec.java


public class Dec
{
    public static void main(String[] args)
    {
        int num; //Integer 4-byte variable
        num = 3; //Assign value
        short num2 = 15; //Integer type 2-byte variable(I'm putting values at the same time)

        boolean aaaa; //true or false
        double bbbb; //8-byte double precision floating point number
        float cccc; //4-byte double precision floating point number
        char str; //Double-byte character
    }
}

I still have it, but I'll omit it. I don't use it much in class ... Values can be assigned at the same time as the declaration, so there is no need to separate them.

Summary here

--Basic type declaration

Calculation / conversion between types

Here, a simple calculation, int type is converted to double type. First calculation

java/Math.java


public class Math
{
    public static void main(String[] args)
    {
        int num1 = 12;
        int num2 = 34;

        answer = num1 + num2; //The order of calculation in programming is reversed!
                                                   //Because the calculated result is substituted

        System.out.println(answer); //When outputting a variable """Do not add
    }
}

When executed, it naturally becomes 46. This time it was easy because it was a calculation between int types, but it cannot be executed if double types are mixed. In Java, ** different types cannot be calculated **. The only way to avoid this is to convert the int type to a double type.

java/Cast.java


public class Cast
{
    public static void main(String[] args)
    {
        int inum = 1;
        double dnum = 2.5;

        //The line below cannot be executed
        //answer = inum + dnum;

        answer = (double)inum + dnum; //Enter the type you want to convert in parentheses, calculate and execute!

        System.out.println(answer);
    }
}

The execution result is naturally 3.5. There are many 0s after the decimal point, but it is a specification. Be careful when assigning an int type to a small type such as a short type. Please note that it may be an unintended number.

Summary here

--Calculation between types --Type conversion

if statement / switch statement

From this point on, it becomes more like programming. The if statement is a statement that "if it isn't ...". Since you can set the conditions freely, the point is whether you can write the conditions according to your purpose.

java/If.java


public class If
{
    public static void main(String[] args)
    {
        int num = 4;

        if(num <= 5) //If num is 5 or less, execute the statement inside
        {
            System.out.println("The value of num is" + num + "So it was 5 or less.");
            //When outputting statement and type values at the same time+Connect with
        }
        else //You can write a sentence that executes other than the above with else
        {
            System.out.println("Was a number greater than 5");
        }
    }
}

While the if statement looks at the conditions in order from the top, the switch statement makes it easier to see by creating a list.

java/Switch.java


public class Switch
{
    public static void main(String[] args)
    {
        int num = 2;

        switch(num)
        {
            case 0:
                System.out.println("num is 0");
                break; //Syntax to end the processing of switch statements
            case 1:
            case 2: //I am taking the specification that it will be executed in order if I do not hit the break statement
            case 3:
                System.out.println("0 < num < 4");
                break;
            default:
                System.out.println("Greater than 3(suitable)"); //The default statement can describe how to execute a condition that does not apply to any of them.
        }
    }
}

There is only a "easy-to-read" difference between the switch statement and the if statement. Perhaps. If you want to write an if statement in a long way, the switch statement is better, and if there are only two conditions, you don't have to choose the switch statement. It would be reasonable to use the function that is easier to follow in your head.

Summary here

--if statement --switch statement

When it gets longer, it's hard to see and write. Part2 and so on ...

Recommended Posts

A quick review of Java learned in class
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 explanation of the five types of static in Java
What is a class in Java language (3 /?)
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Creating a matrix class in Java Part 1
Handle business logic for a set of Entity in Java class
GetInstance () from a @Singleton class in Groovy from Java
NIO.2 review of java
Measure the size of a folder in Java
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)
Write a class in Kotlin and call it in Java
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
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
Think of a Java update strategy
Various methods of Java String class
3 Implement a simple interpreter in Java
I created a PDF in Java.
Sort a List of Java objects
StringBuffer and StringBuilder Class in Java
A brief description of JAVA dependencies
Get stuck in a Java primer
List of types added in Java 9
Implementation of like function in Java
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 10)
Let's create a TODO application in Java 4 Implementation of posting function
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)
I can't create a Java class with a specific name in IntelliJ
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 5)
Let's create a TODO application in Java 6 Implementation of search function
[Note] What I learned in half a year from inexperienced (Java)
[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
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 2)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 1)
Mechanism and characteristics of Collection implementation class often used in Java
Cast an array of Strings to a List of Integers in Java
Creating a matrix class in Java Part 2-About matrices (linear algebra)-
Let's create a TODO application in Java 1 Brief explanation of MVC
[Java] Returns a Japanese name file in filename of HTTP header
Let's create a TODO application in Java 5 Switch the display of TODO
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 11)