基础数据类型(四类八种 ) 不能为null。
整数型 byte 取值范围2的8次方
short 取值范围2的16次方
int 取值范围2的32次方 一般用int
long 取值范围2的64次方
浮点型 :浮点型记录小数点的数据类型,一般用double。
float 4个字节
double 8个字节
布尔型
boolean(true false)

字符型
char(可以是一个字母,也可以是一个汉字)
基础数据类型全部存到栈空间,所以不能为空。

引用类型:String s="abc",
所有的类、数组、接口
运算符 字符串连接字符串需要注意的地方:在输出的时候,只要有一个参数是字符串,整个输出结果都是字符串。
异或运算符:转换成二进制的形式来对比每一位数,不一样的为1,一样的为0;

类型转换的优先级:double float long int chcar short byte

char short byte进行运算的时候,取值默认为int

隐式转换(低--》高) 显示转换(高--》低)
例如:int a=5;b=3.4;
a+=b a的值为8; 用+=赋值的时候有一个隐式的自动转换

byte a=(byte)200 这个为显示转换 也成称为强制转换。

public class textJava3{
public static void main(String[] args){
int a = 3;
int c = 5 - (a--); 2
boolean b = a == c; true
b = b && (a < c--) ? false : true; a=2,c=2 true c赋值后自减1
int d = b ? 7 : 9; 7 int e = d - 3; 4 (*错了)
c *= 3; 3 int f = ((++e == c) ? 25 : 35) + (++a); 38 System.out.println("f的值:" + f);
}

  

class text7
{
public static void main(String[] args)
{ int a, b, c;
a = c = 8; // a=8,c=8
System.out.println("a的值"+a);
System.out.println("c的值"+c);
b = a++; // b=8,a=9,c=8
System.out.println("b的值"+b);
System.out.println("a的值"+a);
System.out.println("c的值"+c);
short d = 3; // d=3
System.out.println("d的值"+d);
long e1 = d++ + a; // e1=12
System.out.println("e1的值"+e1);
long e2 = d++ + a++; // e2=13
System.out.println("e2的值"+e2);
boolean b1 = false;
boolean b2 = !b1; // true
System.out.println("b2的值"+b2);
float g = b2 ? a : b; // 10
System.out.println("g的值"+g); double h = g -= 1; // 9.0
System.out.println("h的值"+h);
boolean b3 = g == 10; // false
System.out.println("b3的值"+b3);
char s = b3 ? 'a' : 'b'; // b
System.out.println("s的值"+s);
int i = 2 * s; // 196 这里s=b b代表的是编码值 *错了
System.out.println("i的值"+i);

  

前加加 后加加 前减减 后减减 即使在外边加了括号 不会影响运算顺序
前加加:将一个整型变量进行前加加,程序先将变量加一,然后在进行计算;
后加加:将一个整型变量进行后加加,程序会先进行就算,计算完以后,再将变量加一

前减减:将一个整型变量进行前减减,程序会先将变量值减一,在进行计算;
后减减:将一个整型变量进行后减减,程序会先进行计算,计算完以后再将变量减一

分支和循环
if(){
}

if(){
}else{
}

if(){
}else if(){
}

if(){
}else if(){
}else

switch(a){
case 1
...
break

case 2
...
break

case 3
...
break
}

if和switch的区别

循环
for( int i=0; i<10; i++){
...
}

while(循环条件){
}

do{
}while(); 这个循环至少循环一次。

if 跟switch的区别
if
1、对具体的值进行判断
2、对区间判断。
3、对运算结果是布尔类型的表达式进行判断
switch
对具体的值进行判断
值得个数通常是固定的
对于几个固定的值判断建议使用switch语句,因为switch语句会将具体的答案一次性都加载到内存,效率相对高

如果判断的具体数值不多,而且符合byte、short、int、char这四种类型。虽然两个语句都可以使用,建议使用switch语句。因为效率稍高
其他情况:对区间判断,对结果为boolean类型判断,使用if,if的使用范围更广。

import java.util.Scanner;
class siji
{
public static void main(String[] args)
{ Scanner s=new Scanner(System.in);
System.out.println("请输入月份。。");
String str=s.nextLine();
int month=Integer.parseInt(str);
/*if(month>=2&&month<=4){
System.out.println("春天了");
}else if(month>=5&&month<=7){
System.out.println("注意防暑");
}else if(month>=8&&month<=10){
System.out.println("秋风瑟瑟");
}else{
System.out.println("雪花飘飘");
}*/ switch(month){ case 2:
case 3:
case 4:
System.out.println("春天了");
break;
case 5:
case 6:
case 7:
System.out.println("注意防暑");
break;
case 8:
case 9:
case 10:
System.out.println("秋风瑟瑟");
break;
case 11:
case 12:
case 1:
System.out.println("雪花飘飘");} }
}

  在选择分支较多时,选用switch...case结构会提高程序的效率,但switch不足的地方在于只能处理字符或者数字类型的变量,if...else结构更加灵活一些,if...else结构可以用于判断表达式是否成立,比如if(a+b>c),if...else的应用范围更广,switch...case结构在某些情况下可以替代if...else结构。

for循环与while循环,当不能明确要循环的次数的时候选择while循环。

class chengfabiao{
public static void main(String[] args){
for( int i=0; i<10; i++){
for(int j=0; j<i; j++){
System.out.print("*");}
System.out.println("*");}

  

class chengfabiao{
public static void main(String[] args){
int i=0; //初始化条件
while (i<100){ //循环条件
System.out.println(i); //循环体
i++;//迭代条件
}}
}

  

if else 与switch case判断的更多相关文章

  1. 使用反射+策略模式代替项目中大量的switch case判断

    我这里的业务场景是根据消息类型将离线消息存入mongoDB不同的collection中.其中就涉及到大量的分支判断,为了增强代码的可读性和可维护性,对之前的代码进行了重构. 先对比一下使用反射+策略模 ...

  2. js switch case 判断的是绝对相对===,值和类型都要相等

    js switch case 判断的是绝对相对===,值和类型都要相等

  3. switch case 判断是否为按钮、设置属性 Load Foreach 绑定事件

    private void button9_Click(object sender, EventArgs e) { foreach (Control CT in this.Controls) {//判断 ...

  4. 利用switch case判断是今天的第多少天

    static void Main(string[] args)        {            while (true)            {                int m1 ...

  5. 为什么说在使用多条件判断时switch case语句比if语句效率高?

    在学习JavaScript中的if控制语句和switch控制语句的时候,提到了使用多条件判断时switch case语句比if语句效率高,但是身为小白的我并没有在代码中看出有什么不同.去度娘找了半个小 ...

  6. switch...case和if...else if的判断应用

    判断成绩所属等级的 两种方法 1...      switch...case方法: #include<stdio.h> int main(void) { ;i <= ;++i) // ...

  7. if判断和switch case 和三元运算符整理

    if判断和switch case 和三元运算符整理 例子1:if判断写法: <script type="text/javascript"> var num = 12; ...

  8. Thymeleaf常用语法:条件判断 if、switch case

    if语句条件判断使用th:if,它会判断表达式是否成立,表达式的结果支持boolean.number.character.String及其他类型.满足下面情况,if语句成立:(1) 表达式的结果是数字 ...

  9. switch case加范围判断

    switch case是可以加范围判断的,但是语法有少许变化,参数不能写在switch里面,而是写在外面,如: const i = 3; switch (true) { case (i <= 0 ...

随机推荐

  1. How to use data analysis for machine learning (example, part 1)

    In my last article, I stated that for practitioners (as opposed to theorists), the real prerequisite ...

  2. HashSet集合

    HashSet特点 1.无序,不允许重复(无序指元素顺序与添加顺序不一致,每次遍历出来的位置不是恒久不变的) 2.HashSet通过调用hashCode()和equals方法来剔除重复 3.HashS ...

  3. [Leetcode] Binary tree level order traversal二叉树层次遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  4. docker dead but pid file exists 问题

    You may have to enable the public_ol6_latest repo in order to get this package. sudo yum-config-mana ...

  5. Asp.Net Core-----简介与安装

    Asp.Net Core简介 ASP.NET Core 是一个全新的开源.跨平台框架,可以用它来构建基于网络连接的现代云应用程序,比如:Web 应用,IoT(Internet Of Things,物联 ...

  6. java之真假分页

    真分页(要的是什么范围的记录在数据库查的时候就只查这几条记录):select s.* from (select *,row_number() over(order by SLoginId) as ro ...

  7. 我的第一个jQuery插件--表格隔行变色

    虽然网上有大量的插件供我们去使用,但不一定有一款适合你的,必要的时候还是要自己动手去敲的.下面,开始我的第一个插件... 参考<锋利的JQuery>,JQuery为开发插件增设了俩个方法: ...

  8. java入门学习笔记之2(Java中的字符串操作)

    因为对Python很熟悉,看着Java的各种字符串操作就不自觉的代入Python的实现方法上,于是就将Java实现方式与Python实现方式都写下来了. 先说一下总结,Java的字符串类String本 ...

  9. 如何用phpcms将静态网页生成动态网页?

    在前两篇随笔中已经简单介绍了phpcms,那么现在让我们来看一下如何用phpcms将静态网页生成动态网页? 1.在templates文件夹下新建模板文件夹ceshi(名字可以自己随笔起) 2.在ces ...

  10. 配置IIS让网站可以播放mp4文件

    最近遇到这么一个问题,网站当中的mp4不能播放了--每次点击播放的时候都会产生404的错误(如下图).这个问题来得有些蹊跷,因为在这台服务器上其他的文件都能正常执行,比如xml.jpg.aspx等文件 ...