I created it as a cheat sheet that you can check the basic syntax of Java.
System.out.println("Hello World");
//When a character string and a numerical value are output together, the numerical value is automatically converted to a character string.
Ststem.out.println("I" + 20 + "I'm old");
//I'm 20 years old but output
//Comment out
Types such as strings and integers are called data types String: String Integer: int
//Declare in the order of data type variable names
String name;
//Assign a value to a variable
name = "Sato";
//Sato is output
System.out.println(name);
Variable definition and assignment can be done at the same time.
//Substitute Sato's value for variable name name in String data type
String name = "Sato";
//Sato is output
System.out.println(name);
Cannot be redefined
//Define variable number
int number = 1;
//Defining the variable number again will result in an error
int number = 1;
Reassignment is possible
//The value of number is 1
int number = 1;
//The value of number is 2(The value has been overwritten)
number = 2;
//Substitute 1 for number
int number = 1;
//Use the original value of 1 and reassign the number by adding 1 to number
number = number + 1;
//2 is output
System.out.println(number);
There is an abbreviation for this writing
number += 1; //number = number + 1;
number -= 1; //number = number - 1;
number *= 1; //number = number * 1;
number /= 1; //number = number / 1;
number %= 1; //number = number % 1;
You can forcibly specify the type when the desired type is different from the variable type. This is called a cast
.
int number1 = 1;
int number2 = 2;
System.out.println((double)number1 / number2);
//(double)Number 1 with is forced to 1.Converted to 0
if Basic form of if statement
if(Conditional expression 1){
Processing to be executed if conditional expression 1 is true
} else if(Conditional expression 2){
Processing to be executed if conditional expression 2 is true
} else {
Processing to be executed if all the above conditions are false
}
switch
Basic form of switch statement
break
is a description required to get out of the switch statement. Without this, all processing will be executed.
switch (processing){
case value 1:
If the return value of the process is 1, the process to be executed
break;
case value 2:
If the return value of the process is value 2, the process to be executed
break;
default:
What to do if it doesn't apply to any case
break;
}
while Basic form of while The process can be repeated as long as the condition is true. If the condition is not set to false someday, it will loop infinitely.
while(conditions){
Processing to be executed if the condition is true
}
Description example If the value of the variable number is 100 or less, iterative processing that outputs that number
int number = 1;
while(number <= 100){ //Returns true if number is 100 or less
System.out.println(number); //Output the value of number
number ++; //Add 1 to number
}
for Uninflected word for
for(Variable definition;conditions;Update value){
Processing to be executed if the condition is true
}
Description example If the value of the variable number is 100 or less, iterative processing that outputs that number
for(int number; number <= 100; number ++){
System.out.println(number);
}
By using ʻif statementsand
break` in the iterative process, the process can be terminated when a specific condition is met.
//Iterative processing that outputs number if the value of number is 100 or less
for(int number = 1; number <= 100; number ++){
//If the value of number is 5 in the conditional branch, the iterative process is forcibly terminated.
if(number == 5){
break; //Forced termination of iterative processing
}
}
You can skip the process for a specific value by using the ʻif statementand
continue` in the iterative process.
//Iterative processing that outputs number if the value of number is 100 or less
for(int number = 1; number <= 100; number ++){
//If the value of number is 5 in the conditional branch, the processing is skipped and the iterative processing is continued.
if(number == 5){
continue; //Start the next loop
}
System.out.println(number);
}
//In this process, it is not output only when the value is 5.
Values can be grouped together and assigned to a variable as an array.
Note that the data type has a []
to represent the array.
Also, the values are enclosed in {}
, and each value is separated by ,
.
Be careful not to confuse []
with {}
.
//Define numbers, which is an array of numbers
int[] numbers = {1, 2, 3, 4, 5};
The array is given a index number
.
This means the order of the values in the array, starting at 0
.
For example, the index number of the first element of the array is 0.
A specific value can be retrieved by specifying the index number of the array.
int[] numbers = {1, 2, 3, 4, 5};
//Specifies the first element of the array numbers
System.out.println(numbers[0]);
//1 is output
An extended for statement is provided as an iterative process for arrays Uninflected form of extended for statement
//Define numbers, an array of numbers
int[] number = {1, 2, 3, 4, 5};
//Iterate with for while assigning the elements of numbers to num one by one.
//Process for as many as the number of elements in the array
for(int num: numbers){
System.out.println(num);
}
//As a result, all the values in the array are output
A method is a collection of processes.
Be sure to define it in class.
By calling, the processing summarized by the method can be executed.
You can return the value to the caller by specifying the return value with return
.
Basic form of method definition
public static Return data type Method name(Data type argument name){
Process to be executed
return Value to be returned as a return value
}
In principle, the same method name cannot be defined. This is because you don't know which method to execute. The same method name can be defined if the argument types and numbers are different.
Class names start with a capital letter.
The file that defines the class is class name.java
.
When calling a method of another class, use class name.method name ()
.
A class is an object-oriented concept of an object blueprint.
A child made from the blueprint is called an instance.
class Main{
public static void main(String args[]){
//User class sayHello()Method call
User.sayHello()
}
}
Classes created by others are called libraries. The range of development can be expanded by utilizing the library.
Have the file read so that the library can be used. Some classes are already loaded and available to Java itself.
//Load the LocalDate class
import java.LocalDate
class Main{
//abridgement
}
Things in the real world have information and behavior. A class is defined to express it.
//Create an instance of the Person class with the name person
Person person = new Person();
The information that the instance has is instance field
//Instance field definition
public String name;
//Set a value in the instance field
person.name() = "Sato";
this
You can access the value of your own instance field using this
.
class Person {
//Set name in instance field
public String name;
//Define constructor
Person(String name){
//Substitute the name received as an argument for the name of the instance field
this.name = name;
}
}
You can define the process to be executed when the instance is created. Uninflected word of definition
class Person {
name of the class(Argument data type Argument){
processing
}
}
You can call another constructor using this ()
.
By using this, it is possible to standardize the overlapping parts in the constructor.
//You can jump out of another constructor and define it in this constructor.
this(argument, argument);
The behavior of an instance is called a instance method
.
The instance method is defined in the class, but it actually belongs to the instance.
I don't need static
public void hello(){
System.out.println("hello");
}
//Instance method call
person.hell();
Instance fields are values that belong to an instance, but you can also define class fields that belong to a class.
Note that unlike the instant field, static
is required.
Basic form of class field
class Person {
public static data type class field name;
}
Access to the class field is done with class name.class field name
.
name of the class.Class field name
You can hide things you don't want to rewrite.
To prevent access from outside the class, specify private
.
Specify public
to make it accessible from outside the class.
Defined as a method to safely retrieve the value specified in private.
It is common to use get field name
.
A method defined to rewrite the value specified in private from the outside.
It is common to use set field name
.
Recommended Posts