java beginner 4

Before reading this, there is java beginner's edition 3, so please have a look there ⬇️⬇︎ ^^

https://qiita.com/shinpachix/items/6f6f972e7980f4c59d3d

continue and break

continue --Go back to the beginning of the repeat and go to the next lap break --End the iteration and go to the next

int x = o;
while(true) {
//Return here when x is 1(println does not run)
x++;
if(x==1)continue;
System.out.println(x);
if(x==10)break;
}
//When you break, go here

Array

String[] a = new String[3];
a[0] = "Hello";
a[1] = "Good morning";
a[2] = "Goodbye";

for(int i = 0; i < 2; i++) {
System.out.pintln(a[i]);

One variable can be treated like a sequence of multiple variables The inside of [] is called a subscript. Start from 0. The number of subscripts is called a dimension 。 Variables can be used to specify subscripts, so it is convenient to combine with a for statement.

Multidimensional array

String[][] cell = new String[9][9];
a[0][2] = "Ayumu";
a[5][5] = "Rook";
a[3][3] = "king";

System.out.println(a[5][5]);
//Rook

The dimension can be increased by making the array variable an array. The two-dimensional array can be used to manage shogi and Othello pieces.

Evaluation of the right side

The AND operator is&&When&, OR operator||When|There is, but there is a slight difference in behavior.

(When the left side is false in AND or true in OR) Fast because you don't see the right side (use & and | when you want to intentionally execute the right side)

int a = 5;
if(true || a++ > 0) {
System.out.println(a);
}
//5 comes out

if(true | a++ > 0) {
System.out.println(a);
}
//6 comes out

Function

Functions in mathematics

f(x) = 3x+1

f(4) = 13

Performs a fixed operation on the input value.

Functions in the program

Functions in the program

public static int hoge(int x) {
return (int) Math.pow(x,2)+3*x+1;

System.out.println(hoge(4));

When a value (argument) is passed, processing is performed and the value (return value) is returned.

Executed when called

Some functions have no arguments in the program

Return value is returned by return (in case of java) Regarding public and static, this time ...

Methods and functions

Functions written in a class are called methods. (It's a very rough word, but)

The story of the class itself is coming again ... In java, there is a class and the program is written in it, so it is called a method.

How to write a method

Method definition

public static int hoge(int x) {
return (int)Math.pow(x,2)+3*x+1;
}

Return type method name (argument type argument name, argument type argument name)

If there are multiple arguments, separate them with commas.

If there is no argument, write nothing hoge ()

If there is no value to return, the return type will be void

The place to write is outside the main method, inside the class! (Main is also a method)

For now, let's add static (I will explain in the future)

How to call a method

Method call

hoge(3);
int n = hoge(4);

Call by method name (argument)

If there are multiple arguments, write them separated by commas. The type of the argument to be passed should be the same as the definition

It is also possible to use it for further calculation by assigning the return value to a variable.

Benefits of creating a method

You can do the same work together

Example ⬇︎ Put the laundry in ➡️ Move the laundry ➡︎ Dry the laundry

All you have to do is say "wash" all at once

Convenient when executing many times (easy when changing the behavior)

If the scope uses a variable only in the method, it does not affect other parts.

The range of influence is different inside and outside the method.

public static int hoge(int x) {
x* = 2;
return x;
}

public static void main(String[] args) {
int a = 3;
int x = 10;
System.out.println(hoge(a)); //6
System.out.println(a); //3(In the case of int, the calculation inside the method does not affect the outside)
System.out.println(a); //10(x in hoge and x in main are different)
}



Recommended Posts

java beginner 4
java beginner 3
java beginner
Java Beginner Exercises
Java Exercise "Beginner"
Java
Java
Progate Java (Beginner) Review & Summary
Java starting from beginner, override
Java, instance starting from beginner
Java starting from beginner, inheritance
[Beginner] Java basic "array" description
Solve AtCoder Beginner Contest 151 in java
Java learning (0)
Studying Java ―― 3
[Java] array
[Java] Annotation
[Java] Module
Java array
Java tips, tips
Java methods
Differences between "beginner" Java and Kotlin
Java method
java (constructor)
Java array
[Java] ArrayDeque
Solve AtCoder Beginner Contest 150 in java
java (override)
java (method)
Java Day 2018
java (array)
Java static
Java serialization
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
Solve AtCoder Beginner Contest 153 in java
[Java beginner] About abstraction and interface
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics