Learning memo when learning Java for the first time (personal learning memo)

Java was my first time to move from iOS to Android development, so I'll leave a note of what I learned at that time.

1. 1. Java overview

1.1 What is Java?

Java is an object-oriented programming language developed by Sun Microsystems, Inc. in 1995. When we simply say Java, it may mean not only Java as a programming language but also the entire platform including Java libraries and development environments. Hereinafter, when simply described as Java, it means Java as a programming language.

Java is a programming language with the following features.

1.2 Keywords for each version of Java

version keyword
Java5 Generics, autoboxing / unboxing, enum types, annotations, extended for statements, Concurrency Utilities
Java6 Speeding up AWT / Swing, JDBC4.0, performance improvement, enhancement of Concurrency Utilities
Java7 NIO2、Fork/Join framework, try-with-Resources, diamond notation, multi-catch of exceptions
Java8 Lambda notation, method references, Stream API, interface default methods, new Time API

1.3 About JDK and JRE

JRE (Java Runtime Environment) is a Java runtime environment, that is, a set of environments required to execute a program written in Java. The JRE consists of a Java Virtual Machine (JVM) and an API (Application Programming Interface), which is a set of standard Java class libraries.

On the other hand, JDK (Java Development Kit) is a Java development environment. The JDK is a binary for Java developers on each operating system and is published by Oracle. The JDK includes the JRE and provides the environment needed to develop Java.

2. Java basics

2.1 Java variables and types

Java types are roughly divided into primitive types and reference types. There are the following eight types of types that belong to primitive types.

Model name Commentary value Default value
boolean Boolean value true, false false
byte 8-bit integer -128〜127 (byte)0
short 16-bit integer -32768〜32767 (short)0
int 32-bit integer -2147483648〜2147483647 0
 long                                           |64-bit integer| -9223372036854775808L〜9223372036854775807L    | 0L                                                
 char                                           | UTF-16 code points| 'a'、'Ah'、'Words'、'\u03b1'Such| '\u0000' (null character)                                 
 float                                          |32-bit floating point number| 1.54f etc.| 0.0f                                              
 double                                         |64-bit floating point number| 2.45d etc.| 0.0d                                              

There are four types of reference types: class type, interface type, type parameter, and array type. Each is described separately.

2.2 Literal

A literal is a representation method for describing primitive type values, strings, and null values in the code field. The literals for each type are shown below.

Literal type Type of value generated Description method Example
Integer (decimal number) int Enumerate numbers 128
Integer (hexaque) int Prefix the number with 0x or 0X 0xab
Integer (8-ary number) int Prefix the number with 0 012
Integer (binary) int Prefix the number with 0b or 0B 0b11
integer long Add l or L to the end of the integer value 123L
Floating point number float Add f or F to the end of the decimal 2.34f
Floating point number double Decimal or exponential notation 3.45、345e-2
letter char Enclose characters in single quotes 'a'
String String Enclose the string in double quotes "abc"
Boolean value boolean true, false true
null value null type null null

2.3 Operator

Since the basics are the same operators as in C language, only some bit operators that are rarely used are described.

operator Example Commentary
>> a>>1 Shift a by 1 bit to the right. Fill the left edge with the same value as the most significant bit
>>> a>>>1 Shift a by 1 bit to the right. Fill the left edge with 0
~ ~a Invert all bits of a

2.4 Comment

As with C language, comments are described as // one-line comments or / * block comments * /. Basically, comments in Javadoc format are desirable.

2.5 branch

Since conditional branching is basically the same as in C language, details are omitted, but only some notes are described.

2.6 Exception handling

[About exceptions]

There are three types of exceptions in Java, all of which are represented by classes that inherit from the Throwable class.

[How to throw an exception]

Raising an exception is often called throwing an exception. In Java, you can throw an exception with "throw new exception class ()". When throwing a checked exception, it is desirable to use @throws in the Javadoc to describe under what circumstances the exception is thrown. Some of the most commonly used run-time exceptions are:

exception Throw case
IllegalArgumentException When the value of the argument at the time of calling is invalid
IllegalStateException When the method is called when the state of the object is invalid
NullPointerException When null is passed in an argument where null is prohibited
IndexOutOfBoundsException When an out-of-range value is specified in an argument that specifies an index

2.7 Resources

When using resource-based processing such as file opening, DB connection, and network connection, use the try-with-resources syntax. The process of closing the resource is automatically executed at the timing of exiting the block, and the process of closing the resource is executed even if an exception occurs in the block. If an exception occurs during the process of closing the resource, you can write a handler for the exception by adding the catch theory at the end.

try  (Class name Variable name=Get resources)  {
Processing using resources
}

2.8 Modifier

The following ** modifiers ** can be specified for classes, interfaces, methods, constructors, and variables.

Modifier class interface method constructor block var Classification Description
public × Access modifier Accessable from all classes
             [protected](http://www.tohoho-web.com/java/modifier.htm#access)|○|○|○|○|×|○|Accessmodifier|<spanstyle="color:rgb(0,0,0);">Protect access from other files and classes</span>    
              [private](http://www.tohoho-web.com/java/modifier.htm#access)       |   ○   |     ○     |   ○    |      ○      |   ×   |  ○  |Access modifier|Only access from own class
               [static](http://www.tohoho-web.com/java/modifier.htm#access)|○|○|○|×|×|○|staticmodifier|<spanstyle="color:rgb(0,0,0);">Indicates that it can be referenced even if it is not instantiated</span>  
                [final](http://www.tohoho-web.com/java/modifier.htm#final)        |   ○   |     ×     |   ○    |      ×      |   ×   |  ○  |final modifier|Indicates that it will not be overwritten. Prohibition of inheritance, overload, and change
             [abstract](http://www.tohoho-web.com/java/modifier.htm#abstract)|○|○|○|×|×|×|Abstractmodifier|<spanstyle="color:rgb(0,0,0);">It is abstract and indicates that the contents are defined and implemented at the inheritance destination.</span>  
               [native](http://www.tohoho-web.com/java/modifier.htm#native)|×|×|○|×|×|×|nativemodifier|<spanstyle="color:rgb(34,24,21);">Indicates that it is implemented in a language other than Java</span>   
         [synchronized](http://www.tohoho-web.com/java/modifier.htm#synchronized)|×|×|○|×|○|×|Syncmodifier|<spanstyle="color:rgb(0,0,0);">Indicates that exclusive control is performed in the case of multithreading.</span>   
            [transient](http://www.tohoho-web.com/java/modifier.htm#transient)    |   ×   |     ×     |   ×    |      ×      |   ×   |  ○  |Temporary modifier|Indicates that it is not subject to serialization
             [volatile](http://www.tohoho-web.com/java/modifier.htm#volatile)|×|×|×|×|×|○|Volatilemodifier|<spanstyle="color:rgb(34,24,21);">Suppress the cache and show that the values are the same across threads</span>
             [strictfp](http://www.tohoho-web.com/java/modifier.htm#strictfp)|○|○|○|×|×|×|Strictfloatingpointmodifier|<spanstyle="color:rgb(0,0,0);">Floating point arithmetic is platform independent and works strictly</span> |

2.9 Annotation

[About annotation]

Annotation means "annotation" and is a function to enter additional information for classes, methods, and packages. Added in Java SE 5 and used in the format "@ + annotation name".

The following annotations exist in the annotation.

The annotations provided as standard are explained below with examples.

[How to create annotation]

import java.lang.annotation.*;
  
//Specify how much information of the defined annotation is saved (retain information even at runtime with RUNTIME)
@Retention(RetentionPolicy.RUNTIME)
//Specify where to attach the defined annotation in the code field. It will be possible to describe in class, interface and enum with TYPE.
@Target(ElementType.TYPE)
//The information of the defined annotation will be described in Javadoc as well.
@Documented
public @interface Beta {
    String from();    //It can take a string type argument named from.@Beta(from = "1.0");And so on. If the argument name is value and it is the only argument, the description of value can be omitted.
}

2.10 Functional interface and lambda expression

[Functional interface]

A method that only declares the method name, signature, and return type is called an abstract method, and is described in the following format.

abstract [Return type] <Method name>(Signature);

Those that can have this abstract method, constant, default method, and static method as members are called an interface, and are described in the following format.

[Modifier] interface <Interface name> {
Data type variable name=value;
Qualifier Return data type Method name(Argument type declaration);
}

A functional interface is an interface that has only one defined abstract method. Even if the default method and static method are included, if there is only one abstract method, it is included in the functional interface.

[Lambda type]

Lambda expressions are a syntax introduced in Java SE 8 that allows you to concisely describe the implementation of a functional interface. In a method call that takes a functional interface as an argument, a lambda expression can be used in the argument part of the corresponding functional interface, and is described as follows.

(Lambda expression argument) -> {processing}

Reference URL: http://www.ne.jp/asahi/hishidama/home/tech/java/lambda.html

2.11 Method reference

Method reference is a syntax introduced in Java SE 8 that puts the method itself in a variable of a functional interface It is a syntax that can be assigned. The method reference is specified as follows.

//For static methods (and instance methods in some cases)
name of the class::Method name
  
//For instance methods
Instance variable name::Method name

2.12 Optional class

Optional is a class introduced in Java SE 8 that only wraps one value. is there. Optional simply holds the value, but each method of Optional changes its behavior depending on whether the held value is null or not. (Basically, processing is performed only when it is not null)

An instance of Optional becomes an object called empty if the contained value is null, and if there is a value, it becomes an instance that normally holds the value. The side that receives the Optional determines whether the Optional is empty and performs processing. Use the isPresent method to determine if Null is included in Optional. In addition to isPresent, there are orElse that returns the specified value when the value is not stored, orElseThrow that throws an exception when the value is not stored.

Recommended Posts

Learning memo when learning Java for the first time (personal learning memo)
Learning for the first time java [Introduction]
Introduction to java for the first time # 2
Learn for the first time java # 3 expressions and operators
Spring Boot for the first time
Spring AOP for the first time
[Android studio / Java] What you don't understand when you touch it for the first time
Think when Rails (turbolinks) doesn't load the page for the first time
A summary of what Java programmers find when reading Kotlin source for the first time
For JAVA learning (2018-03-16-01)
First steps for deep learning in Java
[DL4J] Java deep learning for the first time (handwriting recognition using a fully connected neural network)
[First Java] Make something that works with Intellij for the time being
[Socket communication (Java)] Impressions of implementing Socket communication in practice for the first time
Programming for the first time in my life Java 1st Hello World
I tried using Docker for the first time
The story of learning Java in the first programming
Walls hit by Rspec for the first time
Android Studio development for the first time (for beginners)
Use Java external library for the time being
Run Dataflow, Java, streaming for the time being
Java learning memo (method)
[WSL] Solution for the phenomenon that 404 is displayed when trying to insert Java with apt (personal memo)
Java learning memo (basic)
(Memo) Java for statement
Java learning memo (interface)
Java learning memo (inheritance)
Impressions and doubts about using java for the first time in Android Studio
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation
Oreore certificate https (2020/12/19) for the first time with nginx
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
A memo to do for the time being when building CentOS 6 series with VirtualBox
Java learning memo (data type)
Books used for learning Java
Java learning memo (logical operator)
Java HashMap, entrySet [Personal memo]
[Rails] I tried using the button_to method for the first time
[Personal memo] Frequently used Java grammar updated from time to time
Modeling a Digimon with DDD for the first time Part 1
Java14 came out, so I tried record for the time being
Java learning memo (creating an array)
Personal memo: Metaprogramming with Java reflection
Java learning memo (while statement, do-while statement)
Memo after the first Spring project-MVC-
Memo after the first Spring project-Database-
[Java] Processing time measurement method memo
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
A memo when you want to clear the time part of the calendar
For the time being, run the war file delivered in Java with Docker
The story of releasing the Android app to the Play Store for the first time.
Creating an app and deploying it for the first time on heroku
(Learning memo) Java Level 2 measures: Question range
Memo after the first Spring project-What is Spring-
[Personal memo] Java data type is annoying
Memo: [Java] Check the contents of the directory
Touching kotlin for the first time-Enum Classes
About the procedure for java to work
Precautions when making Docker for deep learning
Organized memo in the head (Java --Array)
Memo when HTTP communication with Java (OkHttp)