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.
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.
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 |
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.
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.
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 |
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 |
As with C language, comments are described as // one-line comments or / * block comments * /. Basically, comments in Javadoc format are desirable.
Since conditional branching is basically the same as in C language, details are omitted, but only some notes are described.
There are three types of exceptions in Java, all of which are represented by classes that inherit from the Throwable class.
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 |
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
}
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> |
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.
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.
}
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 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
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
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