[Java] Study notes

Introduction

reference

Contents

▼ Things that you might forget / make mistakes

▼ Naming convention

Basic

name of the class

Method name

Common

method that returns a boolean variable

Variable name

constant

Convenient symmetry word list

▼ How to use quotation marks properly

▼ Types of variables and how to define them

Data type

char a = 'a';
String s = "hello world!";

int i = 10;
long l = 10000000L;

double d = 234.222;
float f = 234.987F;

Reference type

Array

int[] array;
array = new int[3];

int[] array;
array = new int[] {100, 200, 300};
int[] array = {100, 200, 300};

▼ Check the length of the array

int i = array.length;

▼ Output to command line

Standard output

System.out.println();  //With line breaks
System.out.print();    //No line breaks

Error output

System.err.println();  //With line breaks
System.err.print();    //No line breaks

▼ Increment decrement operator

x++;
x--;

x += 1;
x -= 1;

▼ Type conversion

Number ⇒ character string

String s = String.valueOf(i);

Character string ⇒ numbers

int i = Integer.parseInt(s);

Integer ⇔ floating point

double d = (double)i;

int i = (int)d;

▼ Control statement

if

if (conditions){
     //processing
} else if (conditions){
     //processing
} else {
}

switch

switch(variable){
case value 1:
        //processing
        //Process when the value of the variable is "value 1".
        break;

case value 2:
case value 3:
        //processing
        //Process when the value of the variable is "value 2" or "value 3".
        break;

    default:
        //processing
        break;
}

▼ Ternary operator

conditions?Return value when positive:Return value in case of mistake;

Example:
String msg = i > 80 ? "good" : "so so...";

▼ Repeat statement (loop)

Common

while

while (conditions){
    //processing
}

do while

do {
    //processing
} while (conditions)

for

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

Extension for

String[] names = {"Fujiwara", "Sakai", "Daiwa"};

for (String name : names) {
    System.out.println(name);
}

▼ Modifier

stroke order

Target Order
field public protected private static final transient volatile
Method public protected private abstract static final synchronized native strictfp
class public protected abstract static final strictfp

Access modifier

Access modifier Description
private Only accessible from the same class
protected Accessable from current class and subclass
None Accessable from a class in the same package as the current class
public Accessable from anywhere

static modifier

final modifier

abstract modifier

▼ Mezzot

How to declare

Qualifier Return type method name(argument){
Method processing
}
public static void SayHello(){
    //processing
}

Main method

    public static void main(String[] args){
      
    }

Method overload

public class MyApp{

    public static void sayHi(String name){
        System.out.println("Hi! " +name);
    }

    public static void sayHi(){
        System.out.println("Hi! Nobody");
    }

    public static void main(String[] args){
        sayHi("Jones"); // Hi! Jones
        sayHi();        // Hi! Nobody
    }
}

Method override

public class User {

    public void hello() {
        System.out.println("Hello World!");
    }
}
public class AdminUser extends User {

    @Override  //Annotation
    public void hello() {
        System.out.println("Hello World override!");
    }
}

Abstract method

▼ Class

Declaration

Qualifier class class name{
  
}
class User {
  
}

Instantiation

User jones = new User();

Inheritance

class User {
    void sayHi(){
        System.out.println("hi!");
    }
}

class AdminUser extends User {
}


Abstract class

▼ Calling variables

Description Description
Variable name Calling local variables
this.Variable name Calling instance variables ⇒ At the time of declarationstaticVariables not attached
name of the class.Variable name Calling class variables ⇒ At the time of declarationstaticThe one I put on

▼ Constructor

Basic

//Access class name(){}
//You don't need a return type because you can't return a return value

public class User{
    public User(String name){
        //processing
    }
}

this()

class User{
    private String name;
    User(String name){
        this.name = name;
    }
    
    User() {
        this("no name");
    }
    
}

super()

public class User {
    protected String name;

    public User(String name){
        this.name = name;
    }
}
public class AdminUser extends User {

    public AdminUser(){
        super("AdminUser desu");                // User(String name){}call
        //Special processing only for child class here
    }
}

(Class) Initializer

class User {
    private static int count; //Class variables

    static {
        User.count = 0;
    }
}

(Instance) Initializer

class User {
    private static int count; //Class variables

     {
        System.out.println("Instant Initializer")
    }
}

▼ Package

Construction


/(root)
├── README.md
└── com
    └── dotinstall
        └── myapp
            ├── MyApp.java
            └── model
                ├── AdminUser.java
                └── User.java

Declaration

MyApp.java


package com.dotinstall.myapp.model;

import

MyApp.java


import com.dotinstall.myapp.model.User;

MyApp.java


import com.dotinstall.myapp.modem.*;

▼ interface

Declaration

interface Printable {
    double VERSION = 1.2;       //constant
    void print();               //Abstract method
    default void getInfo() {}   //default method
    static static_method() {}   //static method
}

How to use

class User implements Printable {
}

▼ Enumeration type (enum)

Sample code

enum Fruit {
    Banana(1, "Philippines"),
    Apple(2, "Aomori"),
    Orange(3, "Ehime");

    private int id;
    private String pref;

    private Fruit(int id, String pref) {
        this.id = id;
        this.pref = pref;
    }

    public int getId() {
        return this.id;
    }
    public String getPref() {
        return this.pref;
    }
}

valueOf()

    Fruit frt;
    frt = Fruit.Apple;

    if (frt == Fruit.valueOf("Apple")) {
        //processing
    } 

values()

for (Fruit frt : Fruit.values()) {
    System.out.println(frt.getPref());
}

▼ Exception handling

Own exception class definition

class MyException extends Exception {
    public MyException() {
        super ("error message");
    }
}
class MyException extends Exception {
    public MyException(String errorMessage) {
        super (errorMessage);
    }
}

Call your own exception class

if (b < 0) {
    throw new MyException();
}
if (b < 0) {
    throw new MyException("error message");
}

Error handling (try / catch / finally)

try {

    } catch (ArithmeticException e) { //Error class+Variable name
        //processing
    } catch (MyException e) {         //Error class+Variable name
        //processing
    } finally {
        //processing
}

▼ Wrapper Class

Omitted

▼ Generics

class MyData<T> {
    public void printIt(T x) {
        System.out.println(x);
    }
}

public class MyApp{


    public static void main (String[] args){

            MyData<Integer> i = new MyData<>();
            i.printIt(29);
    }
}

▼ Thread / Multi Thread

class MyRunnable implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 500; i++) {
            System.out.print('.');
        }
    }
}


public class MyApp{


    public static void main (String[] args){
        MyRunnable r = new MyRunnable();
        Thread t = new Thread(r);
        t.start();

        for (int i = 0; i < 500; i++) {
            System.out.print('+');
        }

    }
}

▼ Anonymous class

interface Foo {
    void sampleMethod();
}

public class MyApp{
    public static void main (String[] args){
        Foo unNamed = new Foo() {                    //Here, of the class that implements Foo
            @Override                                //Definition and instantiation
            public void sampleMethod(){              //It is done at the same time.
                System.out.println("print it!");     // (Foo is an interface.)
            }
        };

        unNamed.sampleMethod();
    }
}

▼ Lambda expression / functional interface

interface Foo {
    void sampleMethod(String text);          //Abstract method definition
}


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

        Foo rambda = (text) -> {             //Concrete method definition
            System.out.println(text);        //I'll do it here.
        };

        rambda.sampleMethod("do it !");
    }
}

▼ math / random

▼ ArrayList

import java.util.*;

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

        List<Integer> sales = new ArrayList<>();

        sales.add(10);
        sales.add(20);
        sales.add(30);

        for (Integer i = 0; i < sales.size(); i++) {
            System.out.println(sales.get(i));
        }

        for (Integer sale : sales) {
            System.out.println(sale);
        }
    }
}

▼ HashSet

import java.util.*;

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

        Set<Integer> sales = new HashSet<>();

        sales.add(10);
        sales.add(20);
        sales.add(30);

        for (Integer sale : sales) {
            System.out.println(sale);
        }
    }
}

▼ HashMap

  import java.util.*;
  
  public class MyApp{
      public static void main (String[] args){
  
          Map<String, Integer> students = new HashMap<>();
  
          students.put("uta", 80);
          students.put("jones", 60);
          students.put("tatsuya", 70);
  
          students.remove("jones");
  
          System.out.println(students.get("tatsuya"));
          System.out.println(students.size());
  
          for (Map.Entry<String, Integer> student : students.entrySet()) {
              System.out.println(student.getKey() + ':' + student.getValue());
          }
  
      }
  }

Recommended Posts

[Java] Study notes
java notes
Jigsaw study notes
Let's study Java
[Java] Array notes
Docker study notes
Java serialization notes
Java 8 study (repeatable)
Java study memorandum
Study Java Silver 1
[Java] Stream Collectors notes
Java formatted output [Notes]
Java Silver Study Day 1
[Java ~ Method ~] Study memo (5)
[Java] Control syntax notes
Ruby study notes (puts)
Java study # 1 (typical type)
[Java ~ Array ~] Study memo 4
My Study Note (Java)
Maven basic study notes
[Java] Basic method notes
Study Java # 2 (\ mark and operator)
Study java arrays, lists, maps
Try scraping using java [Notes]
Java Collections Framework Review Notes
[Java ~ Boolean value ~] Study memo (2)
Java study # 7 (branch syntax type)
Java
Java study memo 2 with Progate
Ruby study notes (variables & functions)
Java
Study Java with Progate Note 1
How to study Java Silver SE 8
[Java] Basic types and instruction notes
Summary of [Java silver study] package
[In-house study session] Java exception handling (2017/04/26)
[Study session memo] Java Day Tokyo 2017
Notes on signal control in Java
Internal class and lambda study notes
Java study # 4 (conditional branching / if statement)
Orcacla Java Bronze SE 7/8 Qualification Study
Notes on Android (java) thread processing
Java study # 5 (iteration and infinite loop)
Notes on Java path and Package
Study Deep Learning from scratch in Java.
Studying Java ―― 3
[Java] array
Summary of in-house newcomer study session [Java]
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)