一、注釋
1.單行注釋
//開頭
// 單行注釋
注釋一行
2.多行注釋
/* 注釋 */
/*
多行注釋
多行注釋
多行注釋
多行注釋
*/
注釋多行
3.檔案注釋
/**開頭 */結尾
/**
* 檔案注釋
*
*/
/***
* _ooOoo_
* o8888888o
* 88" . "88
* (| -_- |)
* O\ = /O
* ____/`---'\____
* . ' \\| |// `.
* / \\||| : |||// \
* / _||||| -:- |||||- \
* | | \\\ - /// | |
* | \_| ''\---/'' | |
* \ .-\__ `-` ___/-. /
* ___`. .' /--.--\ `. . __
* ."" '< `.___\_<|>_/___.' >'"".
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
* \ \ `-. \_ __\ /__ _/ .-` / /
* ======`-.____`-.___\_____/___.-`____.-'======
* `=---='
*
* .............................................
* 佛祖保佑 永無BUG
*/
百度:有趣的代碼注釋
4.JavaDoc
javaDoc命令是用來生成自己API檔案的
引數資訊
@author 作者名
@version 版本號
@since 指明需要jak版本
@param 引數名
@return 回傳值情況
@throws 例外拋出情況
public class Dome09 {
/**
* @author mzq
* @version 1.0
* @since 1.1
*/
String name;
/**
*
* @param mzq
* @return
* @throws Exception
*/
public String test(String name) throws Exception{
return name;
}
}
生成Javados檔案


進入cmd

輸入指令:javadoc -encoding UTF-8 -charset UTF-8 Dome09.java
-encoding UTF-8 -charset UTF-8 防止亂碼

會生成一個index.html檔案,點開即可


IDEA生成Javadoc
二、識別符號
Java所有的組成部分都需要名字,類名、變數名以及方法名都被稱為識別符號,
1. 關鍵字
2.識別符號注意點
- 所有識別符號都應該以字母(A-Z或者a-z),美元符($)、或者下劃線(_)開始
- 首字符之后可以是字母(A-Z或者a-z),美元符($)、下劃線(_)或數字的任何字符組合
- 不能使用關鍵字作為變數名或方法名,
- 識別符號是大小寫敏感的
- 合法識別符號舉例:age,$salary,_value,__1_value
- 非法識別符號舉例:123abc,-salary,#abc
public class HelloWord {
public static void main(String[] args) {
String 123abc ="an";//錯誤寫法
System.out.println("123abc");
}
}
- 可以使用中文命名,但是不建議這樣去做,也不建議使用拼音,因為很low
三、資料型別
強型別語言
要求變數的使用要嚴格符合規定,所有變數都必須先定義后才能使用
弱型別語言
變數不必符合規定
Java的資料型別分為兩大類
1.基本型別(primitive type)
-
數值型別:
整數型別
byte占1個位元組,范圍:-128~127
short占2個位元組,范圍:-32768~32767
int占4個位元組,范圍:-2147483648~2147483647
long占8個位元組,范圍:-9223372036854775808~9223372036854775807
浮點型別
float占4個位元組
double占8個位元組
字符型別
char占2個位元組 -
boolean型別:
占1位其值只有true和false兩個
public static void main(String[] args) { //整數 int num1 =20; byte num2 =30; short num3 =50; long num4 =20L;//long型別在后面要加L //小數:浮點數 float num5 = 5.1F;//float型別在后面要加F double num6 = 7.45; //字符 char name = 'A';//單引號 //字串,String不是關鍵字,類 //String namea = "張三" //布林值 boolean flag = true; boolean flag1 = false; }
2.參考型別(reference type)
除基本型別外的所有型別
資料型別拓展
重要
public class Dome02 {
public static void main(String[] args) {
//整數拓展: 進制 二進制0b 十進制 八進制0 十六進制0x
int i = 10;
int i1 = 010; //八進制
int i2 = 0x10;//十六進制
//===============================================
//浮點數擴展 銀行業務表示
// BigDecimal 數學工具類
// ===============================================
//float 有限 離散 舍入誤差 大約 接近但不等于
//double
//最好完全使用浮點數進行比較 少用浮點數進行比較 最好用BigDecimal
float a = 0.1f; //0.1
double b = 1.0/10; //0.1
System.out.println(a==b);//false
float a1=855598655f;
float a2=a1+1;
System.out.println(a1==a2);//true
System.out.println("===============================================");
//===============================================
//字符拓展
// ===============================================
System.out.println("===============================================");
char c1 = 'a';
char c2 ='無';
System.out.println(c1);
System.out.println((int) c1);//強制轉換
System.out.println(c2);
System.out.println((int) c2);//強制轉換
//所有字符本質還是數字
//編碼 Unicode表:(97 = a 65=A) 2位元組 0-65532(2的16次方)
//U0000 UFFF 區間范圍
char c3 = '\u0061'; // 斜桿u表示轉換
System.out.println(c3); //a
//轉義字符
//\t 制表符
//\n 換行 還有很多
System.out.println("hello\tword");
System.out.println("===============================================");
//布林值擴展
boolean flag = true;
if (flag==true){} //新手
if (flag){} //老手
}
}
四、型別轉換
Java是一種強型別語言,運算時需要一些型別轉換

運算中,不同型別需要轉換為同一型別
public class Dome03 {
public static void main(String[] args) {
int i =128;
byte b= (byte) i;//強制轉換 (型別)變數名 高---低
//自動轉換 低---高
System.out.println(i);//128
System.out.println(b);//-128 原因:記憶體溢位
int i1 =128;
double b1= i;
System.out.println(i1);
System.out.println(b1);
/*
* 1.不能對布林值進行轉換
* 2.不能把物件型別轉換為不相干型別
* 3.把高容量轉換到低容量的時候,強制轉換
* 4.轉換的時候存在記憶體溢位,或存在精度問題
* */
System.out.println("==========");
System.out.println((int)66.7); //66
System.out.println((int)-66.87f);//-66
System.out.println("==========");
char c = 'a';
int d = c+1;
System.out.println(d);//98
System.out.println((char) d);//b
}
}
注意問題:
public class Dome04 {
public static void main(String[] args) {
//操作比較大的數的時候,注意溢位問題
//JDK新特性,數字之間可以用下劃線分割
int money = 10_0000_0000;
int years = 20;
int b =money*years;//-1474836480 計算時候溢位了
System.out.println(b); //-1474836480
long b1=money*years;//默認是int 轉換之前已經存在問題
System.out.println(b1); //-1474836480
long b2 = money*((long)years);//先把一個數轉化為long型別
System.out.println(b2);//20000000000
}
}
五、變數
-
變數:可以變化的量; java是一種強型別語言,每個變數必須宣告其型別,
-
Java變數是程式中最基本的存盤單元,其要素包括變數名,變數型別和作用域,
type varName [=value] [{,varName=[=value]}]; //資料型別 變數名=值 可以使用逗號隔開來宣告多個同型別變數 -
注意事項:
(1)每個變數都有型別,型別可以是基本型別,也可以是參考型別,
(2)變數名必須是合法的識別符號,
(3)變數宣告是一條完整的陳述句,因此每一個宣告都必須以分號結束,
-
變數作用域:類變數,實體變數,區域變數
public class Dome05 {
//定義屬性:變數
//類變數static
static double salary=2500;
//實體變數:從屬于物件,在Dome05這個類里面使用
//如果不進行初始化,他會變成這個型別默認值
//布林值默認是false
//除了基本型別,其他默認都是null
String name;
int age;
//main方法
public static void main(String[] args) {
//區域變數(在方法里,只在括號里有效):必須宣告初始化值
int i = 10;
System.out.println(i);
//變數型別 變數名字= new Dome05()
Dome05 dome05 = new Dome05();
System.out.println(dome05.age);//0
System.out.println(dome05.name);//null
System.out.println(salary);//列印類變數
}
//其他方法
public void sdd() {
}
}
- 變數命名規范
(1) 所有變數、方法、類名:見名知意
(2)類成員變數:首字母小寫和駝峰原則:monthSalary 除第一個單詞以外,后面單詞首字母大寫
(3)區域變數:首字母小寫和駝峰原則
(4)常量:大寫字母和下劃線 MAX_VALUE
(5)類名:首字母大寫和駝峰原則 Man GoodMan
(6)方法名:首字母小寫和駝峰原則 run(), runRun()
六、常量
-
常量(Constant):初始化后不能再改變值!不會變動的值;
-
常量可以理解成一種特殊的變數,他的值被設定后,在程式運行程序中不允許改變,
//final 常量名=值; //final double PI=3.14; public class Dome06 { //修飾符,不存在先后順序 static final double PI=3.14; public static void main(String[] args) { System.out.println(PI); } }
七、基本運算子
算數運算子:+,-,*,%(求余),++,–
賦值運算子:=
關系運算子:>,<,>=,<=,==(等于),!=instanceof
public class Dome01 {
public static void main(String[] args) {
//二元運算子
//ctrl+D 復制當前行到下一行
int a=11;
int b=18;
int c=20;
int d=41;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
}
}
public class Dome02 {
public static void main(String[] args) {
long a=8986874879L;
int b = 12;
short c= 11;
byte d = 8;
//有一個long型別,結果為long型別,其余為int型別
System.out.println(a+b+c+d);
System.out.println(b+c+d);
System.out.println(c+d);
System.out.println((double) c+d);//轉換為double型別
}
}
public class Dome03 {
public static void main(String[] args) {
//關系運算子回傳的結果:正確或錯誤 布林值
int a = 10;
int b = 15;
int c = 23;
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
System.out.println(c%a);//取余 模運算
}
}
++ 冥運算
public class Dome04 {
public static void main(String[] args) {
//++ -- 自增 自減 一元運算子
int a = 7;
int b = a++;//a++=a+1 執行這行代碼后加1
System.out.println(a);
int c = ++a;//執行這行代碼前先自增1
System.out.println(a);
System.out.println(b);
System.out.println(c);
//冥運算 2^3 2*2*2=8 很多運算,我們需要一些工具類來操作
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
邏輯運算子:&&,||,!
//邏輯運算
public class Dome05 {
public static void main(String[] args) {
//與(and) 或(or) 非(取反)
boolean a=true;
boolean b=false;
System.out.println("a&&b:"+(a&&b));//邏輯與運算,兩個變數都為真,結果才為true
System.out.println("a||b:"+(a||b));//邏輯或運算,兩個變數一個為真,則結果為true
System.out.println("!(a&&b):"+!(a&&b));//如果是真,則為假,如果是假,則為真
//短路運算
int c =5;
boolean d = (c<4)&&(c++<6);//(c<4)為假,不會執行(c++<6)
System.out.println(d);
System.out.println(c);
}
}
位運算
public class Dome06 {
public static void main(String[] args) {
/*
* A=0011 1100
* B=0000 1101
* A&B = 0000 1100
* A|B = 0011 1101
* A^B = 0011 0001
* ~B = 1111 0010 與B完全相反
*
* 2*8 2*2*2*2
* 效率極高
* << 左移 *2
* >> 又移 /2
* 0000 0000 0
* 0000 0001 1
* 0000 0010 2
* 0000 0011 3
* 0000 0100 4
* 0000 1000 8
* 0001 0000 16
*
* */
System.out.println(2<<3);
}
}
擴展運算子
public class Dome07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;//a=a+b
//a-=b;//a=a-b
System.out.println(a);
//字串連接 +
System.out.println(""+a+b);//字串在前,后面是連接符
System.out.println(a+b+"");//a+b在前,先進行計算
}
}
三元運算子
public class Dome08 {
public static void main(String[] args) {
//x?y:z
//如果x為真,則結果為y,否則為
int score = 80;
String type = score<60?"不及格":"及格";
System.out.println(type);
}
}
學習視頻地址
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/259720.html
標籤:java
