Java to be involved from today

Summary of what you need to study Java

Eclipse shortcut keys

■ Completion (Ctrl + Space) (sysout) System.out.println(); (syso) System.out.println(); (main) public static void main(String[] args) {}

■ Declare a local variable (Ctrl + 2 → L) new ArrayList(); → ArrayList arrayList = new ArrayList();

■ Other shortcuts Source code format (Ctrl + Shift + F) Comment out (Ctrl + /) Quick fix (Ctrl + 1) Move 1 line, copy 1 line (Alt + ↓ ↑, Ctrl + Alt + ↓ ↑) Delete one line (Ctrl + D) Method extraction (Alt + Shift + M) Open call hierarchy (Ctrl + Alt + H) Open Declaration (Ctrl + Click) Open type (Ctrl + Shift + T)

How to write a program

A refreshing summary for myself after reading an introduction to Java

▼ Class block + main block

public class Main {
	public static void main(String[] args) {


Description in the main block

System.out.println("Double quotes (\")");"double quotes(") ”Is displayed

int age;//Variable declaration (prepare a box called age)
age = 20;//Variable age assignment 20
int age = 20;//Initialize variable age with 20
age = 32;//Reassign to variable age
final double Pi = 3.14;//Declare pi as a constant

//: Data type (integer)
byte glasses = 2;//Number of glasses you have
short age = 20;//age
int salary = 180000;//Salary amount
long worldPeople = 6900000000L;//World population
//Data type (decimal)
float weight = 56;//body weight
double pi = 3.14;//Pi
//Data type (true / false value)
boolean isError = true;//true or false
//Data type (character, string)
char initial = F;//1 initial letter
String name = Haru;//name

int m = Math.max(①,②);//Compare the numbers and substitute the larger one
int n = Interger.parseInt(①);Convert strings to numbers
int r = new java.util.Random().nextInt(①);//Generate random numbers (up to ①)
String s = new java.util.Scanner(;Enter characters from the keyboard
int i = new java.util.Scanner(;Integer input from keyboard


if (tenki == true) {
	System.out.println("It's sunny");
}else {
	System.out.println("It's rain");

int lucky = new java.util.Random().nextInt(3)+1;
switch (lucky) {
case 1:
	System.out.println("I'm Daikichi");
case 2:
	System.out.println("I'm good");
case 3:
	System.out.println("It ’s bad.");

//Break statement for iterative processing
//Suspend only this time and continue to the next week

//Logical operator&&(And),||(Or)


for (int i = 0; i < 10; i++) {

while (i < 100) {

do {
	if (i % 3 == 0) {
} while (i < 100);


int[] score;//Prepare an array variable score to which int type elements can be assigned
score = new int[5];Create 5 int type elements and assign them to score

score.length//Get the number of elements in an array

int[] scores1 = new int[] {1,2,3};//Abbreviation 1
int[] scores2 = {1,2,3};//Abbreviation 2

for (Element type variable name:Array variable name){//Extended for statement

int[] moneyList = {1,2,3};
for(int i=0; i < moneyList.length; i++) {
for(int m : moneyList) {


//Method definition
public static return type method name(Argument list) {
Specific action to be taken when the method is called

public static void hello() {//hello method, void is used when there is no return value
hello();//Call the hello method

public static void main(String[] args) {
public static void introduceOneself() {
	String name = "Haru";
	int age = 6;
	double height = 110.5;
	char zodiac = 'Horse';

//Example (overload)
public static void main(String[] args) {
	String address = "[email protected]";
	String text = "Email body";
public static void email(String address, String text) {
	System.out.println(address + "I sent the following email to");
	System.out.println("Subject: Untitled");
	System.out.println("Text:" + text);
public static void email(String title, String address, String text) {
	System.out.println(address + "I sent the following email to");
	System.out.println("subject:" + title);
	System.out.println("Text:" + text);

//Example (triangle area)
public static void main(String[] args) {
	double triangleArea = calcTriangleArea(10.0,5.0);
public static double calcTriangleArea(double bottom, double height) {
	double area = bottom * height / 2;
	return area;

▼ Reference: How to use the method

Development using multiple classes

public class Calc {
	public static void main(String[] args) {
		int a = 10; int b = 2;
		int total = CalcLogic.tasu(a, b);
		int delta = CalcLogic.hiku(a, b);
		System.out.println("When added" + total + ", Pull" + delta);

public class CalcLogic {
	public static void main(String[] args) {
		public static int tasu(int a, int b) {
			return (a + b);
		public static int hiku(int a, int b) {
			return (a - b);

Instances and classes


public class Main {
	public static void main(String[] args) {
		Cleric c = new Cleric(); = "clergyman";

import java.util.Random;

public class Cleric {
	String name;
	int hp = 50;
	final int MAX_HP = 50;
	int mp = 10;
	final int MAX_MP = 10;

	public void SelfAid() {
		System.out.println( + "Chanted self-aid");
		this.hp = this.MAX_HP; -= 5;
		System.out.println("HP recovered to maximum");

	public int pray(int sec) {
		System.out.println( + "Is" + sec + "I prayed to heaven for a second!");

		int recover = new Random().nextInt(3) + sec;

		int recoverActual = Math.min(this.MAX_MP -, recover); += recoverActual;
		System.out.println("MP recovered" + recoverActual);
		return recoverActual;

Various class mechanisms

Constructor conditions -Method name is exactly equal to class name -The return value is not described in the method declaration (void is also useless)

public class class name{
name of the class() {
Process to be executed automatically

Special case for constructor A "no arguments, no processing" constructor is automatically added at compile time only if no constructor is defined in the class.

public class Thief {
	String name;
	int hp;
	int mp;

	//new Thief("Haruta", 40, 5)Description when instantiating with
	public Thief(String name, int hp, int mp) { = name;
		this.hp = hp; = mp;
	//new Thief("Haruta", 40)Description when instantiating with (MP is initialized with 5)
	public Thief(String name, int hp) {
		this(name, hp, 5);
	//new Thief("Haruta")Description when instantiating with (HP is 40,MP is initialized with 5)
	public Thief(String name) {
		this(name, 40);

	//new Thief()Make it impossible to instantiate (no unnamed Thief exists)

A program that uses the above Thief class

public class Main {
	public static void heal(int hp) {
		hp += 10;
	public static void heal(Thief thief) {
		thief.hp += 10;
	public static void main(String[] args) {
		int baseHp  = 25;
		Thief t = new Thief("Haruta", baseHp);
		System.out.println(baseHp + ":" + t.hp);
		System.out.println(baseHp + ":" + t.hp);

	//Execution result
//25:25 (Because if the argument is an int type, the value of the variable baseHp is copied to the argument hp
//25:35 (If the argument is a class type, the address indicated by the variable t is copied to the argument thief by reference..hp and thief.Because hp will point to the same location in memory)


//Create a class of mushroom monsters
public class Matango {
	int hp = 50;
	char suffix;
	public Matango(char suffix) {
		this.suffix = suffix;
	public void attack(Hero h) {
		System.out.println("Mushroom attack" + this.suffix);
		System.out.println("10 damage");
		h.hp -= 10;

public class PoisonMatango extends Matango {
	int poisonCount = 5;
	public PoisonMatango(char suffix) {
	public void attack(Hero h) {
		if (this.poisonCount > 0) {
			System.out.println("Further scattered spores");
			int dmg = h.hp / 5;
			h.hp -= dmg;
			System.out.println(dmg + "Point damage");

public class Hero {
	String name;
	int hp = 150;
	public Hero(String name) { = name;
		System.out.println("Name is" + + ", Physical strength" + this.hp);

public class Main {
	public static void main(String[] args) {
		Hero h = new Hero("Hirota");
		Hero h2 = new Hero("Hirota 2");

		Matango m1 = new Matango('A');

		PoisonMatango pm1 = new PoisonMatango('A');

Recommended Posts

Java to be involved from today
Changes from Java 8 to Java 11
Sum from Java_1 to 100
From Java to Ruby !!
Minecraft BE server development from PHP to Java
Migration from Cobol to JAVA
New features from Java7 to Java8
Connect from Java to PostgreSQL
From Ineffective Java to Effective Java
From Java to VB.NET-Writing Contrast Memo-
Java, interface to start from beginner
The road from JavaScript to Java
[Java] Conversion from array to List
Convert from java UTC time to JST time
Connect from Java to MySQL using Eclipse
From installing Eclipse to executing Java (PHP)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
[Java] Flow from source code to execution
Introduction to monitoring from Java Touching Prometheus
Precautions when migrating from VB6.0 to JAVA
Memo for migration from java to kotlin
Type conversion from java BigDecimal type to String type
[Java] Introduction to Java
Introduction to java
[Java] From two Lists to one array list
Upsert from Java SDK to Azure Cosmos DB
Run R from Java I want to run rJava
Connect to Aurora (MySQL) from a Java application
To become a programmer from a Java shop
CORBA seems to be removed in Java SE 11. .. ..
Migrate from Java to Server Side Kotlin + Spring-boot
How to get Class from Element in Java
There seems to be no else-if in java
[Java] How to switch from open jdk to oracle jdk
I want to write quickly from java to sqlite
[Java] char type can be cast to int type
Kotlin's reified function cannot be called from java.
Select * from Java SDK to Azure Cosmos DB
Launch Docker from Java to convert Office documents to PDF
Call Java from JRuby
Convert Java enum enums and JSON to and from Jackson
Tomcat cannot be started due to java version change
[Java] I want to calculate the difference from the date
How to jump from Eclipse Java to a SQL file
How to write Scala from the perspective of Java
Migrate from JUnit 4 to JUnit 5
[Java] How to extract the file name from the path
Eval Java source from Java
[Java] Connect to MySQL
Access API.AI from Java
Kotlin's improvements to Java
Java development for beginners to start from 1-Vol.1-eclipse setup
How to change from Oracle Java 8 to Adopt Open JDK 9
[Java] How to erase a specific character from a character string
Introduction to java command
Generate models from JSON to Swift, PHP, C #, JAVA
[Java] Platforms to choose from for Java development starting now (2020)
[Java] Things to be aware of when outputting FizzBuzz
Java to fly data from Android to ROS of Jetson Nano
To you who suffer from unexpected decimal points in Java