###### tags: `programing note`
# Java筆記
## 函數庫
import java.util.Scanner;
import java.util.Arrays;
## 先備知識點
### 建構式
類別 物件變數 = new 類別(參數)
### 類別class
class:Ex:
```
public class App {
public static void main(String[] args) throws Exception {
amount netincome = new amount(25000, 24999);
System.out.println(netincome.income - netincome.expenses);
}
}
class amount {
int income;
int expenses;
amount(int a, int b) {
this.income = a;
this.expenses = b;
}
}
```
## 資料型態
### 變數類型
```
int 32位元
short 16位元
long 64位元
float 小數點後6位
double 小數點後16位
char 字元
String 字串
StringBuilder 可變字串 //StringBuilder variable = new StringBuilder(variable or sc.nextLine());
StringBuffer 可變字串 //StringBuffer variable = new StringBuffer(variable or sc.nextLine());
boolean 布林值 //只有true和false
```
### 修飾子
#### final
final 不可再改(加在資料型態前面)
Ex:final int aaa = 7;
#### private
private 只有那個class可以使用
#### public
public 所有class都可以用
#### static
static 將變數轉換成class
### 轉換變數型態
高階轉低階:前面要加(int)之類的
Ex:
`double d = 10.1; int i = (int) d;`
## 輸入輸出
### 輸入
建構式:Scanner sc = new Scanner(System.in); //要import
```
取得數值(除了字元char) = sc.nextInt();
//Int, Float, Double, Line, Boolean
取得字元
char c = sc.next().charAt(0);
關閉Scanner
sc.close();
判斷是否在輸入
sc.hasNext();
```
### 輸出
```
無換行
System.out.print();
換行
System.out.println();
```
## 陣列
建構式:int[][][] variable = new int[][][]; //三維
```
charAt() 第幾項
length 取得長度 Ex. variable = variable.length();
length() 取得字串長度
size() 取得集合物件相關大小
Arrays.sort (variable) 陣列由小到大排列 //要import
equals (Array1, Array2) 比較是否相同 //要import //回傳為boolean
```
## 運算子
### 算數運算子
+ 加
- 減
* 乘
/ 除
% 取餘數
### 比較運算子
```
> 大於
>= 大於或等於
== 等於
<= 小於或等於
< 小於
```
### 邏輯運算子
```
&& 且
|| 或
! 為否
```
### 位元運算子
```
~ 位元補數(complement)運算
& 位元和(and)運算
| 位元或(or)運算
^ 位元互斥或(exclusive or)運算
```
## 判斷式
### if-else
A成立就執行A
由上而下判斷
```
if (a == 1) {
System.out.println("Hello");
} else if (a == 2){
System.out.println("Bye");
} else {
System.out.println("See you");
}
```
### switch
一定要加break
case成立就執行
```
switch (a) {
case 1:
System.out.println("Hello");
break;
case 2:
System.out.println("Bye");
break;
default:
System.out.println("See you");
}
```
### 三元運算式
```
c = a > b ? 30 : 40; // a>b→c=30,!(a>b)→c=40
```
## 迴圈
### while
```
while (a>0) {
System.out.println();
a *= -1;
}
//do-while:先執行一遍(while條件後要加;)
```
### for
```
for (int i = 0; i < 10; i++) {
System.out.println();
}
```
## Math
```
Math.round(5.4); // 四捨五入
Math.ceil(5.4); // 無條件進位
Math.floor(5.4); // 無條件捨去
System.out.println(String.format("%.2f", 1.0)); // 保留小數位數
Math.pow // 次方
Math.sqrt // 平方根
```
## Integer
> Integer是int的包裝類;int是基本數據類型
Integer變量必須實例化後才能使用;int變量不需要
Integer實際是對象的引用,指向此new的Integer對象;int是直接存儲數據值
Integer的默認值是null;int的默認值是0
```
Integer.MAX_VALUE // 2^31-1
Integer.MIN_VALUE // -2^31
```
[https://kknews.cc/code/gmzv5rm.html](https://kknews.cc/code/gmzv5rm.html)
### 回傳值
```
byteValue() byte 以 byte 類型返回該 Integer 的值
shortValue() short 以 short 類型返回該 Integer 的值
intValue() int 以 int 類型返回該 Integer 的值
toString() String 以 String 類型返回一個表示該 Integer
```
### 比較
```
equals(Object obj) return boolean 比較此對象與指定對象是否相等
compareTo(Integeranotherlnteger) return int 如調用對象的數值小於 anotherlnteger 的數值,則返回負值;
```
## 大數運算
### BigInteger
```
variable.add(variable); // +
variable.subtract(variable); // -
variable.multiply(variable); // *
variable.divide(variable); // /
variable.remainder(variable); // %
BigInteger.valueOf(variable); // 指派數值
variable.isProbablePrime(1); // 判斷質數
```
### BigDecimal
```
```
## StringBuffer/StringBuilder
```
StringBuilder variable = new StringBuilder(length); // 建構
var.append(char c/String str); // 將指定的字符串追加到此字符序列
var.insert(int offset, char c/String str); // 將指定的字符串插入到此字符序列
var.delete(int start, int end); // 移除此序列的子字符串中的字符
var.reverse(); // 反轉字符序列
var.replace(int start, int end, String str); //
int var = var.length(); //
```
## 動態陣列
### ArrayList
```
```
### Vector
```
```
## 其他
### 比較字元
```
Character.compare(A,B) //return 0 equal
//return A-B else
```
### 二進位轉十進位
```
Integer.valueOf(字串,2).toString();
```