public class HelloWorld {
public static void main(String[] args){
//字符使用单引号
char param = '中'
System.out.println(param);
}
}
2.5 变量
变量就是java中一个最基本的单元;包含4个属性:
变量的类型:基本类型或引用类型都可以
变量名称:符合标识符即可
变量的值:如果是基本类型就是具体的值,如果是引用类型就是内存地址
变量的存储单元:根据不同的类型决定在内存中开辟空间的大小(1byte=8位)
创建一个变量
public class Demo {
public static void main(String[] args){
//创建了一个变量
int i = 10;
}
}
2.6 数据类型
2.6.1 整型
分为:byte、short、int、long
表示的方式:
十进制:1,2,3...
八进制:以0开头;012
十六进制:以0x开头;0x123
在java中整型的默认类型就是int
如果使用长整型:10L(大小写均可)
2.6.2 浮点
浮点类型:单精度float;双精度double
java中默认精度类型是:double
案例:
正确创建单精度
public class Demo {
public static void main(String[] args){
//创建单精度;大小写均可
float f = 2.0f;
float f1 = 2.0F;
}
}
笔试题:float和long谁大?
答:float大,因为,数字的大小不是按照字节数比较的
2.6.3 布尔类型
boolean类型:true;flase,只能为这两个值,不能取其他的值
案例:
public class Demo {
public static void main(String[] args){
//创建布尔类型
boolean bo = true;
boolean bo1 = false;
}
}
错误的
2.6.4 基本类型的转换
在java中基本类型可以转换,但是,boolean类型比较特殊不能与其他类型转换;
类型转换分为:
默认转换:小类型转大类型
强制转换:大类型转小类型
类型由小到大转换:
byte->short->int->long->float->double
注意:byte、short、char之间不会相互转换,首先转为int后再转换
注意:只要不超出范围,整型可以直接赋值给byte、short、char
总结:多种类型混合运算的过程中,返回最大的类型
案例:
public class Demo {
public static void main(String[] args){
//创建参数
byte b = 100;
//byte a = 1000; 错误,超出范围
//short b = 1000; 错误,变量不能同名
short d = 1000; //正确
int c = 1000; //正确
long e = c; //正确;因为int比long小,默认就可以转换了
//int f = e; //错误;因为long比int大,超出范围了,存在精度丢失
int f = (int)e; //正确;可以进行强制类型转换
}
}
案例2:
public class HelloWorld {
public static void main(String[] args){
int i = 10/3; //正确的
long n = 10; //正确的
//int f = n/3; //错误;返回的是long类型
int f = (int)n/3; //正确,可以把long强制转为int类型
//byte h = (byte)n/3; //错误;返回的是int类型
byte h = (byte)(n/3); //正确
}
}
案例3:
public class HelloWorld{
public static void main(String[] args){
short k = 10;
short k1 = k+1; //错误,因为需要转为int后在运算
}
}
案例4:
public class HelloWorld{
public static void main(String[] args){
//正确:char转为int后在运算
char f = 'a';
int n = f+100;
System.out.println(n);
}
}
public class Demo{
public static void main(String[] args){
//创建变量
int i = 10;
i++; //自身加一
System.out.println(i);
}
}
案例2:
public class Demo{
public static void main(String[] args){
//创建变量
int i = 10;
//++在后表示先赋值,自身再加一
int n = i++;
System.out.println(i);
System.out.println(n);
}
}
案例3:
public class Demo{
public static void main(String[] args){
//创建变量
int i = 10;
//++在前表示自身先加一在赋值
int n = ++i;
System.out.println(i);
System.out.println(n);
}
}
案例4:
public class Demo{
public static void main(String[] args){
//创建变量
int i = 10;
//i++相等于i=i+1
i = i+1;
System.out.println(i);
}
}
案例5:
public class Demo {
public static void main(String[] args){
//创建变量
short i = 10;
//正确;推荐使用 +=、-=,*=,/=、因为,【可以自动转为接收的类型】
i += 1;
System.out.println(i);
}
}
案例6:
public class Demo {
public static void main(String[] args){
//创建变量
int i = 10;
i /= 3;
System.out.println(i);
}
}
案例7:取模
public class Demo {
public static void main(String[] args){
//创建变量
int i = 10;
int n = 1%3; //就是取余数
System.out.println(n);
}
}
使用场景:就是分页的使用
public class Demo {
public static void main(String[] args){
//总记录数
int total = 20;
//每页显示9条
int pageSize = 9;
//总页码
int pages = 1;
if (total%pageSize==0){
pages = total/pageSize;
}else {
pages = total/pageSize+1;
}
System.out.println("总页码="+pages);
}
}
public class TestDemo {
public static void main(String[] args) {
//创建变量
int i = 10;
//三元运算符
int n = i>30?1:-1;
System.out.println(n);
//三元运算符
boolean bo = i%3==0?true:false;
System.out.println(bo);
}
}
public class TestDemo {
public static void main(String[] args){
int Age = 3;
if (Age>0&&Age<=5){
System.out.println("儿童");
}
if (Age>5&&Age<=10){
System.out.println("小学生");
}
if (Age>10&&Age<=18){
System.out.println("中学生");
}
}
}
2.8.1.3 案例:使用if-else if-else 效率高
public class TestDemo {
public static void main(String[] args){
int Age = 3;
if (Age>0&&Age<=5){
System.out.println("儿童");
}else if (Age>5&&Age<=10){
System.out.println("小学生");
}else if (Age>10&&Age<=18){
System.out.println("中学生");
}else{
System.out.println("大学生");
}
}
}
2.8.1.4 案例:两个添加判断if-else
public class TestDemo {
public static void main(String[] args){
int Age = 3;
if (3%2==0){
System.out.println("偶数");
}else {
System.out.println("奇数");
}
}
}
2.8.2 switch分支语句
2.8.2.1 格式
switch(表达式){
case 值:
语句;
break;
case 值:
语句;
break;
case 值:
语句;
break;
default:
语句;
break;
}
表达式:byte、short、int、char、String
break:推荐使用;如果省略,那么从【符合条件的那一行】穿透
如果所有条件都不满足执行default
2.8.2.2 案例
public class TestDemo {
public static void main(String[] args){
int key = 1;
switch(key){
case 0:
System.out.println("0");
break;
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
case 3:
System.out.println("3");
break;
default:
System.out.println("-1");
break;
}
}
}
2.8.2.3 不添加break那么从符合条件的那一行开始穿透
public class TestDemo {
public static void main(String[] args){
int key = 1;
switch(key){
case 0:
System.out.println("0");
case 1:
System.out.println("1");
case 2:
System.out.println("2");
case 3:
System.out.println("3");
default:
System.out.println("-1");
}
}
}
笔试题:
public class TestDemo {
public static void main(String[] args){
int key = 1;
int i = 10;
switch(key){
case 0:
i++;
case 1:
i+=10;
case 2:
i--;
case 3:
i++;
}
System.out.println("i="+i);
}
}
笔试题:输出-1,因为,default与位置无关
public class TestDemo {
public static void main(String[] args){
int key = 10;
switch(key){
default:
System.out.println("-1");
break;
case 0:
System.out.println("0");
break;
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
case 3:
System.out.println("3");
break;
}
}
}
2.8.3 for循环
2.8.3.1 格式
for(初始化;条件;步长){
语句
}
2.8.3.2 案例
public class TestDemo {
public static void main(String[] args){
for(int i=0;i<5;i++){
System.out.println(i);
}
}
}
笔试题:是否可以执行?答:这是一个死循环
public class TestDemo {
public static void main(String[] args){
for(;;){
System.out.println("abc");
}
}
}
循环的顺序
public class TestDemo {
public static void main(String[] args){
for(int i=0;i<5;i++){
System.out.println(i);
}
}
}
等价于
public class TestDemo {
public static void main(String[] args){
int i = 0;
for(;i<5;){
System.out.println(i);
i++;
}
}
}
笔试题:执行错误,因为,i变量属于for的语句块
public class TestDemo {
public static void main(String[] args){
for(int i=0;i<5;i++){
i+=1;
}
System.out.println(i);
}
}
笔试题:书写是否正确?正确
public class TestDemo {
public static void main(String[] args){
for(int i=0;i<5;i++)
System.out.println(i);
}
}
2.8.4 while循环
2.8.4.1 格式
while(布尔类型){
语句;
防止死循环;
}
public class TestDemo {
public static void main(String[] args){
int i=0;
while(i<5)
System.out.println(i);
i++;
}
}
2.8.5 do-while循环
特点:至少循环一次
格式:
public class TestDemo {
public static void main(String[] args){
int i=0;
do{
System.out.println(i);
i++;
}while(i<5); //必须注意结束使用分号
}
}
笔试题:输出5 5
public class TestDemo {
public static void main(String[] args){
int i=10,n=0;
do{
n++;
i--;
}while(n<5);
System.out.println(i);
System.out.println(n);
}
}
2.8.6 跳出循环体
break:跳出当前循环体
continue:跳出本次循环
return:跳出所有循环体,因为,方法弹栈
2.8.6.1 break
public class TestDemo {
public static void main(String[] args) {
for(int i = 0;i < 5;i++)
if(i==2){
break;
}
System.out.println(i);
}
}
}
嵌套循环
public class TestDemo {
public static void main(String[] args) {
for(int n = 0;n < 5;n++){
for(int i = 0; i < 5; i++){
if(i==2){
break;
}
System.out.println(i);
}
System.out.println("-------外层-------");
}
}
}
跳出所有循环
public class TestDemo {
public static void main(String[] args) {
//创建变量
boolean flag = false;
for (int = 0; n < 5; n++) {
if (flag) {//跳出外层
break;
}
for (int i = 0; i < 5; i++) {
if (i==2){
flag = true;
break;
}
System.out.println(i);
}
System.out.println("-------外层-------");
}
}
}
2.8.6.2 continue
public class TestDemo {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
if (i==2){
continue;
}
System.out.println(i);
}
}
}
2.8.6.3 return
public class TestDemo {
public static void main(String[] args) {
for(int n = 0; n < 5; n++) {
for (int i = 0; i < 5; i++) {
if (i==2){
return; //方法弹栈
}
System.out.println(i);
}
System.out.println("-------外层-------");
}
}
}