[day: 5] I summarized the basics of Java

I summarized the basics of Java

I created it as a cheat sheet that you can check the basic syntax of Java.

output

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

//Comment out

Data type

Types such as strings and integers are called data types String: String Integer: int

variable

Variable definition

//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 initialization

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);

Variable redefinition and reassignment

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;

Self-substitution

//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;

cast

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

Conditional branch

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;
}

Iterative processing

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);
}

Forced termination of iterative processing

By using ʻif statementsandbreak` 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
  }
}

Skip certain iterations

You can skip the process for a specific value by using the ʻif statementandcontinue` 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.

Array

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

Extension for

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

Method

Method definition

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
}

Method overload

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

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()
  }
}

Library

Classes created by others are called libraries. The range of development can be expanded by utilizing the library.

import

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
}

Object-orientation

Things in the real world have information and behavior. A class is defined to express it.

Instance generation

//Create an instance of the Person class with the name person
Person person = new Person();

instance

Instance field

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;
  }
}
constructor

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);

Instance method

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();

Class field

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

Encapsulation

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.

Getter

Defined as a method to safely retrieve the value specified in private. It is common to use get field name.

Setter

A method defined to rewrite the value specified in private from the outside. It is common to use set field name.

Recommended Posts

[day: 5] I summarized the basics of Java
I summarized the types and basics of Java exceptions
I tried to summarize the basics of kotlin and java
Now, I've summarized the basics of RecyclerView
[Java] I personally summarized the basic grammar.
Looking back on the basics of Java
I passed the Oracle Java Bronze, so I summarized the outline of the exam.
Muscle Java Basics Day 1
I want to output the day of the week
I understood the very basics of character input
I compared the characteristics of Java and .NET
I touched on the new features of Java 15
Try the free version of Progate [Java I]
Docker monitoring-explaining the basics of basics-
Basics of character operation (java)
I summarized the collection framework.
[For beginners] Quickly understand the basics of Java 8 Lambda
Understand the basics of docker
4th day of java learning
I summarized the display format of the JSON response of Rails
The basics of Swift's TableView
Summary of Java language basics
[Java] I implemented the combination.
Java is the 5th day
I studied the constructor (java)
How to derive the last day of the month in Java
[Java] I thought about the merits and uses of "interface"
[Java] Delete the elements of List
About the basics of Android development
[Java version] The story of serialization
I read the source of ArrayList I read
The basics of SpringBoot + MyBatis + MySQL
I read the source of Integer
I read the source of Long
I tried the Java framework "Quarkus"
I read the source of Short
I read the source of Byte
I read the source of String
The origin of Java lambda expressions
I tried using GoogleHttpClient of Java
Java Day 2018
[Java1.8 +] Get the date of the next x day of the week with LocalDate
I tried the input / output type of Java Lambda ~ Map edition ~
Java basics
Java basics
I tried to summarize the methods of Java String and StringBuilder
Java basics
[Java] I participated in ABC-188 of Atcorder.
Get the result of POST in Java
[Challenge CircleCI from 0] Learn the basics of CircleCI
Understand the basics of Android Audio Record
Check the contents of the Java certificate store
Examine the memory usage of Java elements
Memorandum of new graduate SES [Java basics]
Memo: [Java] Check the contents of the directory
I tried the new era in Java
Compare the elements of an array (Java)
I investigated the internal processing of Retrofit
What are the updated features of java 13
Easily measure the size of Java Objects
Output of the book "Introduction to Java"