For personal notes
--Caution when using wrapper class
--The initial value is null, so if you try to leave it as it is, it will drop with a nullpo.
--branch on a string
--Comparison of String hashCode ()
and ʻequals ()` in that order.
--Case sensitive.
public class Sample {
static int num1;
static Integer num2;
public static void main(String arg[]) {
//The basic data type is initialized to 0
switch (num1) {
case 0:
System.out.println(0);
break;
default:
System.out.println(1);
break;
}
//Fall with a nullpo
switch (num2) {
case 0:
System.out.println(0);
break;
default:
System.out.println(1);
break;
}
}
}
//result
[skanehira@MacBook java]$ java Sample
0
Exception in thread "main" java.lang.NullPointerException
at Sample.main(Sample.java:17)
[skanehira@MacBook java]$
-** Extended Literal **
--Integer literals with _ (underscore)
--The beginning and end, and before and after the symbol cannot be used.
-** Access decorator and encapsulation **
--Encapsulation
--By adding private to member variables and public to Getter and Setter,
Make data accessible only through methods.
--protected and private can only use methods
--Access range (table below)
Access decorator | meaning |
---|---|
public | Accessable from anywhere |
protected | Only accessible from the same package or inherited classes |
unspecified | Only accessible from the same package |
private | Only accessible from within the class |
-** final and static decorators **
-- How to call static members
--Class name.static member
--Variable name.static member
--`Direct access within the instance and static member classes`` --static → instance is NG --instance → static is OK
--final decorator
Applicable place | meaning |
---|---|
variable | Become a constant |
Method | Cannot be overridden |
class | Cannot inherit |
-** Initializer block **
--static initializer`` --A block that runs only once when the class is loaded. --
Initiator``
--A block that is executed before the constructor is called when instantiating.
--Order of execution
--static initializer-> initializer-> constructor.
public class Sample {
public static void main(String arg[]) {
new Init();
}
}
class Init {
//static initializer
static {
System.out.println("static initializer");
}
//Initializer
{
System.out.println("initializer");
}
//constructor
Init() {
System.out.println("construct");
}
}
//Execution result
[skanehira@MacBook java]$ java Sample
static initializer
initializer
construct
-** Enumeration **
- Enum
--If enum is defined, it cannot be inherited because it is a final class that inherits java.lang.Enum class.
-Defined variables are automatically added with public static final
and become class constants.
--Cannot be instantiated with new.
--You can implement the interface.
--Since the Comparable interface is implemented, the order of defined variables is guaranteed.
--Constructor / variable / method definition in enumeration
public class Sample {
public static void main(String arg[]) {
// toString():The constant name is returned
System.out.println(Status.ACTIVE);
System.out.println(Status.STOP);
//Defined method
System.out.println(Status.ACTIVE.getNum());
// ordinal():Return constant position
//0 from the defined order,1,2...
System.out.println(Status.ACTIVE.ordinal());
System.out.println(Status.STOP.ordinal());
// values():Get a list of defined constants
for (Status s : Status.values()) {
System.out.println(s);
}
// valueOf():Get object of defined constant
Status active = Status.valueOf("ACTIVE");
// name():Get constant name
System.out.println(active.name());
}
}
enum Status {
//Passing an int to the constructor to create a new Status type Object
ACTIVE(0), STOP(1);
//Define variables
private int num;
//constructor
Status(int n) {
this.num = n;
}
//Method
public int getNum() {
return num;
}
}
//result
[skanehira@MacBook java]$ java Sample
ACTIVE
STOP
0
0
1
ACTIVE
STOP
ACTIVE
-** Object class **
- toString()
--When overriding, add public
(I often forget ...)
- `finalize()`
--Executed before the object is destroyed by the garbage collector. --The implementation timing cannot be specified, and the VM will do it appropriately. --super finalize () needs to be called explicitly.
--ʻEquals () and hashCode () --ʻEquals ()
compares if they are the same object
For String class, compare if they are the same character
--The rules for overriding are as follows.
When ʻequals ()is true,
hashCode () returns the same value ʻequals ()
returns false if hashCode ()
has different values
Even if ʻequals ()is false, both
hashCode ()` are OK.
-** static import **
--Syntax
ʻImport static package name. class name. *; ʻImport static package name.class name.static method;
ʻImport static package name.class name.static variable;`
import static java.lang.System.out;
public class Sample {
public static void main(String arg[]) {
out.println("import static");
}
}
//result
[skanehira@MacBook java]$ java Sample
import static
-** Inheritance **
--Override and concealment
--Override
--Access decorators must be as wide as or wider than the parent class.
--The return value, method name, number of arguments, and order must match exactly.
--Concealment
--When overriding a static member, the access destination is determined by the type of the variable.
--For non-static members, the access destination is determined by the instance type.
--variadic argument
--The syntax is data type ...
.
--Must be defined at the end of the argument list.
--Only one can be defined.
--If no arguments are passed, the array will be empty.
--If you pass null, you will get a warning if you don't cast to string
or [] string
.
--The method that explicitly defines the argument list is called preferentially.
--Variadic arguments and overloads`` --Overloaded arguments are judged in the following order of priority.
Exact match → Implicit type conversion → AutoBoxing → Variadic variable`
--this and super
--If no constructor is defined, a no-argument constructor is defined at compile time.
--If a constructor is defined, no argument-free constructor is defined at compile time.
--If the subclass constructor does not have super ()
or super (with arguments)
,
Super ()
with no arguments is defined at compile time.
--this
and super
must be defined at the beginning of the constructor.
-** Abstract class **
--What is an abstract class
?
--A class with abstract.
--Abstract method (with abstract) and concrete method (with implementation) can be defined.
--Cannot be instantiated.
--Abstract class inheritance class
--Abstract methods must be overridden.
--You can inherit between abstract classes, but you don't have to override the abstract method.
--All abstract methods need to be implemented.
--Static member definition of abstract class
</ summary>
--The instance member defined in the abstract class must be called from the implemented abstract method.
--Static methods can be called with class name.static member
like any other.
-** Interface **
--What is an interface
?
--Only abstract methods and static variables can be defined.
--You can define public static variables.
--Subinterfaces can be defined.
--Constants and methods
--Static variables are forced to have public static final
, so they need to be initialized at the time of declaration.
--The method is forced to have public abstract
, so you need to use public
when overriding.
--Implementation class
--You can use extends and implements at the same time, but you need to write extends first.
--Inheritance
--Multiple inheritance (subIF extends XIF, YIF {}
) and multiple implementation (class A implements XIF, YIF {}
) are possible
-** Type conversion **
--Basic data type type conversion rule
--Implicit
- byte
→char or short
→int
→long
→float
→double
--Cast --Reverse order to implicit conversion
byte, short, char
type as it is.
--When calculating with the operator, if one A is double, float, long, int,
If one B has a smaller data type, it is converted to the other A data type.--Reference type conversion rule
--Implicit
--Handle subclass instances as superclass variables.
--Cast --Subclass instances can be converted to superclass types. ――The reverse is not possible. (Because superclasses do not contain subclasses)
-** Nested class ** --What is `? --A class defined in a class. --Because it is one of the members of the outer class, access modifiers, abstract, and final can be used. --There are static and non-static nested classes. Non-static classes are called inner classes.
--Rule
--You cannot use the same name in the outer class.
--You can use access modifiers, abstract, and final.
--Static classes can have non-static and static members.
You cannot access the instance variable of the outer class.
--Inner classes cannot have static members.
You can access the instance variables of the outer class.
--Definition and compilation`` --Nested classes are also compiled, so the generated class file will be
outer class name $ nested class name.class`.
--Access to nested classes`` --
Outer class. Non-static class name Variable name = new Outer class (). New Non-static class name (); --
Outer class.static class name Variable name = new Outer class.static class name () --When using nested classes in methods
new non-static class name (). methodA ()
static class name.methodB ()or
new static class (). methodB ()`
--Application of nested class
--Nested classes implement interface and can inherit abstract classes.
The rules are the same.
-** Local class ** --What is `? --A class defined in a method.
--Rule
--Only non-static classes can be defined.
--Access modifiers cannot be used.
--Abstract and final can be used.
--You can access the instance variables of the outer class.
--Of the arguments and local variables of the outer class method, only those specified as final can be accessed.
-** Anonymous class **
--What is ? --Without specifying the class name in the method A class that can define a class declaration and instantiation with a single expression. Since it is defined as an expression,
;` is required at the end.
--Rule
--Only non-static classes can be defined.
--Access modifiers cannot be used.
--Abstract and final cannot be used.
--You can access the instance variables of the outer class.
--Of the arguments and local variables of the outer class method, only those specified as final can be accessed.
--Cannot define constructor.
-** is-a relationship ** ――It refers to the inheritance relationship. The same reasoning that dogs, cats, and humans are animals.
-** has-a relationship ** --Has a different class of functionality. The same reasoning that a car has tires and steering wheel.
-** Cohesion and coupling **
--Cohesion
Refers to the appropriateness of class role division.
If the degree of cohesion is high,
There is a strong relationship between methods and variables between classes,
The range of influence is large when specification changes occur.
--Coupling
Refers to the degree of dependence between classes.
A low degree of dependence is called loose coupling.
It is desirable that the degree is low.
I don't know if it fits, but for example, a car doesn't move without tires because it has a high degree of coupling.
The car works poorly without the DVD pliers.
-** Composition ** ――A particularly strong relationship in aggregation is called composition. (↑ Example of a car) The survival time of the whole class and the subclass is the same.
-** Pattern **
--Single tone
A pattern that creates only one object.
Create a private
constructor and
Hold the instance in the private static final
variable and
This can be achieved by preparing a public
method that returns an instance.
- `DAO`
--A pattern that separates persistence objects and application logic (DB connection (opens files), CRUD).
--Factory
--A pattern that creates a class that has the role of creating an instance.
In the java standard API, this is the valueOf ()
of each wrapper class.
Java API
-** Difference between StringBuilder and StringBuffer **
--What is ? --You can perform operations on the character string once generated. The point is that you can mess with the data in memory, so don't duplicate it. Even if str1.append (str2)
does not change the reference destination, it feels like str2 is added to str1.
--Difference
--StringBuilder
is not threadsafe, StringBuffer
is threadsafe.
Other than that, it is almost the same.
--ʻEquals method --Since the equals of Object is not overridden, it is a reference comparison. Overriding ʻequals
is String
and wrapper classes (such as Integer)
--Other methods
constructor | Description |
---|---|
StringBuffer() | Build a StringBuffer with a capacity of 16 characters |
StringBuffer(int capacyty) | Build a StringBuffer with the specified capacity |
StringBuffer(String str) | Build a StringBuffer initialized with the specified characters |
Method | Description |
---|---|
StringBuffer append(String str) | Add the specified string at the end |
StringBuffer insert(int offset, String str) | offset-Insert the specified character string at position 1 |
StringBuffer reverse() | Reverse the string(cba for abc) |
void setCharAt(int index, char ch) | Replace index character with specified character |
StringBuffer replace(int start, int end, String str) | start to end-Replaces up to 1 character with the specified character |
String subString(int start, int end) | start to end-Returns a string that specifies up to 1 character |
String subString(int start) | Return the string from start to the end |
StringBuffer delete(int start, int end) | start to end-Delete strings up to 1 |
-** Wrapper class ** --What is `? --A class for manipulating basic data types. Useful when you want to convert a number to a string and vice versa.
--Method
- ValueOf()
Static method to convert basic data type to wrapper class
- `parseXXX`
Static method to convert a string to a basic data type
- `ValueXXX`
Methods to convert wrapper classes to basic data types
-** Auto Boxing and Unboxing **
-** Locale **
-** Resource Bundle ** --What is `?
--Using the ListResourceBundle class
--Implementation of resource bundle and its usage class ①
--Implementation of resource bundle and its usage class ②
--Using the PropertyResourceBundle class
--`Search resource bundle``
--ResourceBundle.Control class
NumberFormat
DecimalFormat
DateFormat
SimpleDateFormat
? --
Metacharacter`
java.util.regex
-** Collection **
--What is ? --
Types and features -
List,Set,Queue,Map --
List interface implementation --
Implementation of Set interface --
Iterator``
--Queue implementation
--Map interface implementation
-** Generics **
--What is ? --
Diamond operator --
Class definition --
Method definition --
Interface declaration --
Generics with inheritance --
Generics with wildcards``
-** Object ordering **
--Compareble and Comparator interfaces
- Comparable
- Comparator
--Sort and search of arrays and lists
--ʻArrays class`
-** Exception class **
- checked
- unchecked
--Original exception class
Role of each block
--Multiple block catch
--Multi Catch
-** throw and throws **
- throw
- throws
- rethrows
--`Caution when overriding``
-** Extension of Throwable class **
-** Assertion **
-** System constant **
-** Serialization **
-** Serialization inheritance **
-** Stream formatting and parsing **
--Stream formatting
--Stream analysis
NIO.2
-** Package **
- java.nio.file
- java.nio.file.attribute
- java.nio.channels
-** Path interface **
-** Random access file **
-** Directory operation **
-- Directory access
--Searching the file tree`` --
File search --`Change and monitor
JDBC
-** Package **
- java.sql
- javax.sql
- javax.sql.rowset
-** JDBC driver **
-** Exception **
-** Execution of SQL statement **
--Statement interface
--Inquiry
--Insert
--Delete
-** PreparedStatement interface **
-** CallbackStatement interface **
-** Advanced data type **
-** Metadata **
--ResultSetMetaData interface
-** Extension of ResultSet **
--Scroll query results, absolute / relative position specification
--`Insert / update data on ResultSet object``
-** Transaction **
--Control
--Transaction isolation level
-** RowSet related components **
--RowSet interface
--RowSet implementation type`` -
Connected RowSet -
Disconnected RowSet`
-** Thread ** --Create and start
-** Parallel collection ** --Package --Extended Queue interface --Map interface extension --Extended ArrayList and Set interfaces --Executor framework --Thread pool --ScheduleThreadPoolExecutor class --Callable interface --Atomic and rock --java.util.concurrent.atomic package --java.utii.concurrent.locks package --Fork / Join framework