What I learned in Java (Part 3) Instruction execution statement

Looking back on Java

Last time I posted about variables in Java. Please see that article if you like. What I learned in Java (Part 2) What are variables

This time I would like to write about the statement of instruction execution.

What is an instruction execution statement?

An instruction execution statement is an instruction statement prepared by Java from the beginning. As a basic form of instruction execution statement

Statement to call (argument)

It will be. * Information in parentheses is called an argument (hikisu) or parameter.

Display to console

As one of the concrete ones, "System.out.println ()" used in previous articles etc.

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

Execution result スクリーンショット 2020-11-09 111752.png This is a statement to display the arguments enclosed in parentheses on the screen. There is also a difference between "System.out.println ();" and "System.out.print ();".

public class Main {
	public static void main(String[] args) {
		//new line
		System.out.println("Nice to meet you");
		System.out.println("Hello");
		
		//No line breaks
		System.out.print("Nice to meet you");
		System.out.print("Hello");
	}
}

Execution result スクリーンショット 2020-11-09 115118.png It is also possible to use with variables

public class Main {
	public static void main(String[] args) {
		//Variable declaration, assignment
		int a = 1;
		String b = "Hello";
		
		//Display variables
		System.out.println(a);
		System.out.println(b);
	}
}

Execution result スクリーンショット 2020-11-09 115345.png

Comparison of two values

Instructions for comparing two values

int m = Math.max(a,b);

This is an instruction to compare "a" and "b" specified by the argument and assign the larger one to the variable "m".

public class Main {
	public static void main(String[] args) {
		//Variable declaration, assignment
		int a = 1;
		int b = 2;
		
		//Substitute the larger of a and b into the variable m
		int m = Math.max(a, b);
		
		//Show assigned value
		System.out.println(m);
	}
}

Execution result スクリーンショット 2020-11-09 120244.png

Convert strings to numbers

Converts a number entered in a String type variable to an integer

int seisu = 1;
String moziretu = "1";

The contents "1" of the two variables created above are completely different. Integers cannot be calculated with the character string "1" assigned to the String type. Therefore, the statement for converting a String type number to an int type (integer) is as follows.

int n = Integer.parseInt(moziretu);

Example

public class Main {
	public static void main(String[] args) {
		//Substitute "1" for each of int type and String type
		int seisu = 1;
		String moziretu = "1";
		//Display by adding int type and String type
		System.out.println(seisu + moziretu);
		
		//Convert String type to int type
		int n = Integer.parseInt(moziretu);
		//After conversion, calculate and display between int types
		System.out.println(seisu + n);
	}
}

Execution result スクリーンショット 2020-11-09 122428.png As you can see from the execution result, the first line is the state where the integer "1" and the character string "1" are lined up side by side, and the second line converts the character string "1" to an integer and the calculation result between the integers. Is displayed.

Instructions to create random numbers

Instructions to generate random numbers like dice

int r = new java.util.Random().nextInt(10);

In the above, a random number from 0 to 9 is assigned to the variable r. Example

public class Main {
	public static void main(String[] args) {
		//0 for int type r~Generate and substitute random numbers up to 9
		int r = new java.util.Random().nextInt(10);
		//Display the variable r
		System.out.println(r);
	}
}

Execution result (randomly generated numbers are assigned) スクリーンショット 2020-11-09 124155.png It is also possible to range.

public class Main {
	public static void main(String[] args) {
		//0 for int type r~Generate and substitute random numbers up to 9
		int r = new java.util.Random().nextInt(10) + 10;
		//Display the variable r
		System.out.println(r);
	}
}

Execution result (random numbers from 10 to 19 are assigned) スクリーンショット 2020-11-09 124458.png

Command to receive input from the keyboard

Accepts input of strings or integers from the keyboard

//Receive a string
String input1 = new java.util.Scanner(System.in).nextLine();
//Receive an integer
int input2 = new java.util.Scanner(System.in).nextInt();

Example

public class Main {
	public static void main(String[] args) {
		System.out.println("Please enter the product you purchased");
		String input1 = new java.util.Scanner(System.in).nextLine();
		
		System.out.println("Please enter the number of purchases");
		int input2 = new java.util.Scanner(System.in).nextInt();
		
		System.out.println(input1 + "To" + input2 + "It's an individual. Thank you for your purchase.");
	}
}

Execution result スクリーンショット 2020-11-09 130245.png I tried to summarize the basic instruction execution statements.

References

https://book.impress.co.jp/books/1113101090

Next time, I would like to write about conditional branching and repetition.

Recommended Posts

What I learned in Java (Part 3) Instruction execution statement
What I learned in Java (Part 2) What are variables?
What I learned in Java (Part 4) Conditional branching and repetition
What I have learned in Java (Part 1) Java development flow and overview
What I learned when building a server in Java
What I learned with Java Gold
What I learned with Java Silver
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
What i learned
[Note] What I learned in half a year from inexperienced (Java) (3)
What I learned from Java monetary calculation
Summary of what I learned in Spring Batch
What I learned ② ~ Mock ~
What I learned ① ~ DJUnit ~
Parallel execution in Java
A quick review of Java learned in class part4
A quick review of Java learned in class part3
I tried using an extended for statement in Java
[Rilas] What I learned in implementing the pagination function.
A quick review of Java learned in class part2
I tried to find out what changed in Java 9
What I researched about Java 8
What I researched about Java 6
I made roulette in Java.
What I researched about Java 9
What I researched about Java 7
External process execution in Java
I tried metaprogramming in Java
What I learned about Kotlin
What I researched about Java 5
I want to simplify the conditional if-else statement in Java
What I learned from studying Rails
[java] What I did when comparing Lists in my own class
I sent an email in Java
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)
I created a PDF in Java.
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 4)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 5)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 2)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 1)
[Java] What got caught in encapsulation
I made an annotation in Java.
I tried using JWT in Java
What I learned from doing Java work with Visual Studio Code
Creating lexical analysis in Java 8 (Part 2)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 11)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 12)
What I researched about Java learning
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 8)
Creating lexical analysis in Java 8 (Part 1)
Let's think about what declarative programming is in Java and Elm (Part 1)
Java review (2) (calculation, escape sequence, evaluation rule, type conversion, instruction execution statement)
[Java] I participated in ABC-188 of Atcorder.
What is a class in Java language (3 /?)
I tried using Elasticsearch API in Java
What I investigated in Wagby development Note 1
Take what you've learned about Java reflection
Java study # 3 (type conversion and instruction execution)