復(fù)習(xí)
jvm : 虛擬機(jī) --> sandbox
jre : jvm + 核心類庫
jdk : jre + 工具,javac java
path: 操作系統(tǒng)搜索路徑
classpath: java搜索類的目錄順序java -classpath d:\java;d:;.
-
java程序制作過程
1) 編寫java源文件記事本notepad
editplus
2) 編譯源文件,生成class文件(字節(jié)碼文件)
3) 運(yùn)行程序class文件java 類名(在classpath下找)
java -classpath | -cp 類名
java關(guān)鍵字
-
數(shù)據(jù)類型
- class //類
- interface //接口
- byte //字節(jié) bit:位
- short //短整型 2 byte
- int //整型 4 bytes
- long //長整型 8 bytes
- float //浮點(diǎn)型
- double //雙精度浮點(diǎn).
- char //字符
- boolean //布爾 false true
- void //空
- null //空,不存在
-
流程控制
- if //如果
- else //否則
- switch //分支
- case //情況.
- default //默認(rèn),缺省
- while //當(dāng).. 循環(huán)
- do //
- for //for 循環(huán)
- break //中斷
- continue //繼續(xù)
- return //返回
-
訪問權(quán)控制
private //私有的
proected //受保護(hù)的
public //公共的
-
類,函數(shù)修飾符
abstract //抽象
final //最終的
static //靜態(tài)的
synchronized//同步
extends //擴(kuò)展
implements //實(shí)現(xiàn)
new //新建
this //當(dāng)前
super //超類,父類,
instanceof //是否是xxx實(shí)例
try //嘗試
catch //捕獲
finally //最終
throw //拋出
throws //定義類聲明時(shí),拋出異常
package //包
import //導(dǎo)入 export
native //本地的
transient //瞬時(shí)的,暫時(shí)的,臨時(shí)的 persistent
asset //斷言
-
標(biāo)識符
- 26個英文字母
- 數(shù)字0-9
- _ + $
規(guī)范
java區(qū)分大小寫
包名: aaa.bbb.ccc.ddd,全都小寫
類: helloWorld --> HelloWorld,
變量和函數(shù): convertStringToInt --> convertString2Int
常量大寫: final String BRAND ="benz" ;
Demo: 演示作用
java的負(fù)數(shù)采用補(bǔ)碼方式存儲:取反 + 1 。
-
java中基本數(shù)據(jù)類型:primitive
- 默認(rèn)整數(shù)是int類型,默認(rèn)浮點(diǎn)數(shù)是double類型.
- 升格自動轉(zhuǎn)換。隱式轉(zhuǎn)換。
- 降格強(qiáng)制轉(zhuǎn)換。顯式轉(zhuǎn)換。
注釋
-
單行注釋
//xxxxxxxx -
多行注釋
/* * 多行注釋 */ -
文檔注釋: 類頂端
/** * 文檔注釋 */
存儲范圍換算
2^10 = 1024 = 1K
2^20 = 1024K = 1M
2^30 = 1024M = 1G
2^40 = 1024G = 1T
進(jìn)制
十進(jìn)制:0~9,(123,456)
二進(jìn)制:0,1
八進(jìn)制:0~7,以0開頭(076)
十六進(jìn)制:0~F,以0x開頭(0x12,0xfe)
定權(quán)重:位高權(quán)重
進(jìn)制轉(zhuǎn)換
[十進(jìn)制轉(zhuǎn)二進(jìn)制]
使用除法
2 | 13 ........ 1
2 | 6 ......... 0
2 | 3 ......... 1
2 | 1 ......... 1
---> 1101
內(nèi)存中數(shù)據(jù)存儲形式
-
byte: 2^8 = 256, -128~127
3 + (-3) = 0000 0011 + 1000 0011 ------------- = 1000 0110 = -6 -
負(fù)數(shù)的表現(xiàn)形式: 補(bǔ)碼(正數(shù)取反+1)
124 = 0111 1100 | -124 = 1000 0100 125 = 0111 1101 | -125 = 1000 0011 126 = 0111 1110 | -126 = 1000 0010 127 = 0111 1111 | -127 = 1000 0001 | -128 = 1000 0000
判斷結(jié)構(gòu)
if(exp){
....
}if(exp1){
...
}
else{
...
}if(exp1){
...
}
else if(exp2){
...
}
...
else{
...
}
switch
支持的類型: byte short int char
循環(huán)語句
while(exp){
....
}//至少執(zhí)行一次.
do{
...
}while(exp)
function
main() function
Modifier : public static void
一段獨(dú)立程序
修飾符 返回值 函數(shù)名(參數(shù)類型1 參數(shù)名稱1,參數(shù)類型2 參數(shù)名稱2,...){
函數(shù)body
return ;//只有void類型的返回值才可以不寫return.
}
變量的使用范圍
僅在所處的{}中有效,也稱之為生命,下面程序編譯時(shí)會報(bào)錯:
/**
*這是我的類,演示版。
*/
class CommentDemo{
/*
* 程序的入口點(diǎn)。args是參數(shù)。
*/
public static void main(String[] args){
{
int age = 20;
}
// 輸出helloworld
System.out.println("hello world" + age);
}
}
強(qiáng)類型語言
javascript----弱類型語言,var
java-----強(qiáng)類型
int age = 20;
String name = "xxxx";
java中基本數(shù)據(jù)類型
byte, 1個字節(jié)
short, 2個字節(jié)
int, 4個字節(jié)
long, 8個字節(jié)
float, 4個字節(jié)
double, 8個字節(jié)
boolean, 1個字節(jié)
char, 兩個字節(jié)
整數(shù)默認(rèn):int,小數(shù)默認(rèn):double
引用類型:
class
interface
[], 數(shù)組
特殊字符
'\t' //tab
'\r' //return
'\n' //new line
所有的byte、short、char 都將提升到int型
運(yùn)算符
/**
*這是我的類,演示版。
*/
class CalcDemo{
/*
* 程序的入口點(diǎn)。args是參數(shù)。
*/
public static void main(String[] args){
int i = +5;
int j = -5;
System.out.println("i = " + i + ",j = " + j);
System.out.println("i = " + (+i) + ",j = " + (-j));
// + - * /
System.out.println(5 + 5);
System.out.println(5 - 5);
System.out.println(5 * 5);
System.out.println("==="+ ((float)5 / 3 * 3));
//取模
System.out.println(5 % 3); //求余
// ++ --
int a = 5, b = 5;
System.out.println("a = "+(a++)); //
System.out.println("b = "+ (++b)); //
// + 操作符重載
System.out.println("hello"+ "world"); //
System.out.println("5 + 5 = "+ (5 + 5)); //
//賦值 = += -= *= /= %=
int a1 = 5;
System.out.println("a1 += "+ (a1+=3)); //
short s = 2;
s = (short)(s+2);
s += 2;
System.out.println("s = "+ s); //
//比較運(yùn)算符 == <> !=
boolean res = 5<= 5;
System.out.println(5<=5);
System.out.println(res);
System.out.println(5!=5);
Object o = "hello" ;
//instanceof 判斷對象是否是指定的類型,后面跟的是引用類型
System.out.println(o instanceof String);
System.out.println(o instanceof Object);
//System.out.println(o instanceof int);//報(bào)錯
System.out.println(o instanceof Class);
//邏輯運(yùn)算符
// && || 存在短路操作
int c1 = 8, c2 = 3;
if(c1>5 && c2>5){
System.out.println("Ok");
}
else{
System.out.println("no ok");
}
if(c1>5 || c2>5){
System.out.println("Ok");
}
else{
System.out.println("no ok");
}
// & | ! ^ 不存在短路
if((c1 > 5) & (c2>5)){
System.out.println("Ok");
}
else{
System.out.println("no ok");
}
//System.out.println(5 && 6);//報(bào)錯
System.out.println(true && false);
// 5 0000 0101
// 6 0000 0110
// ------------
// 0000 0100 = 4
System.out.println(5 & 6);//結(jié)果為4,按位與
System.out.println(5 | 6);//結(jié)果為7,按位或
//System.out.println(!6);//報(bào)錯
//對boolean類型取反
System.out.println(!true);
boolean b0 = 3>4;
System.out.println(!b0);
//按位取反 ~
// 0000 0000 0000 0000 0000 0000 0000 0101
// 1111 1111 1111 1111 1111 1111 1111 1010
// 0000 0000 0000 0000 0000 0000 0000 0110
System.out.println(~5);
//異或 ^,按位異或,相同為假,不同為真
// 0...0 0101 = 5
//^0...0 1100 = 12
//------
// 0...0 1001 = 9
System.out.println(5^12);
//移位運(yùn)算
// << 有符號左移
// >> 有符號右移
// 0000 1010 = 10
// 0010 10 00 = 40
System.out.println("10 << 2 = " + (10<<2));
System.out.println("6 << 2 = " + (6<<2));
// 0000 0110 = 6
// 0000 0001 = 1
System.out.println("6 >> 2 = " + (6>>2));
// -6 1111 1010
// -6 << 2 1110 1000 = -24
// 0001 1000
System.out.println("-6 << 2 = " + (-6<<2));
// -6 1111 1010
// -6 >> 2 1111 1110 = -2
// 0000 0010
System.out.println("-6 >> 2 = " + (-6>>2));
// >>> 無符號右移,空出的位用0填充
// -6 1111 1010
// -6 >>> 2 0011 1110 =
System.out.println("-6 >>> 2 = " + (-6>>>2));
System.out.println("-1 >> 3 = " + (-1>>3));
System.out.println("-1 >>> 3 = " + (-1>>>3));
// 三元運(yùn)算符 ? : ;
int i1 = 10;
if(i1 < 5){
System.out.println("<5");
}
else{
System.out.println(">=5");
}
System.out.println(i1 < 5 ? "<5" : ">=5");
int x = 5, y = 6, z = 7;
System.out.println(x>y? x : y);
//三元運(yùn)算符的嵌套
System.out.println(x>y? (x>z? x : z) : (y>z? y:z));
}
}
流控制
/**
* 流程控制
*/
class FlowControl
{
public static void main(String[] args){
//1. if
int a = 10;
if(a>5){
System.out.println("a is big");
}
//2. if - else,非此即彼
if(a<5){
System.out.println("a is small");
}
else{
System.out.println("a is big");
}
System.out.println("over");
//3. if - else if - else
// month 1-12
int m = 100;
if(m<=3 && m >=1){
System.out.println(m+"是春季");
}
else if (4<=m && m<=6){
System.out.println(m+"是夏季");
}
else if (7<=m && m<= 9){
System.out.println(m+"是秋季");
}
else if(m>=10 && m<=12){
System.out.println(m+"是冬季");
}
else{
System.out.println(m+"是無效的月份");
}
//4.另一種寫法
if(m<=0 || m>=13){
System.out.println(m+"是無效的月份");
}
else if(m<=3){
System.out.println(m+"是春季");
}
else if(m<=6){
System.out.println(m+"是夏季");
}
else if (m<=9){
System.out.println(m+"是秋季");
}
else{
System.out.println(m+"是冬季");
}
//5. switch語句
int b = 2;
switch (b){
case 1:
case 2:
case 3:
System.out.println("small");
break;
case 4:
System.out.println("midlle");
//break;
case 5:
System.out.println("big");
break;
default:
System.out.println("??");
break;
}
//6.利用switch判斷月份
int n = 11;
switch (n){
case 1:
case 2:
case 3:
System.out.println(n+"是春季");
break;
case 4:
case 5:
case 6:
System.out.println(n+"是夏季");
break;
case 7:
case 8:
case 9:
System.out.println(n+"是秋季");
break;
case 10:
case 11:
case 12:
System.out.println(n+"是冬季");
break;
default:
System.out.println(n+"是非法月份");
break;
}
//7. 簡單寫法
int c = 12;
if (c<1 || c>12){
System.out.println("非法月份");
return;
}
int season = (c-1)/3;
switch(season){
case 0:
System.out.println("春");
break;
case 1:
System.out.println("夏");
break;
case 2:
System.out.println("秋");
break;
default:
System.out.println("冬");
break;
}
//8. while循環(huán)
int i = 0;
while(true){
System.out.println(i);
i ++ ;
if(i>=10){
break;
}
}
System.out.println("--------");
//9. do...while循環(huán)
i = 0;
do{
System.out.println(i);
i ++ ;
if(i>=10){
break;
}
}while(i>1);
//10.打印直角三角形
int k = 10;
int k1 = 1;
while(k1<=10){
//輸出整個一行
int k2 = 1;
while(k2<=k1){
System.out.print("*");
k2++;
}
System.out.println();
k1++;
}
//11. 99表格
int line = 9;
int row = 1;
//行循環(huán)
while(row<=9){
//列循環(huán)
int col = 1;
while(col <=row){
System.out.print(col + "x" + row + "=" + (col * row)+"\t");
col ++;
}
row ++;
System.out.println();
}
//12. for 循環(huán)
for(i = 0;i<=10;i++){
System.out.println(i);
}
//13. 前n項(xiàng)和
int sum = 0;
for(i = 0;i<=100;i++){
sum = sum +i;
}
System.out.println(sum);
}
}
[思考]
負(fù)數(shù)的表現(xiàn)形式,為什么如此設(shè)計(jì)?
-128是在內(nèi)存中是如何存儲的?推算過程?
如果字節(jié)表示為11111111,則其真實(shí)數(shù)據(jù)是多少?
正負(fù)數(shù)是否都支持補(bǔ)碼的方式?
正數(shù)是否是負(fù)數(shù)的減一取反?
空心三角形
倒三角空心
[定義函數(shù)]
add // +
subtract // -
multiple // *
divide // /
支持float,doule,long,int。//函數(shù)重載.