java笔记
day01
一、jdk和idea下载及安装(一般不建议装C盘):
jdk:java开发环境
idea:开发工具(软件),用来编写代码的
苍老师文档服务器:doc.canglaoshi.org
jdk下载:https://repo.huaweicloud.com/java/jdk/
打开cmd窗口(win+r,输入cmd)
依次输入
java
javac
java -version进行测试
配置环境变量(win+s搜索环境变量,点击编辑系统的环境变量)
再次测试javac命令(一定要打开一个新的cmd窗口)
二、基本语法
1、数据类型
(1)基本数据类型:八种
整型:
①byte,取值范围:-128~127
②short,取值范围:-32768~32767
③int(默认),取值范围:-2147483648~2147483647
④long,数字后面需要加后缀L(l)
浮点型:
⑤float,需要加后缀F(f)
⑥double(默认),不需要加后缀
布尔型:
⑦boolean,值:true或者false
字符型:
⑧char,只能存放一个中文汉字或者一个英文字母、整数
当存放的是数字是,底层会基于ascii码表会进行相应的转化,转化成对应的字符
package cn.td.jsd2303;
/**
* 八大基本数据类型
*/
public class Test2 {
public static void main(String[] args) {
//定义变量
//语法格式:定义的类型 变量名 = 值;
//修改变量的值
//语法格式:变量名 = 值;
//整型
byte b1 = 127;//(-128~127)
System.out.println(b1);
b1 = -127;
short s1 = 30000;//(-32768~32767)
int i1 = 210;//(-2147483648~2147483647)
long l1 = 21432934535432L;
System.out.println(b1);
System.out.println(s1);
System.out.println(i1);
System.out.println(l1);
//浮点型
float f1 = 8.9F;
double d1 = 15.6;
System.out.println(f1);
System.out.println(d1);
//布尔型
boolean bl1 = true;
boolean bl2 = false;
System.out.println(bl1);
System.out.println(bl2);
//字符型
char c1 = '北';
char c2 = 'W';
char c3 = 98;
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}
}
2、运算符
(1)赋值:= += -= /= *= %=
package cn.td.jsd2303;
/**
* 赋值运算符:= += -= *= /= %=
*/
public class Test8 {
public static void main(String[] args) {
//定义变量格式:变量的类型 变量名 = 变量的值;
int i1 = 23;
//+=:a+=b <=> a = a + b
int i2 = 15;
// i1 = i1 + i2;
i1+=i2;
System.out.println(i1);
int b1 = 20;
int b2 = 8;
// ctrl + / 注释单行
// b2 = b2 - b1;
b2-=b1;
System.out.println(b2);
double d1 = 2.0;
double d2 = 5.0;
// d1 = d1*d2;
d1*=d2;
System.out.println(d1);
int i3 = 100;
int i4 = 5;
// i3 = i3/i4;
i3/=i4;
System.out.println(i3);
int i5 = -13;
// i5 = i5 % i3;
i5%=i3;
System.out.println(i5);
}
}
(2)逻辑:> < >= <= == !=
package cn.td.jsd2303;
/**
* 逻辑:> < >= <= == !=
*/
public class Test9 {
public static void main(String[] args) {
//定义(声明)多个变量
//语法格式:变量的类型 变量1,变量2,......;
int i1,i2,i3,i4,i5,i6;
boolean b1;
i1 = 15;
i2 = 28;
i3 = -15;
i4 = 19;
i5 = 28;
b1 = i1 > i2;
System.out.println(b1);//false
b1 = i2 < i3;
System.out.println(b1);//false
b1 = i1 >= i4;
System.out.println(b1);//false
b1 = i3 <= i1;
System.out.println(b1);//true
b1 = (i2==i5);
System.out.println(b1);//true
b1 = (i1!=i3);
System.out.println(b1);//true
}
}
package cn.td.jsd2303;
/**
* 超市打折案例:如果购物金额达到200,打九折,达到500,打8折,达到800,打6折
*
* 逻辑运算符 + 分支结构
*/
public class Test10 {
public static void main(String[] args) {
double shopMoney = 850.0;
if(shopMoney >= 800) {
shopMoney = shopMoney * 0.6;
System.out.println("您本次一共消费了:" + shopMoney + "元");
} else if(shopMoney >= 500) {
shopMoney = shopMoney * 0.8;
System.out.println("您本次一共消费了:" + shopMoney + "元");
} else {
shopMoney = shopMoney * 0.9;
System.out.println("您本次一共消费了:" + shopMoney + "元");
}
}
}
(3)&& || !
package cn.td.jsd2303;
/**
* && A&&B,A、B都为true,结果才为true,有一个是false,结果还是false
* || A||B,有一个为true,即为true,如果都为false,才是false
* ! !A,如果A为true,结果则为false,如果A为false,结果则为true
*/
public class Test11 {
public static void main(String[] args) {
int i1,i2,i3,i4;
boolean b1;
i1 = 10;
i2 = -13;
i3 = 28;
i4= 33;
b1 = (i1 > i3) && (i4 > i3);
System.out.println(b1);//false
if(b1) {
System.out.println("result1 is true");
} else if((i1 > i4) || (i2 < i3)) {//true
System.out.println("result2 is true");
}
if(!b1) {
System.out.println("result3 is " + b1);
}
}
}
(4)++ –
package cn.td.jsd2303;
/**
* ++ --
*/
public class Test7 {
public static void main(String[] args) {
int i1 = 10;
//++i1;//等价于 i1=i1 + 1
System.out.println(++i1);//21
System.out.println(i1);//21
int i2 = 20;
//i2++;//等价于 i2=i2 + 1
System.out.println(i2++);//20
System.out.println(i2);//21
int i3 = 51;
int i4 = 28;
int i5 = 0;
i5 = i3+++--i4;
System.out.println(i5);//78
}
}
(5)算术:+ - * / %(取模)
package cn.td.jsd2303;
/**
* 算术:+ - * / %(取模)
*/
public class Test6 {
public static void main(String[] args) {
int i1 = 10;
System.out.println(i1);
i1 = i1 + 100;
System.out.println(i1);
int i2 = 5;
int i3 = 8;
int i4 = 0;
i4 = i3 - i2 ;
System.out.println(i4);
//基本数据类型运算,可能会出现运算结果不精确问题,如下
double d1 = 10;
double d2 = 9.8;
System.out.println(d1-d2);
//% 取模(求余数)
int i5 = 5;
int i6 = 3;
int i7 = 0;
i7 = i5%i6;
System.out.println(i7);
//小取大,得到那个小的值
i5 = -1;
i6 = 10;
i7 = i5 % i6;
System.out.println(i7);
}
}
(6)三目或者三元
package cn.td.jsd2303;
/**
* 三目或者三元
* 格式:a>b?c:d
*
* 例子:找出三个数中的最大值
*/
public class Test12 {
public static void main(String[] args) {
int i1,i2,i3,i4;
i1 = 10;
i2 = 50;
i3 = -100;
if(i1 > i2) {//i1比i2大,拿i1和i3比
if(i1 > i3) {
System.out.println("max value is " + i1);
} else {
System.out.println("max value is " + i3);
}
} else {//i1比i2小,拿i2和i3比
if(i2 > i3) {
System.out.println("max value is " + i2);
} else {
System.out.println("max value is " + i3);
}
}
//上面的方法虽然可行,但麻烦,推荐用三木或者三元运算符
i4 = i1>i2? (i1>i3?i1:i3) : (i2>i3?i2:i3);
System.out.println("max value is " + i4);
}
}
3、流程控制
(1)分支结构
①if()…else
package cn.td.jsd2303;
/**
* if else结构
*/
public class Test3 {
public static void main(String[] args) {
boolean b1 = false;
if(b1) {
System.out.println("this true");
} else {
System.out.println("this false");
}
}
}
②if()…else if()…else
package cn.td.jsd2303;
/**
* if...else if...else
*/
public class Test4 {
public static void main(String[] args) {
boolean b1 = false;
boolean b2 = false;
if(b1) {
System.out.println("this b1 is true");
} else if(b2) {
System.out.println("this b2 is true");
} else {
System.out.println("b1 and b2 is false");
}
}
}
③switch case
package cn.td.jsd2303;
/**
* switch(args) {
* case
* }
*
* args可以是byte、int、short、char、String、Enum六种类型
*/
public class Test5 {
public static void main(String[] args) {
int i1 = 10;
switch(i1) {
case 1:
System.out.println("output 1");
break;//中断、中止
case 2:
System.out.println("output 2");
case 10:
System.out.println("output 10");
case -1:
System.out.println("output -1");
default:
System.out.println("output default");
}
}
}
(2)循环