---
title: Java()
description: JavaSPIIO(BIO/NIO/AIO)
category: Java
tag:
- Java
head:
- - meta
- name: keywords
content: Java,,,,SPI,,IO,,try-with-resources,BIO NIO AIO,Java
---
##
**Java **

### Exception Error
Java `java.lang` `Throwable` `Throwable` :
- **`Exception`** : `catch` `Exception` Checked Exception () Unchecked Exception ()
- **`Error`** `Error` ~~ `catch` ~~`catch` Java `Virtual MachineError`(`OutOfMemoryError`)`NoClassDefFoundError` Java JVM
### ClassNotFoundException NoClassDefFoundError
- `ClassNotFoundException` Exception
- `NoClassDefFoundError` Error jar JVM
### Checked Exception Unchecked Exception
**Checked Exception** Java `catch``throws`
IO

`RuntimeException``Exception` IO `ClassNotFoundException``SQLException`...
**Unchecked Exception** **** Java
`RuntimeException`
- `NullPointerException`()
- `IllegalArgumentException`()
- `NumberFormatException``IllegalArgumentException`
- `ArrayIndexOutOfBoundsException`
- `ClassCastException`
- `ArithmeticException`
- `SecurityException`
- `UnsupportedOperationException`()
-

### Checked Exception Unchecked Exception
Unchecked Exception Checked Exception
Unchecked Exception `NullPointerException` Bug Bug `try-catch`
Checked Exception bug Checked Exception
### Throwable
- `String getMessage()`:
- `String toString()`:
- `String getLocalizedMessage()`: `Throwable` `getMessage()`
- `void printStackTrace()`: `Throwable`
### try-catch-finally
- `try` `catch` `catch` `finally`
- `catch` try
- `finally` `finally` `try` `catch` `return` `finally`
```java
try {
System.out.println("Try to do something");
throw new RuntimeException("RuntimeException");
} catch (Exception e) {
System.out.println("Catch Exception -> " + e.getMessage());
} finally {
System.out.println("Finally");
}
```
```plain
Try to do something
Catch Exception -> RuntimeException
Finally
```
** finally return!** try finally return try return try return finally return finally return
```java
public static void main(String[] args) {
System.out.println(f(2));
}
public static int f(int value) {
try {
return value * value;
} finally {
if (value == 2) {
return 0;
}
}
}
```
```plain
0
```
### finally
finally
finally finally
```java
try {
System.out.println("Try to do something");
throw new RuntimeException("RuntimeException");
} catch (Exception e) {
System.out.println("Catch Exception -> " + e.getMessage());
// Java
System.exit(1);
} finally {
System.out.println("Finally");
}
```
```plain
Try to do something
Catch Exception -> RuntimeException
```
2 `finally`
1.
2. CPU
issue
`try catch finally`
### `try-with-resources` `try-catch-finally`
1. **** `java.lang.AutoCloseable` `java.io.Closeable`
2. ** finally ** `try-with-resources` catch finally
Effective Java
> `try-with-resources` `try-finally``try-with-resources``try-finally`
Java `InputStream``OutputStream``Scanner``PrintWriter``close()``try-catch-finally`
```java
//
Scanner scanner = null;
try {
scanner = new Scanner(new File("D://read.txt"));
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
```
Java 7 `try-with-resources` :
```java
try (Scanner scanner = new Scanner(new File("test.txt"))) {
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
```
`try-with-resources` `try-catch-finally`
`try-with-resources`
```java
try (BufferedInputStream bin = new BufferedInputStream(new FileInputStream(new File("test.txt")));
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(new File("out.txt")))) {
int b;
while ((b = bin.read()) != -1) {
bout.write(b);
}
}
catch (IOException e) {
e.printStackTrace();
}
```
###
- new
-
- `NumberFormatException``IllegalArgumentException`
-
-
##
###
**Java Generics** JDK 5
`ArrayList persons = new ArrayList()` `ArrayList` `Person`
```java
ArrayList extends AbstractList
```
`List` `Object`
###
:************
**1.**
```java
//TTEKV
//T
public class Generic{
private T key;
public Generic(T key) {
this.key = key;
}
public T getKey(){
return key;
}
}
```
```java
Generic genericInteger = new Generic(123456);
```
**2.**
```java
public interface Generator {
public T method();
}
```
```java
class GeneratorImpl implements Generator{
@Override
public T method() {
return null;
}
}
```
```java
class GeneratorImpl implements Generator {
@Override
public String method() {
return "hello";
}
}
```
**3.**
```java
public static < E > void printArray( E[] inputArray )
{
for ( E element : inputArray ){
System.out.printf( "%s ", element );
}
System.out.println();
}
```
```java
// Integer, Double Character
Integer[] intArray = { 1, 2, 3 };
String[] stringArray = { "Hello", "World" };
printArray( intArray );
printArray( stringArray );
```
> : `public static < E > void printArray( E[] inputArray )` ; java ``
###
- `CommonResult` `T`
- `Excel` `ExcelUtil` `Excel`
- `Collections` `sort`, `binarySearch`
-
##
[Java ](https://javaguide.cn/java/basis/reflection.html)
###
Java (Reflection) ****
****
****
###
****
1. ****
2. **** Java SpringHibernateMyBatisDIAOPORM
3. ****Bean
****
1. **** JIT
2. **** Java `private`
3. ****
[Java Reflection: Why is it so slow?](https://stackoverflow.com/questions/1392351/java-reflection-why-is-it-so-slow)
###
Java Reflection** Spring/Spring BootMyBatis **
**1.IoC**
Spring/Spring Boot IoC `@Component`, `@Service`, `@Repository`, `@Controller`Bean `@Autowired`
**2.**
`@Value`
**3. AOP**
AOP AOP JDK Proxy InvocationHandler `Method.invoke`
```java
public class DebugInvocationHandler implements InvocationHandler {
private final Object target; //
public DebugInvocationHandler(Object target) { this.target = target; }
// proxy: , method: , args:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(" " + method.getName() + " ");
//
Object result = method.invoke(target, args);
System.out.println(" " + method.getName() + " ");
return result;
}
}
```
**4.ORM**
MyBatisHibernate Java Java Java setter SQL
##
Java [Java ](https://javaguide.cn/java/basis/proxy.html "Java ")
###
******Enhancement**
Java **JDK ** **CGLIB **
**JDK **
Java JDK `Proxy.newProxyInstance()` `.java` `.class`
`InvocationHandler` `invoke` `invoke`
**CGLIB **
CGLIB JDK ASM `final``private` `static`
CGLIB `MethodInterceptor` `intercept` `InvocationHandler` `invoke` `intercept`
###
****
| | (Static Proxy) | (Dynamic Proxy) |
| ---------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
| | `.class` | JVM |
| | | `Handler`/`Interceptor` |
| | | |
| | | |
| | | |
| | | Spring AOPRPC DubboORM |
### JDK CGLIB
1. JDK CGLIB `final` `final` `private`
2. JDK JDK
###
**Spring AOP**
AOP(Aspect-Oriented Programming:)
Spring AOP Spring AOP **JDK Proxy** JDK Proxy Spring AOP **Cglib**

##
###
`Annotation` Java5
`Annotation`
```java
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
public interface Override extends Annotation{
}
```
JDK `@Override``@Deprecated`
###
- **** Java `@Override`
- ****( Spring `@Value``@Component`)
## SPI
SPI [Java SPI ](https://javaguide.cn/java/basis/spi.html)
### SPI?
SPI Service Provider Interface
SPI
Java SPI Spring Dubbo
[22e1830e0b0e4115a882751f6c417857tplv-k3u1fbpfcp-zoom-1.jpeg]
### SPI API
** SPI API **
SPI APIApplication Programming Interface

- **API**
- **SPI**
H H
### SPI
SPI SPI
-
- `ServiceLoader` `load`
##
[Java ](https://javaguide.cn/java/basis/serialization.html)
### ??
Java Java Java
- **** JSON, XML
- ****
Java Object(Class) C++struct() class
- RPC
-
- Redis
-
> ****serializationmarshallingdeserializationunmarshalling
****

https://www.corejavaguru.com/java/serialization/interview-questions-1
** TCP/IP 4 **
TCP/IP
1.
2.
3.
4.

OSI
OSI TCP/IP TCP/IP
###
`transient`
`transient` `transient`
`transient`
- `transient`
- `transient` `int` `0`
- `static` (Object) `transient`
###
JDK HessianKryoProtobufProtoStuff
JSON XML
### JDK
JDK
- **** :
- ****
- ****[JAVA ](https://cryin.github.io/blog/secure-development-java-deserialization-vulnerability/)
## I/O
I/O
- [Java IO ](https://javaguide.cn/java/io/io-basis.html)
- [Java IO ](https://javaguide.cn/java/io/io-design-patterns.html)
- [Java IO ](https://javaguide.cn/java/io/io-model.html)
### Java IO
IO `Input/Output` IO IO Java
Java IO 40 4
- `InputStream`/`Reader`:
- `OutputStream`/`Writer`:
### I/O ?
** I/O **
- Java
-
### Java IO
[Java IO ](https://javaguide.cn/java/io/io-design-patterns.html)
### BIONIO AIO
[Java IO ](https://javaguide.cn/java/io/io-model.html)
##
###
**Syntactic sugar**
Java `for-each` for
```java
String[] strs = {"JavaGuide", "JavaGuide", "https://javaguide.cn/"};
for (String s : strs) {
System.out.println(s);
}
```
JVM Java JVM Java Java JVM`com.sun.tools.javac.main.JavaCompiler``compile()``desugar()`
### Java
Java for try-with-resources lambda
[Java ](./syntactic-sugar.md)