A memorandum to reach the itchy place for Java Gold

2017/02/09 Added time and date

Target

Those who aim for javaGold Those who understand javaSilver

Overview

Focus on the parts that can be reached where it is itchy Inner class or anonymous class Since it also serves as a memorandum, please forgive comments and typographical errors in variable names (laugh) Will be updated from time to time static

static_.java


public class static_ {
    int x;
    static int y;

    //Accessing static instances from dynamic methods
    void doA(){
        x=1; //OK
        y=2; //OK
    }

    static void doB(){
//        x=1; //NG
        y=2; //OK
    }
}

final

final_.rjava


//final class
//Not inheritable
final class final_{

    //final field
    //Cannot be reassigned
    final int x = 0;

    //final method
    //Cannot be overridden
    final void doIt() {

        //final local variable
        //Cannot be reassigned
        final int y = 1;

        //Below, compilation error
        /*
        x = 10;
        y = 10;
    }
}
class temp extends finalMethod {

    @Override
    public void doIt(){

    }
    */
    }
}

abstract

abstract_.rjava


abstract class A{
    void X (){};
    abstract void y();
//    void Z(); //Be sure to add abstract
}
/*NG abstract class must be implemented firmly
public class B extends A{
}
*/

//OK
abstract class C extends A{
    void y(){
        System.out.println("###");
    }
}

Inner class

naibu.java


public class naibu {

    //Below, nested class

    //① static menba class
    static class X {}

    //② Member class
    private class Y{}

    void doIt(){
        //③ Local class
        class Z{}
    }

    //Calling an inner class
    public static void main(String args[]){

        Outer.Inner in = new Outer().new Inner();
        in.doIt();

        Outer.Inner.Inner2 in2 = new Outer().new Inner().new Inner2();
        in2.doIt();
    }
}

class Outer{

    public class Inner{

        public void doIt(){
            System.out.println("Outer->Inner");
        }

        public class Inner2{
            public void doIt(){
                System.out.println("Outer->Inner->Inner2");
            }
        }
    }
}

interface default / static

interface_.java


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

        //static method
        Bar bar = new Bar();
        Foo.doA();
//        Bar.doA();//NG implementation class → static
//        bar.doA();//NG implementation class → static

        //default method
        bar.doB();

    }
}

interface Foo {
    //Internally qualified with public static final
    int interfaceMenba =10;
    public static final int interfaceMenba2 = 10;

    //Ordinary abstract method
    //Internally qualified with public abstract
    void doZ();

    //static method
    static void doA() {
        System.out.println("static");
    }

    //default method
    default void doB() {
        System.out.println("default");
    }


}
class Bar implements Foo {
     public void doZ(){
        System.out.println("doZ");
    }
}

Anonymous class

anonymous.java


public interface anonymous {
    public void doIt();
}

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

        //Implemented at the same time as the declaration by the anonymous class(Not running)
        anonymous A = new anonymous() {
            public void doIt() {
                System.out.println("Declaring a class with an anonymous class");
            }
        };

        //Just run it for simplicity
        new anonymous(){
            public void doIt(){
                System.out.println("Execution by anonymous class");
            }
        }.doIt();
        //↑ This is the point
    }
}

Enum

rekkyo.java


public class rekkyo {
    //Nested enums
    public enum singou {
        GREEN, RED, YELOW
    }

    public static void main(String[] args) {
        //Enumerate all enums
        for (singou2 s : singou2.values()) {
            System.out.println(s.name() + "#");
        }

        //Enum declarations and field references
        Souldout so = Souldout.bro_hi;
        System.out.println(so.getV() + "#" + so);
    }
}

//Top-level enums
enum singou2 {
    GREEN, RED, YELOW;
    //Variables / methods can be placed in enumeration type
    private int a;
    public int b;

    public int add(int a, int b) {
        return a + b;
    }

}

enum Souldout {
    //Enum with constructor ↓
    diggy_mo(2), sinnosuke(3), bro_hi(5);
    private int nenrei;

    //Constructor must be private
//    public Souldout(){
//    }
    private Souldout() {

    }

    private Souldout(int v) {
        this.nenrei = v;
    }

    public int getV() {
        return nenrei * 100;
    }
}

//It is possible to implement an interface
interface temp {
    void doItIt();
}
enum singou3 implements temp {
    GREEN, RED, YELOW;

    public void doItIt() {
        System.out.println("####");
    }
}

//However, class inheritance is not possible
class temp2 {
    int a;
}
/*
enum singu4 extends temp2{
}
*/

Time / date

2017/02/06 update

DATE.java


import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;

import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.MONTHS;
import static java.time.temporal.ChronoUnit.WEEKS;

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

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //LocalDate / LocalDateTime
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //There are the following methods to create an instance
        LocalDate ld = LocalDate.now();
        ld = LocalDate.of(2016, Month.MARCH,10); //  Month.MARCH ⇔ 2
        ld = LocalDate.parse("2016-12-12");
        //ld = new LocalDate();                //NG

        LocalDateTime ldt = LocalDateTime.now();

        //isBefore method
        //isAfter method
        //System.out.println(ldt.isAfter(ld));  //NG LocalDate and LocalDateTime
        //System.out.println(ld.isAfter(ldt));  //NG LocalDateTime and LocalDate
        System.out.println(LocalDate.now().isAfter(ld)); //OK

        //ldt = LocalDateTime.from(ld);  //Time information is required to change from NG LocalDate to LocalDateTime!
        ld = LocalDate.from(ldt);       //OK LocalDateTime to LocalDate do not require time information

        System.out.println(ldt.plus(5,MONTHS)
                + "#" + ldt.minus(365,DAYS)
                + "#" + ldt.until(ldt.plus(10,WEEKS),WEEKS));

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //Temporal ←(Inheritance)― TemporalAccessor ←(Implementation)― ChronoUnit
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
        TemporalAccessor ta =ChronoUnit.DAYS.addTo(LocalDate.now(),10);
        Temporal t =ChronoUnit.DAYS.addTo(LocalDate.now(),10);

        //LocalDateTime and LocalDateTime are not compatible
        //long days = ChronoUnit.DAYS.between(LocalDateTime.now(),LocalDate.now().plus(5,DAYS));  //NG
        long days = ChronoUnit.DAYS.between(LocalDate.now(),LocalDate.now().plus(5,DAYS));          //OK

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //Period Represents the period of the date
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        Period p = Period.between(LocalDate.now(),LocalDate.now().plus(5,WEEKS));
        //See the contents of Period
        System.out.println(p.getDays());
        //You can also add / remove periods to Period
        Period newP = p.minusDays(10);
        //Can be added or subtracted from Period
        LocalDate ld2 =(LocalDate) p.addTo(ld);
        ld2 =(LocalDate) p.subtractFrom(ld);
        //Addition / subtraction between Periods is also possible
        Period p2 = p.plus(Period.ofDays(5));
        Period p3 = p.minus(Period.ofDays(5));

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //Duration Represents the duration of a date
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        Duration d = Duration.between(LocalDateTime.now(),LocalDateTime.now().plusDays(1));
        System.out.println(d);
        d = Duration.of(1,ChronoUnit.MINUTES);
        System.out.println("nanos" + d.getNano());
        System.out.println("seconds" + d.getSeconds());

        DateTimeFormatter dtf_bid = DateTimeFormatter.BASIC_ISO_DATE;
        DateTimeFormatter dtf_idt = DateTimeFormatter.ISO_DATE_TIME;
        DateTimeFormatter dtf_ii = DateTimeFormatter.ISO_INSTANT;
        DateTimeFormatter dtf_original = DateTimeFormatter.ofPattern("yyyy_MM_dd_hh_mm_ss"); //Define your own display format

        System.out.println(
                dtf_bid.format(LocalDateTime.now())
                + "#" +dtf_idt.format(LocalDateTime.now())
                //+ "#" + dtf_ii.format(LocalDateTime.now())  //NG
                + "#" + dtf_original.format(LocalDateTime.now()));


        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //Instant A class with cumulative seconds, milliseconds, and nanoseconds from a certain point in time
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //What is a certain point ↓
        System.out.println(Instant.EPOCH);  //January 1, 1970 0:00:00

        //Instance acquisition
        Instant i1 = Instant.ofEpochMilli(51455414);
        Instant i2 = Instant.ofEpochSecond(51455414);
        Instant i3 = Instant.ofEpochSecond(51455414,2515);  //The second argument is milliseconds
        //Instant instant = Instant.from(LocalTime.now());   //NG Unsupported

        //Acquisition of seconds, milliseconds, and nanoseconds
        System.out.println(Instant.now().getEpochSecond());  //get
        System.out.println(Instant.now().toEpochMilli());    //to (Why is this all to?)
        System.out.println(Instant.now().getNano());         //get

        System.out.println(Instant.ofEpochMilli(500000));

        //From various Temporal to Instant
        //From OffsetDateTime, toInstant()Get in
        OffsetDateTime odt = OffsetDateTime.now();
        System.out.println(odt.toInstant());
        //From ZonedDateTime, toInstant()Get in
        System.out.println(ZonedDateTime.now(ZoneId.systemDefault()).toInstant());
        //LocalDate does not have offset, so you need to set it when calculating instant
        System.out.println(LocalDateTime.now().toInstant(ZoneOffset.UTC));
    }
}

Recommended Posts

A memorandum to reach the itchy place for Java Gold
How to check for the contents of a java fixed-length string
Introduction to java for the first time # 2
About the procedure for java to work
[Java] (for MacOS) How to set the classpath
Java8 Gold exam memorandum
A note for Initializing Fields in the Java tutorial
How to make a mod for Slay the Spire
What Java engineers need to prepare for the Java 11 release
How to create a lightweight container image for Java apps
Create a method to return the tax rate in Java
[For beginners] About the JavaScript syntax explained by Java Gold
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
Input to the Java console
A memorandum when you want to see the data acquired by Jena & SPARQL for each variable.
How to deal with the type that I thought about writing a Java program for 2 years
Mechanism for converting to a language that the browser can recognize
I want to create a chat screen for the Swift chat app!
A cheat sheet for Java experienced people to learn Ruby (rails)
[Java] Adding an element to the Collection causes a compile error
Connecting to a database with Java (Part 1) Maybe the basic method
How to get the contents of Map using for statement Memorandum
Replace with a value according to the match with a Java regular expression
I made a method to ask for Premium Friday (Java 8 version)
[Java] I tried to make a maze by the digging method ♪
kotlin & Java: How to hide the toolbar only for specific fragments
How to make a groundbreaking diamond using Java for statement wwww
A memorandum for writing beautiful code
[java8] To understand the Stream API
How to make a Java container
[Java] How to create a folder
A memorandum of the FizzBuzz problem
Welcome to the Java Library Swamp! !!
The road from JavaScript to Java
How to make a Java array
The story of forgetting to close a file in Java and failing
[Java] How to turn a two-dimensional array with an extended for statement
[Java small story] Monitor when a value is added to the List
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
[Java improvement case] How to reach the limit of self-study and beyond
[Java] [For beginners] How to insert elements directly in a 2D array
[AWS SDK for Java] Set a retry policy on the S3 client
Set up a Java GUI in a separate thread to keep the main
[Java: memorandum] Until the line feed code CRLF is changed to LF
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
I tried to make a program that searches for the target class from the process that is overloaded with Java
How to make a Java calendar Summary
A memorandum on how to use Eclipse
A review note for the class java.util.Scanner
[Java] How to use the File class
Java reference to understand in the figure
A memorandum for android application development beginners
[Java] How to use the hasNext function
The road to creating a music game 2
Java SE8 Silver ~ The Road to Pass ~
[Creating] A memorandum about coding in Java
[Introduction to Java] How to write a Java program
[Java] How to use the toString () method
How to create a Maven repository for 2020
Java Programming Style Guide for the Java 11 Era