流程控制,JVM内存"/>
简单聊一下流程控制,JVM内存
1.流程控制
java中非常出名的就有判断语句和选择语句
判断语句最出名的就有:
1.if else 语句
if(boolean表达式){执行代码;
}else {执行代码;
}
2.if... else if... else...
if(boolean表达式){执行代码;
}else if(boolean表达式){执行代码;
}else if(boolean表达式){执行代码;
}else {执行代码;
}
3.小练习
public class Test1 {public static void main(String[] args) {int score=99;if(score<0||score>100){System.out.println("成绩有误");}else if(score>=90&&score<=100){System.out.println("优秀");}else if(score>=80&&score<90){System.out.println("好");}else if(score>=70&&score<80){System.out.println("良");}else if(score>=60&&score<70){System.out.println("及格");}else if(score<60){System.out.println("不及格");}}
}
2.选择语句
switch(a){case 1:代码;break;case 2:代码;break;default:代码;
}
小练习
public class Test8 {public static void main(String[] args) {for (int i=1;i<=12;i++){System.out.println(i+":"+getXingZuo(i));}}public static String getXingZuo(int i){String xingzuo="";switch (i){case 1:xingzuo="水瓶";break;case 2:xingzuo="双鱼";break;case 3:xingzuo="白羊";break;case 4:xingzuo="金牛";break;case 5:xingzuo="双子";break;case 6:xingzuo="巨蟹";break;case 7:xingzuo="狮子";break;case 8:xingzuo="处女";break;case 9:xingzuo="天秤";break;case 10:xingzuo="天蝎";break;case 11:xingzuo="射手";break;case 12:xingzuo="摩羯";break;}return xingzuo;}
}
注意,case是可以合并的
public class Test2 {public static void main(String[] args) {int season = 3;switch (season){case 1: case 2: case 3:System.out.println("爆竹声中一岁除,春风送暖入屠苏");break;case 4: case 5: case 6:System.out.println("接天莲叶无穷碧,映日荷花别样红");break;case 7: case 8: case 9:System.out.println("塞下秋来风景异,衡阳雁去无留意");break;case 10: case 11: case 12:System.out.println("窗含西岭千秋雪,门泊东吴万里船");break;}}
}
case还是可以穿透的,如果上一个case没有写break跳出循环,会一直执行一下要么遇到break,要么选择语句结束。
int num=1;String str;switch (num){case 1:str="选择1";case 2:str="选择2";break;default:str="这是另外的价钱";}System.out.println(str);
输出结果是:选择2;遇到break结束的;
int num=1;String str;switch (num){case 1:str="选择1";case 2:str="选择2";default:str="这是另外的价钱";}System.out.println(str);
这个时候都没有break,我们可以想一想它会输出什么?会不会从头开始运算?
它的输出为:这是另外的价钱;
default默认自带break,当没有找到符合的条件时,会跳到default。
2.循环语句
1.while语句
当符合条件时彩云运行循环体里面的代码;
while(boolean表达式){循环体的代码;
}
2.do...while语句
不管符不符合都会执行一次循环体里面的代码
do{循环体的代码;
}while(boolean表达式)
3.for循环
for(初始值;boolean表达式;增量表达式){循环语句
}
break:跳出最外面的循环,不管嵌套了多少层循环与判断;
continue:跳出本次循环
for(int i=0;i<5;i++){if(i==3){continue;}System.out.println(i);
}
控制台打印0,1,2,4
3.JVM内存
jvm的内存划分为:程序计数器(记录下一条JVM指令的内存地址),本地方法栈,方法区,堆内存,栈内存。
方法区加载类信息,首先会去找类的信息,像方法名等等,再会去寻找mian主方法,还有其他的方法。
堆内存:只要是通过new创建的,都会存储在堆内存里面;
栈内存(虚拟机栈,VM栈):所有方法调用时,都会进栈执行。比如main方法运行时,就会进如方法栈中执行。栈内存是线程私有的。
更多推荐
简单聊一下流程控制,JVM内存
发布评论