ImmutableClass
一旦初始化,值沒法修改
主要是用於多線程之間的數據共享,避免數據污染
class不設置setter便可實現Immutable Class
public class ImmutableIntArray {
private final int[] array;
public ImmutableIntArray(int[] array) {
this.array = array.clone();
}
public int[] getValue() {
return this.clone();
}
}
null檢查
對象null檢查
Object obj = null;
Objects.isNull(obj); // true
Objects.nonNull(obj); // false
集合null檢查
List<Object> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(null);
list.add(3);
list.stream().filter(Objects::nonNull).forEach(item -> {
System.out.println(item);
}); // 過濾null值
字符編碼
BufferedWriter wr = Files.newBufferedWriter(Paths.get("demo.txt"),StandardCharsets.UTF_8)
BufferedReader reader = Files.newBufferedReader(Paths.get("demo.txt"),StandardCharsets.UTF_8)
String s = new String(Files.readAllBytes(Paths.get("demo.txt")), StandardCharsets.UTF_8);
byte[] bytes = "aaaa".getBytes(StandardCharsets.UTF_8);
default logger
private static final Logger logger = Logger.getLogger(TestController.class.getName());
logger.log(Level.INFO, String.format("%s: %s", "輸出", "日誌"));
logger.warning(String.format("%s: %s", "輸出", "日誌"));
日誌等級
SEVERE
WARNING
INFO
CONFIG
FINE
FINER
FINEST
函數可變參數
void fn(int... x) {
for (int item : x) {
System.out.println(item);
}
}
fn(1,2,3,4,5);
Nashorn JavaScript engine
執行腳本文件
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
engine.eval(new FileReader("C:\\Users\\26401\\Desktop\\" + "demo.js"));
改變js文件輸出流
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");
StringWriter stringWriter = new StringWriter();
engine.getContext().setWriter(stringWriter);
engine.eval(new FileReader("C:\\Users\\26401\\Desktop\\" + "demo.js"));
return stringWriter.toString();
全局變量
engine.put("global", "globaldata");
engine.eval("print(global);");
engine.get("global");
2D圖
package com.springlearn.learn.model;
import javax.swing.*;
import java.awt.*;
// MyPanel extends JPanel, which will eventually be placed in a JFrame
public class MyPanel extends JPanel {
// custom painting is performed by the paintComponent method
@Override
public void paintComponent(Graphics g){
// clear the previous painting
super.paintComponent(g);
// cast Graphics to Graphics2D
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red); // sets Graphics2D color
// draw the rectangle
g2.drawRect(0,0,100,100); // drawRect(x-position, y-position, width, height)
g2.setColor(Color.blue);
g2.fillRect(200,0,100,100); // fill new rectangle with color blue
}
}
public class Test{
JFrame f;
MyPanel p;
public Test(){
f = new JFrame();
// get the content area of Panel.
Container c = f.getContentPane();
// set the LayoutManager
c.setLayout(new BorderLayout());
p = new MyPanel();
// add MyPanel object into container
c.add(p);
// set the size of the JFrame
f.setSize(400,400);
// make the JFrame visible
f.setVisible(true);
// sets close behavior; EXIT_ON_CLOSE invokes System.exit(0) on closing the JFrame
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) throws Exception {
DemoApplication t = new DemoApplication();
}
}
類單例
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
public class Singleton {
private static class InstanceHolder {
static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return InstanceHolder.INSTANCE;
}
private Singleton() {}
}
克隆
public class Test implements Cloneable {
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Test test = new Test();
Test testcopy = (Test) test.clone();
修飾符
經常使用修飾符
public 公開
protected 父類和子類可用
default 不寫權限關鍵字,默認是default,只能在當前package下是使用
private 本類可用
final 表示固定不變的
final修飾的類不能被繼承
final修飾的方法不能被子類重寫
final修飾的變量就是常量,不能修改 final int j = 11;
static 表示靜態,只能經過類自己訪問
其餘修飾符
strictfp 浮點計算
strictfp class A{}
volatile 多線程中,讀取最新值
volatile boolean active;
synchronized 只容許一個線程操做
synchronized void SharedMethod(){}
void SharedMethod(){
synchronized (this){
...
}
}
transient 不會被序列化
public transient int limit = 55;
結語
本文章是java成神的系列文章之一
若是你想知道,可是本文沒有的,請下方留言
我會第一時間總結出來併發布填充到本文