3.1 使用java运算符
3.1.1 优先级
3.1.2 赋值
3.1.3 算术运算符
3.1.4 自动递增和递减
3.1.5 关系运算符
3.1.6 逻辑运算符
3.1.7 按位运算符
3.1.8 移位运算符
3.1.9 三元if-else运算符
3.1.10 逗号运算符
3.1.11 字串运算符
3.1.12 运算符常规操作规则
3.1.13 造型运算符
3.1.14 java没有”sizeof“
3.1.15 复习计算顺序
3.1.16 运算符总结
3.2 执行控制
3.2.1 真和假
3.2.2 if else
3.2.3 反复
3.2.4 do-while
3.2.5 for
3.2.6 中断和继续
3.2.7 开关
3.1.1 优先级
package com.sunny.hello.c3; public class Demo1 { public static void main(String[] args) { int x = 1, y =1, z = 1; int a = x + y - 2/2 + z; int b = x + (y - 2)/(2 + z); System.out.println("a = " + a + " ,b = "+b); }}
输出:
a = 2 ,b = 1
3.1.2 赋值
package com.sunny.hello.c3; public class Assignment { //理解基本类型和对象赋值的区别 public static void main(String[] args) { Number n1 = new Number(); Number n2 = new Number(); n1.i = 9; n2.i = 47; System.out.println("1:n1.i: " + n1.i + ", n2.i: " + n2.i); n2 = n1; System.out.println("2:n1.i: " + n1.i + ", n2.i: " + n2.i); n1.i = 27; System.out.println("1:n1.i: " + n1.i + ", n2.i: " + n2.i); } } class Number { int i; }
运行结果:
1:n1.i: 9, n2.i: 47 2:n1.i: 9, n2.i: 9 1:n1.i: 27, n2.i: 27
package com.sunny.hello.c3; public class PassObject { static void f(Letter y){ y.c = ‘z‘; } public static void main(String[] args) { Letter x = new Letter(); x.c = ‘a‘; System.out.println("x.c:"+x.c); f(x); System.out.println("x.c:"+x.c); } } class Letter{ char c; }
运行结果:
x.c:a x.c:z
3.1.4 i++与i--
package com.sunny.hello.c3; public class AutoInc { public static void main(String[] args) { int i = 1; System.out.println("i:"+i); System.out.println("++i:" + ++i); System.out.println("i++:" + i++); System.out.println("i:" + i); System.out.println("--i:" + --i); System.out.println("i--:" + i--); System.out.println("i:"+i); } }
运行结果:
i:3 --i:2 i--:2 i:1
3.1.5 关系运算符
package com.sunny.hello.c3; public class Equivalence { public static void main(String[] args) { //检查对象是否相等 Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); //false System.out.println(n1.equals(n2)); //true } }
package com.sunny.hello.c3; public class EqualsMethod { public static void main(String[] args) { Value v1 = new Value(); Value v2 = new Value(); v1.i = 100; v2.i = 100; System.out.println(v1.equals(v2)); //false /* 这里的equals调用的是Object类的 public boolean equals(Object obj) { return (this == obj); }*/ } } class Value{ int i; }
3.1.6 逻辑运算符
package com.sunny.hello.c3; public class ShortCircuit { public static boolean test1(int i){ if(i>1){ System.out.println("test1()执行"); return true; } return false; } public static boolean test2(int i){ if(i%2 == 0){ System.out.println("test2()执行"); return true; } return false; } public static void main(String[] args) { int a = 2, b = 6; if(test1(a) && test2(b)){ System.out.println("未短路"); }else{ System.out.println("短路"); } } }
运行结果:
test1()执行 test2()执行 未短路
若将a的值改为1,则:(注意,test1为false,则test2不会继续执行了,这就是短路)
短路
3.1.8 移位运算符
<<:左移
>> :右移(若值为正,则在高位插入 0;若值为负,则在高位插入 1)
>> :无符号右移(无论正负,都在高位插入0)
package com.sunny.hello.c3; public class URShift { public static void main(String[] args) { int a = 2; System.out.println(a<<2); System.out.println(a>>2); System.out.println(a>>>2); short b = -1; System.out.println(b<<2); System.out.println(b>>2); System.out.println(b>>>2); } }
运行结果:
8 0 0 -4 -1 1073741823
3.1.9 三元运算符
public int ternary(int i){ return i<10 ? i*100 : i*10; }
3.1.15 复习计算顺序
算术运算符>关系运算符>逻辑运算符>赋值
3.1.16 运算符总结
溢出问题:
package com.sunny.hello.c3; public class Overflow { public static void main(String[] args) { int big = Integer.MAX_VALUE; System.out.println(big); big = big * 4; System.out.println(big); } }
输出结果:
2147483647 -4
3.2: 执行控制
break和continue区别:
package com.sunny.hello.c3; public class Demo { public static void main(String[] args) { for(int i=0; i<6; i++){ if(i%2 != 0){ break; }else{ System.out.println(i); } System.out.println(i + "是偶数"); } } }
运行结果:
0 0是偶数
package com.sunny.hello.c3; public class Demo { public static void main(String[] args) { for(int i=0; i<6; i++){ if(i%2 != 0){ continue; }else{ System.out.println(i); } System.out.println(i + "是偶数"); } } }
运行结果:
0 0是偶数 2 2是偶数 4 4是偶数
switch开关:
package com.sunny.hello.c3; public class VowelsAndConsonants { public static void main(String[] args) { for (int i = 0; i < 5; i++){ char c = (char)(Math.random()*26+‘a‘); System.out.println("c:"+c); switch (c){ case ‘a‘: case ‘e‘: case ‘i‘: case ‘o‘: case ‘u‘: System.out.println("vowel"); break; case ‘y‘: case ‘w‘: System.out.println("Sometimes a vowel"); break; default: System.out.println("consonant"); } } } }
运行结果:
c:c consonant c:h consonant c:g consonant c:e vowel c:b consonant
时间: 2024-11-05 18:51:44