java基本語法
1.注釋
-
單行注釋//
-
多行注釋/* */
-
檔案注釋/** */
2.識別符號
基本
1.關鍵字

2.所有識別符號都以 字母,_ , $ 開頭
識別符號大小寫敏感
3.整數型 byte short int long
浮點型 float double
字符型 char (字符代表的是一個字,兩個及多個就不對)
字串 string (它不是關鍵字,它是一個類)
布林值 boolean
long num = 90L;
float num1 = 5.1F;
char name = 'a';
string name1 = "容辭";
boolean flag = true;
4.1bit(1位)
? 1Byte(一個位元組)
? 8b = 1B
? 1024B = 1KB
? 1024KB = 1M
? 1024M = 1G
? 1024G = 1TB
5.轉義字符
\t 制表符 \n 換行
關于資料型別面試擴展
1.int
int i = 10;
int i1 = 010;(八進制0)
int i2 = 0b10;(二進制0b)
int i3 = 0x10;(十六進制0x)
2.float/double
float f = 0.1f;
double d = 1.0/10;
System.out.println(f == d); //false
float f1 = 2333333333f;
float f2 = f1+1;
System.out.println(f1 == f2); //true
float 有限 大約 舍入誤差
銀行業務不能用浮點型表示!可以用類BigDecimal
結論:少用浮點型進行比較
3.char
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1); //強制轉換型別
System.out.println(c2);
System.out.println((int)c2); //強制轉換型別
char c3 = '\u0061'; //unicode
System.out.println(c3);
所有字符型本質上都是數字
這里有個unicode編碼 u0000—uFFFF
4.boolean
boolean flag = true;
if (flag == true){} //新手
if (flag){} //老手
//less is more!
3.型別轉換
高低順序:
char,short,byte—>int—>long—>float—>double
int i = 128;
byte b = (byte)i; //強制轉換--->從高到低
System.out.println(i);
System.out.println(b); //輸出b=-128--->記憶體溢位
int i = 128;
double b = i; //自動轉換--->從低到高
System.out.println(i);
System.out.println(b);
System.out.println((int)23.77); //23
System.out.println((int)-45.5789f); //-45
char c = 'a';
int t = c+1;
System.out.println(t); //98
System.out.println((char)t); //b
注意事項:
布爾型不能進行轉換
轉換的時候可能存在記憶體溢位或精度問題
int money = 10_0000_0000; //可在數字中加入下劃線
System.out.println(i); //1000000000
int year = 20;
int total = money*year;
long total1 = money*(long)year;
System.out.println(total); //溢位
System.out.println((long)total); //溢位
System.out.println(total1); //20000000000
4.變數
變數作用域
1.實體變數:它在類內方法外
? 實體變數如果不自行初始化,它的默認值為0 0.0 u0000
? 布林值默認false
? 除了基本型別外其余默認為null
2.類變數(static)
3.區域變數:區域變數必須宣告和初始化值
public class Helloworld {
String name; //實體變數:從屬于物件
int age;
static double salary = 25000; //類變數
public static void main(String[] args) //mian方法
int a = 1; //區域變數
System.out.println(a);
//變數型別+變數名字 = new Helloworld()
Helloworld Helloworld = new Helloworld(); //實體變數
System.out.println(Helloworld.name);
System.out.println(salary); //類變數
}
}
變數的命名規范
1.變數命名: 見名知意
2.類成員變數: 首字母小寫,除了第一個單詞后面單詞首字母大寫(駝峰原則)
3.常量名:全是大寫字母或者下劃線
4.類名:首字母大寫,駝峰原則(新建檔案命名)
5.常量
一種特殊的變數,它的值被設定后便不可以改變!
final 常量名 = 常量值
常量名一般用大寫字符
public class Helloworld {
static final double PI = 3.14; //常量
public static void main(String[] args) {
System.out.println(PI);
}
}
6.基本運算子
ctrl+D 復制當前行到下一行
當使用除法的時候會出現小數一定要注意資料型別,要把整數強制轉化為小數
整數型進行運算的時候,當有long參與,結果為long型;無long參與,結果全為int,(double同理)
與或非 &&,||,!
自增自減 ++ ——
int a =10;
int b = a++; //先給b賦值,然后a在自增
(隱藏了a=a+1)
System.out.println(a); //a=11
(隱藏了a=a+1)
int c = ++a; //a先自增,再給c賦值
System.out.println(a); //a=12
System.out.println(b); //b=10
System.out.println(c); //c=12
冪
double pow = Math.pow(2,3); //Math類
System.out.println(pow); //8.0
邏輯運算子
&& || !
boolean a = true;
boolean b = false;
System.out.println("a&&b:"+(a&&b));
System.out.println("a||b:"+(a||b));
System.out.println("!(a&&b):"+(!(a&&b)));
短路原則:當執行與運算時,如果前面的已經為flase那么根本沒有執行與運算,
int c = 5;
boolean d = (c<=4)&&(c++<=4);
System.out.println(d); //flase
System.out.println(c); //c=5
位運算
& | ~ (非) ^
int a = 0b1000;
int b = 0b1101;
System.out.println(a&b); //與運算 1000 & 1101 = 1000
System.out.println(a|b); //或運算 1101
System.out.println(a^b); //異或運算 0101 相同就取0,不同就取1
<< >>
System.out.println(2<<3); //16 << 代表乘2
System.out.println(8>>2); //2 >> 代表除2
三元運算子
a+=b a=a+b
x ? y : z 如果x==true,則執行y;否則執行z
int score = 80;
String type = score<60?"不及格":"及格";
System.out.println(type); //及格
字串連接符 +
int a = 10;
int b = 20;
System.out.println(""+a+b); //1020
System.out.println(a+b+""); //30
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/281237.html
標籤:java
上一篇:設計模式——策略模式
下一篇:深度決議JVM記憶體模型
