| [ Web Proxy ] |
| Viewing: https://raw.githubusercontent.com/buleli-AI/JavaNote/main/Java.md | [Back] [Original] |
[Debug]
***
##
###
Java
****
*
* == ****
*
***
###
####
```java
class {
}
```
1. StudentNameCode
2. Java Java
3. Java public **public Java **
```java
:
class {
1.(Field):
2.(Method):
3.(Constructor):
4.
5.
}
public class ClassDemo {
System.out.println(1);//
}
```
***
####
```java
(){
}
```
****
= new
*
*
------
###
`package `
`import .`
***
###
1. ** private **
2. ** getter setter **
private
***
### this
this
* this
* this ** this **
* this
* this
------
### static
####
Java static
static
*
* static ****
* static ****
*
* static
* static
****
#### static
* ****
* .
* .
*
* .
* static
* .
* . JVM
* static
* .
```java
public class Student {
// 1.static
public static void inAddr(){ }
// 2.static
public void eat(){}
public static void main(String[] args) {
// a..
Student.inAddr();
inAddr();
// b..
// Student.eat(); //
Student sea = new Student();
sea.eat();
}
}
```
***
####
* main
*
* class jdk8
*
*
* ?
*
* ****
*
* ?
*
------
###
####
Java
* /
*
* ****
*
*
1.
2. ****
3.
4.
5. Object Object **Object Java **
```java
extends {
}
```
*
*
* ****
```java
public class ExtendsDemo {
public static void main(String[] args) {
Cat c = new Cat();
// c.run();
Cat.test();
System.out.println(Cat.schoolName);
}
}
class Cat extends Animal{
}
class Animal{
public static String schoolName ="seazean";
public static void test(){}
private void run(){}
}
```
***
####
****
super.super
```java
public class ExtendsDemo {
public static void wmain(String[] args) {
Wolf w = new Wolf();w
w.showName();
}
}
class Wolf extends Animal{
private String name = "";
public void showName(){
String name = "";
System.out.println(name); // name
System.out.println(this.name); // name
System.out.println(super.name); //
System.out.println(name1); //
//System.out.println(name2); //
}
}
class Animal{
public String name = "";
public String name1 = "";
}
```
***
####
****
****
@Override
*
* @Override
****
-
-
-
-
- ****
```java
public class ExtendsDemo {
public static void main(String[] args) {
Wolf w = new Wolf();
w.run();
}
}
class Wolf extends Animal{
@Override
public void run(){}//
}
class Animal{
public void run(){}
}
```
***
####
*
1. super()
2.
3. JVM
```java
class Animal {
public Animal() {
System.out.println("==Animal==");
}
}
class Tiger extends Animal {
public Tiger() {
super(); //
System.out.println("==Tiger==");
}
public Tiger(String name) {
//super();
System.out.println("==Tiger==");
}
}
```
* ** Java **
Java
```java
class A{
public void test(){
System.out.println("A");
}
}
class B{
public void test(){
System.out.println("B");
}
}
class C extends A , B {
public static void main(String[] args){
C c = new C();
c.test();
// Java
}
}
```
------
### super
super
* this this.this.**this(...)**
* super super.super.super(...)
* this(...) super(...)
* this(...) super(...)
* this(...) super(...) ****
```java
public class ThisDemo {
public static void main(String[] args) {
//
Student s1 = new Student("", 1000 );
Student s2 = new Student("", 2000, "" );
}
}
class Study extends Student {
public Study(String name, int age, String schoolName) {
super(name , age , schoolName) ;
//
}
}
class Student{
private String name ;
private int age ;
private String schoolName ;
public Student() {
}
public Student(String name , int age){
//
this(name , age , "");
}
public Student(String name, int age, String schoolName) {
this.name = name;
this.age = age;
this.schoolName = schoolName;
}
// .......get + set
}
```
***
### final
####
final
* final
* final
* final
final abstract ****
***
####
#####
final
public static final
final
1.
2.
```java
public class FinalDemo {
//public static final
public static final String SCHOOL_NAME = "" ;
public static final String SCHOOL_NAME1;
static{
//SCHOOL_NAME = "java";//
SCHOOL_NAME1 = "1";
}
}
```
***
#####
final
final 1
1.
2.
3.
```java
public class FinalDemo {
private final String name = "" ;
private final String name1;
private final String name2;
{
//
name1 = "1";
}
//
public FinalDemo(){
name2 = "2";
}
public FinalDemo(String a){
name2 = "2";
}
public static void main(String[] args) {
FinalDemo f1 = new FinalDemo();
//f1.name = "1"; //
}
}
```
***
###
####
>
abstract
abstract ****
****
```java
public class AbstractDemo {
public static void main(String[] args) {
Dog d = new Dog();
d.run();
}
}
class Dog extends Animal{
@Override
public void run() {
System.out.println("");
}
}
abstract class Animal{
public abstract void run();
}
```
***
####
?
*
* ****
>
```java
public class AbstractDemo {
public static void main(String[] args) {
//Animal a = new Animal();
//a.run(); //
}
}
abstract class Animal{
private String name;
public static String schoolName = "";
public Animal(){ }
public abstract void run();
//
public void go(){ }
}
```
static abstract
static
***
####
****
""****
```java
//
public class ExtendsDemo {
public static void main(String[] args) {
Student xiaoMa = new Student();
xiaoMa.write();
}
}
class Student extends Template{
@Override
public String writeText() {return "\t"}
}
// 1.
abstract class Template{
private String title = "\t\t\t\t\t";
private String start = "\t";
private String last = "\t";
public void write(){
System.out.println(title+"\n"+start);
System.out.println(writeText());
System.out.println(last);
}
//
public abstract String writeText();
}
```
***
###
####
Java
```java
interface {
//
//
//
//
}
```
* public abstract
*
* public static final public static final
```java
public interface InterfaceDemo{
//public static final String SCHOOL_NAME = "";
String SCHOOL_NAME = "";
//public abstract void run();
void run();//
}
```
***
####
****
*
*
* ****
```java
class implements 1,2,3,....{
}
interface extend 1,2,3,....{
}
```
1.
2.
3. ****
4. ****
```java
public class InterfaceDemo {
public static void main(String[] args) {
Student s = new Student();
s.run();
s.rule();
}
}
class Student implements Food, Person{
@Override
public void eat() {}
@Override
public void run() {}
}
interface Food{
void eat();
}
interface Person{
void run();
}
// interface Person extend Food,
// class Student implements Person
```
***
####
jdk1.8
*
* default public
*
*
*
* public
*
* ClassName.method()
*
* JDK 1.9 ****
```java
public class InterfaceDemo {
public static void main(String[] args) {
// 1.
Man m = new Man();
m.run();
m.work();
// 2.
InterfaceJDK8.inAddr();
}
}
class Man implements InterfaceJDK8 {
@Override
public void work() {
System.out.println("");
}
}
interface InterfaceJDK8 {
//
void work();
// a.
//
default void run() {
go();
System.out.println("");
}
// b.
//
static void inAddr() {
System.out.println("");
}
// c.: JDK 1.9
//
private void go() {
System.out.println("");
}
}
```
***
####
| **** | **** | **** |
| ------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
| | | jdk8 |
| | **extends** | **implements** |
| | | |
| Java | Java | |
| | **public****protected** **default** | **public** |
| main | main | jdk8 main jdk8 default static main |
| | | |
| | | |
| | | |
------
###
####
* >
```java
= new ;
= new ;
```
* ****
* ****
*
*
*
* ****
* ****
*
```java
public class PolymorphicDemo {
public static void main(String[] args) {
Animal c = new Cat();
c.run();
//c.eat();//
go(c);
go(new Dog);
}
// DogCat Anima
public static void go(Animal d){}
}
class Dog extends Animal{}
class Cat extends Animal{
public void eat();
@Override
public void run(){}
}
class Animal{
public void run(){}
}
```
***
####
>
>
>1.
>2.
****
****
- ** (upcasting)**
- ** (downcasting)**
```java
public class PolymorphicDemo {
public static void main(String[] args){
Animal a = new Cat(); //
Cat c = (Cat)a; //
}
}
class Animal{}
class Cat extends Animal{}
```
***
#### instanceof
instanceof
*
* ** = ()()**
* / ClassCastException
```java
public class Demo{
public static void main(String[] args){
Aniaml a = new Dog();
//Dog d = (Dog)a;
//Cat c = (Cat)a; ClassCastException
if(a instanceof Cat){
Cat c = (Cat)a;
} else if(a instanceof Dog) {
Dog d = (Dog)a;
}
}
}
class Dog extends Animal{}
class Cat extends Animal{}
class Animal{}
```
***
###
####
****
****
***
####
static
*
*
*
.
. = new .
* ?
* ?
```java
public class Demo{
public static void main(String[] args){
Outter.Inner in = new Outter.Inner();
}
}
static class Outter{
public static int age;
private double salary;
public static class Inner{
//
System.out.println(age);
//System.out.println(salary);
}
}
```
***
####
static
.
. = new .new
* `Outter.Inner in = new Outter().new Inner()`
****
*
*
***
####
for
```java
public class InnerClass{
public static void main(String[] args){
String name;
class{}
}
public static void test(){
class Animal{}
class Cat extends Animal{}
}
}
```
***
####
```java
new ||(){
//
}
```
*
*
* ** new **
* ****JVM
```java
public class Anonymity {
public static void main(String[] args) {
Animal a = new Animal(){
@Override
public void run() {
System.out.println("~~");
//System.out.println(n);
}
};
a.run();
a.go();
}
}
abstract class Animal{
public abstract void run();
public void go(){
System.out.println("go~~~");
}
}
```
***
###
**private -> -> protected - > public **
| | private | | protected | public |
| ------------------ | :-----: | :--: | :-------: | :----: |
| | | | | |
| | X | | | |
| | X | | | |
| | X | X | | |
| | X | X | X | |
protected
* protected
* protected protected
***
###
####
```java
static {
}
```
*
* static
*
*
*
* ** main **
```java
public class CodeDemo {
public static String schoolName ;
public static ArrayList lists = new ArrayList();
// ,!
static {
System.out.println("~~~~~~~");
//
schoolName = "";
lists.add("3");
lists.add("4");
lists.add("5");
}
public static void main(String[] args) {
System.out.println("main");
System.out.println(schoolName);
System.out.println(lists);
}
}
/*~~~~~~~
main
[3, 4, 5] */
```
***
####
```java
{
}
```
*
* static
*
*
*
```java
public class CodeDemo {
private String name;
private ArrayList lists = new ArrayList();
{
name = "";
lists.add("java");
System.out.println("~~~~~~~~");
}
public CodeDemo02(){ }//
public CodeDemo02(String name){}
public static void main(String[] args) {
CodeDemo c = new CodeDemo();//
System.out.println(c.name);
System.out.println(c.lists);
new CodeDemo02();//
}
}
```
***
## API
### Object
####
Object Java Object Object Object
Object
* `public String toString()`@Student@735b478
* toString() toString()
* toString() toString
* `public boolean equals(Object o)`
* `protected Object clone()`
```java
public boolean equals(Object o) {
// 1.true
if (this == o) return true;
// 2.null ,
if (o == null || this.getClass() != o.getClass()) return false;
// 3.o
Student student = (Student) o;
return age == student.age &&
sex == student.sex &&
Objects.equals(name, student.name);
}
```
****== equals
* == ****
* Object **** equals **** java.lang.Object
hashCode
* hashCode HashtableHashMap
* equals(java.lang.Object) hashCode
*
***
####
Object clone() protected clone() clone()
* (shallowCopy)****
**Java **Object.clone()System.arraycopy()Arrays.copyOf()
* (deepCopy)
Cloneable clone clone() Cloneable
* Clone & Copy`Student s = new Student`
`Student s1 = s` copy references s1 Object
`Student s2 = s.clone()` Student s
* Shallow Clone & Deep Clone
Object clone() copy
* StringInteger ****
*

```java
public class Student implements Cloneable{
private String name;
private Integer age;
private Date date;
@Override
protected Object clone() throws CloneNotSupportedException {
Student s = (Student) super.clone();
s.date = (Date) date.clone();
return s;
}
//.....
}
```
SDP
***
### Objects
Objects Object
Objects
* `public static boolean equals(Object a, Object b)`
```java
public static boolean equals(Object a, Object b) {
//
return a == b || a != null && a.equals(b);
}
```
* `public static boolean isNull(Object obj)` null null true
* `public static String toString()`
* `public static String toString(, )`
```java
public class ObjectsDemo {
public static void main(String[] args) {
Student s1 = null;
Student s2 = new Student();
System.out.println(Objects.equals(s1 , s2));//
// System.out.println(s1.equals(s2)); //
System.out.println(Objects.isNull(s1));
System.out.println(s1 == null);//
}
}
public class Student {
}
```
***
### String
####
String final **Integer **
```java
public final class String implements java.io.Serializable, Comparable, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
}
```
Java 9 String byte `coder`
value final value String value ** String **
****
```java
String s = "abc";
s = s + "cd"; //s = abccd
```
****
####
API
* `public boolean equals(String s)`
* `public boolean equalsIgnoreCase(String anotherString)`
* `public int length()`
* `public String trim()`
* `public String[] split(String regex)`
* `public char charAt(int index)`
* `public char[] toCharArray()`
* `public boolean startsWith(String prefix)`
* `public int indexOf(String str)` -1
* `public int lastIndexOf(String str)` str -1
* `public String substring(int beginIndex)`
* `public String substring(int i, int j)` j - 1 j - i
* `public String toLowerCase()` String
* `public String toUpperCase()` String
* `public String replace(CharSequence target, CharSequence replacement)`
```java
String s = 123-78;
s.replace("-","");//12378
```
***
####
* `public String()`
* `public String(char[] chs)`
* `public String(String original)`
`String s = "abc"` abc
- new new ****
- `" "` JVM ** String Pool **
`String str = new String("abc")`
* abc
* abc equals()
[]
`new String("a") + new String("b")`
* 1new StringBuilder()
* 2new String("a") 3 a
* 4new String("b") 5 b
[]
* StringBuilder toString()
```java
@Override
public String toString() {
return new String(value, 0, count);
}
```
* 6new String("ab")
* StringBuilder toString() ** ab**new String("ab") ab String
***
#### String Pool
#####
String Pool / StringTable / literal strings Java ****
* StringTable HashTable `-XX:StringTableSize` JDK 1.8 60013
*
* **** StringBuilder#appendappend JDK 1.8
* ****
* String intern() String Pool
***
##### intern()
JDK 1.8 intern() String Pool
* String Pool
* **** Pool
JDK 1.6
```java
public class Demo {
// a b abjava
// ldc #2 a "a" ldc:
// ldc #3 b "b"
// ldc #4 ab "ab"
public static void main(String[] args) {
String s1 = "a"; //
String s2 = "b";
String s3 = "ab"; //
String s4 = s1 + s2; //
// new StringBuilder().append("a").append("b").toString() new String("ab")
String s5 = "a" + "b"; // javac ab
System.out.println(s3 == s4); // false
System.out.println(s3 == s5); // true
String x2 = new String("c") + new String("d"); // new String("cd")
// new cd toString()
x2.intern();
String x1 = "cd";
System.out.println(x1 == x2); //true
}
}
```
- ==
- ==
```java
String s1 = "ab"; //
String s2 = new String("a") + new String("b"); //
//
String s = new String("ab");
```
****
#####
```java
public static void main(String[] args) {
String s = new String("a") + new String("b");//new String("ab")
//"ab"
String s2 = s.intern();
//jdk6"ab"
//jdk8"ab", new String("ab")
System.out.println(s2 == "ab");//jdk6:true jdk8:true
System.out.println(s == "ab");//jdk6:false jdk8:true
}
```
```java
public static void main(String[] args) {
String str1 = new StringBuilder("58").append("tongcheng").toString();
System.out.println(str1 == str1.intern());//true
String str2 = new StringBuilder("ja").append("va").toString();
System.out.println(str2 == str2.intern());//false
}
```
* System Version Version
```java
private static void initializeSystemClass() {
sun.misc.Version.init();
}
```
* Version launcher_name `"java"`
```java
package sun.misc;
public class Version {
private static final String launcher_name = "java";
private static final String java_version = "1.8.0_221";
private static final String java_runtime_name = "Java(TM) SE Runtime Environment";
private static final String java_profile_name = "";
private static final String java_runtime_version = "1.8.0_221-b11";
//...
}
```
***
#####
Java 7 String Pool Java 7 String Pool OutOfMemoryError
StringTable
* `-Xmx10m` 10m
* JDK8 `-Xmx10m -XX:-UseGCOverheadLimit` Run Configurations VM options
* JDK6 `-XX:MaxPermSize=10m`
```java
public static void main(String[] args) throws InterruptedException {
List list = new ArrayList();
int i = 0;
try {
for (int j = 0; j < 260000; j++) {
list.add(String.valueOf(j).intern());
i++;
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
System.out.println(i);
}
}
```

***
####
* -XX:StringTableSize=
* intern
```java
/**
* intern
* -XX:StringTableSize=200000 -XX:+PrintStringTableStatistics
* -Xsx500m -Xmx500m -XX:+PrintStringTableStatistics -XX:StringTableSize=200000
*/
public class Demo1_25 {
public static void main(String[] args) throws IOException {
List address = new ArrayList();
System.in.read();
for (int i = 0; i < 10; i++) {
//
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("linux.words"), "utf-8"))) {
String line = null;
long start = System.nanoTime();
while (true) {
line = reader.readLine();
if(line == null) {
break;
}
address.add(line.intern());
}
System.out.println("cost:" +(System.nanoTime()-start)/1000000);
}
}
System.in.read();
}
}
```
***
####
* hash String HashMap key hash
* String Pool String String Pool String String Pool
* String String String String String
* String
* String API
***
### StringBuilder
String StringBuffer StringBuilder
* String : ****
* StringBuffer : **** synchronized
* StringBuilder : ****JDK5.0
char[]
* `public StringBuilder()`
* `public StringBuilder(String str)`
API :
* `public StringBuilder append()`
* `public StringBuilder reverse()`
* `public String toString()` toString() StringBuilder String
```java
String str = "abc";
char data[] = {'a', 'b', 'c'};
StringBuffer sb1 = new StringBuffer();//new byte[16]
sb1.append('a'); //value[0] = 'a';
```
append
```java
public AbstractStringBuilder append(String str) {
if (str == null) return appendNull();
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
private void ensureCapacityInternal(int minimumCapacity) {
// char
if (minimumCapacity - value.length > 0) {
//int newCapacity = (value.length yyyyMMdd HH:mm:ss EEE a"
```java
public static void main(String[] args){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss);
String time = sdf.format(date);
System.out.println(time);//2020-10-18 19:58:34
//121s
long time = date.getTime();
time+=121;
System.out.println(sdf.formate(time));
String d = "2020-10-18 20:20:20";//
Date newDate = sdf.parse(d);
System.out.println(sdf.format(newDate)); //
}
```
****
### Calendar
Calendar
Calendar `Calendar rightNow = Calendar.getInstance()`****
Calendar
* `public static Calendar getInstance()`
* `public int get(int field)`
* `public void set(int field,int value)`
* `public void add(int field,int amount)`/
* `public final Date getTime()`
* `public long getTimeInMillis()`
```java
public static void main(String[] args){
Calendar rightNow = Calendar.getInsance();
int year = rightNow.get(Calendar.YEAR);//
int month = rightNow.get(Calendar.MONTH) + 1;//+1
int days = rightNow.get(Calendar.DAY_OF_YEAR);
rightNow.set(Calendar.YEAR , 2099);//
rightNow.add(Calendar.HOUR , 15);//15 -1515
Date date = rightNow.getTime();//
long time = rightNow.getTimeInMillis();//
//700
rightNow.add(Calendar.DAY_OF_YEAR , 701);
Date date d = rightNow.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(d));//700
}
```
***
### LocalDateTime
JDK1.8
+ LocalDate
+ LocalTime
+ LocalDateTime +
* public static LocalDateTime now()
* public static LocalDateTime of(, , , , , )
API
| | |
| --------------------------------------------------------- | ------------------------------------------------------------ |
| public int getYear() | |
| public int getMonthValue() | 1-12 |
| public int getDayOfMonth() | 1-31 |
| public int getDayOfYear() | 1-366 |
| public DayOfWeek getDayOfWeek() | |
| public int getMinute() | |
| public int getHour() | |
| public LocalDate toLocalDate() | LocalDate |
| public LocalTime toLocalTime() | LocalTime |
| public String format() | LocalDateTime |
| public LocalDateTime parse(, ) | LocalDateTime |
| public static DateTimeFormatter ofPattern(String pattern) | DateTimeFormatter |
```java
public class JDK8DateDemo2 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
System.out.println(localDateTime);
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
String s = localDateTime.format(pattern);
LocalDateTime parse = LocalDateTime.parse(s, pattern);
}
}
```
| | |
| ------------------------------------------- | -------------- |
| public LocalDateTime plusYears (long years) | |
| public LocalDateTime withYear(int year) | |
**** Duration API
| | |
| ------------------------------------------------ | -------------------- |
| public static Period between(,) | " |
| public int getYears() | |
| public int getMonths() | |
| public int getDays() | |
| public long toTotalMonths() | |
| public static Durationbetween(,) | " |
| public long toSeconds() | |
| public long toMillis() | |
| public long toNanos() | |
```java
public class JDK8DateDemo9 {
public static void main(String[] args) {
LocalDate localDate1 = LocalDate.of(2020, 1, 1);
LocalDate localDate2 = LocalDate.of(2048, 12, 12);
Period period = Period.between(localDate1, localDate2);
System.out.println(period);//P28Y11M11D
Duration duration = Duration.between(localDateTime1, localDateTime2);
System.out.println(duration);//PT21H57M58S
}
}
```
***
### Math
Math
Math
| | |
| -------------------------------------------- | --------------------------------- |
| public static int abs(int a) | a |
| public static double ceil(double a) | |
| public static double floor(double a) | |
| public static double pow(double a, double b) | a b |
| public static long round(double a) | |
| public static int max(int a,int b) | |
| public static int min(int a,int b) | |
| public static double random() | double [0.0,1.0) |
```java
public class MathDemo {
public static void main(String[] args) {
// 1.:
System.out.println(Math.abs(10));
System.out.println(Math.abs(-10.3));
// 2.: 5
System.out.println(Math.ceil(4.00000001)); // 5.0
System.out.println(Math.ceil(-4.00000001));//4.0
// 3.4
System.out.println(Math.floor(4.99999999)); // 4.0
System.out.println(Math.floor(-4.99999999)); // 5.0
// 4.
System.out.println(Math.pow(2 , 3)); // 2^3 = 8.0
// 5. 10
System.out.println(Math.round(4.49999)); // 4
System.out.println(Math.round(4.500001)); // 5
System.out.println(Math.round(5.5));//6
}
}
```
****
### DecimalFormat
```java
public static void main(String[]args){
double pi = 3.1415927;//
//
System.out.println(new DecimalFormat("0").format(pi));//3
//
System.out.println(new DecimalFormat("0.00").format(pi));//3.14
//0
System.out.println(new DecimalFormat("00.000").format(pi));// 03.142
//
System.out.println(new DecimalFormat("#").format(pi));//3
//
System.out.println(new DecimalFormat("#.##%").format(pi));//314.16%
long c =299792458;//
//
System.out.println(new DecimalFormat("#.#####E0").format(c));//2.99792E8
//
System.out.println(new DecimalFormat("00.####E0").format(c));//29.9792E7
//
System.out.println(new DecimalFormat(",###").format(c));//299,792,458
//
System.out.println(new DecimalFormat(",###").format(c));
}
```
***
### BigDecimal
Java java.math API 16
* `public static BigDecimal valueOf(double val)`
* `public BigDecimal(double val)`
* `public BigDecimal(String val)`
API
* `public BigDecimal add(BigDecimal value)`
* `public BigDecimal subtract(BigDecimal value)`
* `public BigDecimal multiply(BigDecimal value)`
* `public BigDecimal divide(BigDecimal value)`
* `public double doubleValue()` BigDecimal double
* `public int intValue()` int
* `public BigDecimal divide (BigDecimal value)`
```java
public class BigDecimalDemo {
public static void main(String[] args) {
// + - * /
System.out.println(0.1 + 0.2);
System.out.println(1.301 / 100);
double a = 0.1 ;
double b = 0.2 ;
double c = a + b ;
System.out.println(c);//0.30000000000000004
// 1.
BigDecimal a1 = BigDecimal.valueOf(a);
BigDecimal b1 = BigDecimal.valueOf(b);
BigDecimal c1 = a1.add(b1);//a1.divide(b1);
System.out.println(c1);
// BigDecimaldouble
double d = c1.doubleValue();
}
}
```
1. BigDecimal
2. BigDecimal
3. divide
```java
BigDecimal divide = bd1.divide(,,);
//1BigDecimal
//2
//3
// BigDecimal.ROUND_UP
// BigDecimal.ROUND_FLOOR
// BigDecimal.ROUND_HALF_UP
```
***
### Regex
####
qq
```java
public static boolean checkQQRegex(String qq){
return qq!=null && qq.matches("\\d{4,}");// 4
}// \\d \ \t \n
```
java.util.regex
- Pattern
Pattern Pattern Pattern Pattern
- Matcher
Matcher Pattern Matcher Pattern matcher Matcher
- PatternSyntaxException
PatternSyntaxException
***
####
#####
****
***
#####
\r\n Windows Unix/Linux \n
| | |
| ------ | ------------------------------------------------------------ |
| \ | |
| \f | |
| \n | |
| \r | |
| \t | |
| \\ | \ |
| () | () |
***
#####
****
| | |
| ------ | ------------------------------------------------------------ |
| . | \n [\s\S] |
| \d | 0~9 [0-9] |
| \D | [ ^0-9] |
| \w | [a-zA-Z_0-9_] |
| \W | \w[ ^\w] |
| \s | [\f\n\r\t\v] |
| \S | \s |
\x \0 \xA 10 ASCII \n
***
#####
[ ] ****
| | |
| ------------ | ----------------------------------------- |
| [ab5@] | "a" "b" "5" "@" |
| [^abc] | "a","b","c" |
| [f-k] | "f"~"k" |
| [^A-F0-3] | "A","F","0"~"3" |
| [a-d[m-p]] | a d m p[a-dm-p] |
| [a-z&&[m-p]] | a z m p[a-dm-p] |
| [^] | |
* ^,- \
*
[\d. \ -+] +-
***
#####
* ( )\* +
* ?
| | |
| ------ | --------------------------------- |
| X? | X {0,1} |
| X* | X {0,} |
| X+ | X {1,} |
| X{n} | X n |
| {n,} | X n |
| {n,m} | X n m |
***
####
#####
| | |
| ------ | ------------------------------------------------------------ |
| ^ | |
| $ | |
| \b | |
***
#####
`((A)(B(C)))`((A)(B(C)))(A)(B(C))(C) group(1)...
* matcher groupCount int matcher
* group(0)group() groupCount
| | |
| ------------------------- | ------------------------------------------------------------ |
| \| () | "" |
| () () | (1) | Web Proxy Viewer | New URL | Original Page |