板凳----On_Java第四章 操作符

4.1使用Java操作符

4.2优先级

最简单的规则就是先乘除后加减。程序员经常会忘记其他优先级规则然后用括号来明确指定计算顺序。

public class Precedence {
  public static void main(String[] args) {
    int x = 1, y = 2, z = 3;
    int a = x + y - 2/2 + z;           // [1]
    int b = x + (y - 2)/(2 + z);       // [2]
    System.out.println("a = " + a);
    System.out.println("b = " + b);
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ javac Precedence.java
(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java Precedence
a = 5
b = 1

4.3赋值

操作符=用来赋值。它的意思是“取等号右边的值 一般称作右值 把它复制给等号左边 一般称作左值 ”。右值可以是任何常量、变量或者可以产生值的表达式。但左值必须是一个独特的命名变最 也就是说 必须有一个物理空间来存储右值 。

class Tank {
  int level;
}

public class Assignment {
  public static void main(String[] args) {
    Tank t1 = new Tank();
    Tank t2 = new Tank();
    t1.level = 9;
    t2.level = 47;
    System.out.println("1: t1.level: " + t1.level +
      ", t2.level: " + t2.level);
    t1 = t2;
    System.out.println("2: t1.level: " + t1.level +
      ", t2.level: " + t2.level);
    t1.level = 27;
    System.out.println("3: t1.level: " + t1.level +
      ", t2.level: " + t2.level);
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java Assignment
1: t1.level: 9, t2.level: 47
2: t1.level: 47, t2.level: 47
3: t1.level: 27, t2.level: 27
这种现象通常称作“别名” Java操作对象的一种基本方式。不想让别名出现在这里 像下面这样写
t1.level = t2.level
这样就可以保持两个对象彼此独立.而不是丢弃一个对象 然后将tl t2都绑定到剩下的那个对象上。

方法调用中的别名
将一个对象作为参数传递给方法时 也会产生别名:

class Letter {
  char c;
}

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("1: x.c: " + x.c);
    f(x);
    System.out.println("2: x.c: " + x.c);
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java PassObject
1: x.c: a
2: x.c: z

4.4算术操作符

Java的基本算术操作符与其他大多数编程语言相同 包括加法 、减法 - 、除法 / 、乘法 * 和取模 .它从除法中产生余数 0整数除法的结果会舍弃小数位, 而非四舍五入。

import java.util.*;

public class MathOps {
  public static void main(String[] args) {
    // Create a seeded random number generator:
    Random rand = new Random(47);
    int i, j, k;
    // Choose value from 1 to 100:
    j = rand.nextInt(100) + 1;
    System.out.println("j : " + j);
    k = rand.nextInt(100) + 1;
    System.out.println("k : " + k);
    i = j + k;
    System.out.println("j + k : " + i);
    i = j - k;
    System.out.println("j - k : " + i);
    i = k / j;
    System.out.println("k / j : " + i);
    i = k * j;
    System.out.println("k * j : " + i);
    i = k % j;
    System.out.println("k % j : " + i);
    j %= k;
    System.out.println("j %= k : " + j);
    // Floating-point number tests:
    float u, v, w; // Applies to doubles, too
    v = rand.nextFloat();
    System.out.println("v : " + v);
    w = rand.nextFloat();
    System.out.println("w : " + w);
    u = v + w;
    System.out.println("v + w : " + u);
    u = v - w;
    System.out.println("v - w : " + u);
    u = v * w;
    System.out.println("v * w : " + u);
    u = v / w;
    System.out.println("v / w : " + u);
    // The following also works for char,
    // byte, short, int, long, and double:
    u += v;
    System.out.println("u += v : " + u);
    u -= v;
    System.out.println("u -= v : " + u);
    u *= v;
    System.out.println("u *= v : " + u);
    u /= v;
    System.out.println("u /= v : " + u);
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ javac MathOps.java
(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java MathOps
j : 59
k : 56
j + k : 115
j - k : 3
k / j : 0
k * j : 3304
k % j : 56
j %= k : 3
v : 0.5309454
w : 0.0534122
v + w : 0.5843576
v - w : 0.47753322
v * w : 0.028358962
v / w : 9.940527
u += v : 10.471473
u -= v : 9.940527
u *= v : 5.2778773
u /= v : 9.940527


import java.util.Random;

public class Random1{
	public static void  main(String[] args){
		Random r = new Random();

	//	for(int i = 0; i <= 20; i++ ){
	//	int number = r.nextInt(10);
	//	System.out.println("随机生成了: " + number);
	//	}

		for(int i = 0; i <= 20; i++ ){
		int number = r.nextInt(30) +65;
        //      int number = r.nextInt(10, 30);       java 17 中的功能
		System.out.println("随机生成了: " + number);
		}
	}
}

import java.util.Random;
import java.util.Scanner;

public class Random2{
	public static void  main(String[] args){
		Random r = new Random();
		int luckNumber = r.nextInt(100)+1;

		Scanner sc = new Scanner(System.in);
		while(true){
		int guessNumber = sc.nextInt();

		if(guessNumber > luckNumber){
			System.out.println("您猜得数过大+++ ");
		}else if(guessNumber < luckNumber){
			System.out.println("您猜得数过小---- ");
		}else{		
		System.out.println("恭喜恭喜,您猜对了 " );
		break;
		}
	   }
	}
}

4.5自动递增和自动递减

递增和递减操作符都有两种使用方式 通常称为前缀式和后缀式。前缀递增表示+ + 操作符位于变虽之前 后缀递増表示 操作符位于变虽之后。类似地 前缀递减意味着-- 操作符位于变量之前 后缀递减意味着–操作符位于变量之后。对于前缀递增和前缀递比如++a和–a),程序会先执行运算 然后返回生成的结果。而对于后缀递增和后缀 递减 a++” a–),程序则会先返回变星的值 然后再执行运算。


public class AutoInc {
  public static void main(String[] args) {
    int i = 1;
    System.out.println("i: " + i);
    System.out.println("++i: " + ++i); // Pre-increment
    System.out.println("i++: " + i++); // Post-increment
    System.out.println("i: " + i);
    System.out.println("--i: " + --i); // Pre-decrement
    System.out.println("i--: " + i--); // Post-decrement
    System.out.println("i: " + i);
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java AutoInc
i: 1
++i: 2
i++: 2
i: 3
–i: 2
i–: 2
i: 1
对于前缀形式 我们在执行完运算后才得到返回值。但对于后缀形式 则是先获得返回值再执行运算。

4.6关系操作符

public class Equivalence {
  static void show(String desc, Integer n1, Integer n2) {
    System.out.println(desc + ":");
    System.out.printf(
      "%d==%d %b %b%n", n1, n2, n1 == n2, n1.equals(n2));
  }
  @SuppressWarnings("deprecation")
  public static void test(int value) {
    Integer i1 = value;                             // [1]
    Integer i2 = value;
    show("Automatic", i1, i2);
    // Old way, deprecated since Java 9:
    Integer r1 = new Integer(value);                // [2]
    Integer r2 = new Integer(value);
    show("new Integer()", r1, r2);
    // Preferred since Java 9:
    Integer v1 = Integer.valueOf(value);            // [3]
    Integer v2 = Integer.valueOf(value);
    show("Integer.valueOf()", v1, v2);
    // Primitives can't use equals():
    int x = value;                                  // [4]
    int y = value;
    // x.equals(y); // Doesn't compile
    System.out.println("Primitive int:");
    System.out.printf("%d==%d %b%n", x, y, x == y);
  }
  public static void main(String[] args) {
    test(127);
    test(128);
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java Equivalence
Automatic:
127127 true true
new Integer():
127
127 false true
Integer.valueOf():
127127 true true
Primitive int:
127
127 true
Automatic:
128128 false true
new Integer():
128
128 false true
Integer.valueOf():
128128 false true
Primitive int:
128
128 true
show()方法将==的行为和每个对象都有的equals() 方法进行了比较。printf()通过使用指定的符号来対参数逬行格式化处理 %d用于int类型参数的输出 %b用于boolean 类型的输出 %n用于换行。
[1] 自动转换为Integer。这其实是通过对Integer.valueOfO的自动调用来完成的。
[2] 使用标准的对象创建语法new。这是以前创建“包装 / 装箱"Integer对象的首选方法。
[3] Java 9开始,valueOf。优于[2]如果尝试在Java 9中使用方式[2],你将收到警告 ’ 并被建议使用[3]代替。很难确定[3]是否的确优于[1],不过[1]看起来更简洁。
[4] 基本类型int也可以当作整数值对象使用。
在使用Integer 时候,你应该只使用equals。。如果不小心使用了 和 !=、并旦没有测试-128~127范围外的值 那么虽然你的代码能运行 但在运行中可能悄悄地就会出现错误。如果使用了基本类型int,你就不能使用equals()而必须使用和 !=如果
你幵始使用基本类型int,然后更改为包装类型Integer,这可能会导致问题 反之亦然。

public class Cast_Exe_Test1{

	public static void main(String [] args){
		double price = calculate(1000, 8, "经济舱");
		System.out.println("优惠价格是: "+ price);
	}

	public static double calculate(double price, int month, String type){
		if(month >= 5 && month <= 10){
			switch(type){
				case "头等舱":
					price *= 0.9;
				break;
				case "经济舱":
					price *= 0.85;
				break;
			}
		}else{
			switch(type){
				case "头等舱":
					price *= 0.7;
				break;
				case "经济舱":
					price *= 0.65;
				break;
			}
		}
		return price;
	} 
}

(base) wannian07@wannian07-PC:~/Desktop/java/code$ java Cast_Exe_Test1
优惠价格是: 850.0

Java中验证码接口的实现方式有多种,其中常见的有:

图片验证码: 通过Java图形处理库如Java2D绘制验证码图片,并通过Servlet输出到前端。
短信验证码: 通过Java短信接口库如SMSLib发送短信验证码到用户手机,并在后台验证用户输入的验证码是否正确。
邮件验证码: 通过Java邮件接口库如JavaMail发送邮件验证码到用户邮箱,并在后台验证用户输入的验证码是否正确。

滑动验证码:通过JavaScript或者前端框架实现滑动验证,后台仅做校验。

import java.util.Random;

public class Cast_Exe_Test2{

	public static void main(String [] args){
		String code = createCode(5); //循环5次每次生成一位,5位验证码
		System.out.println("验证码是: "+ code);
	}

	public static String createCode(int n){
		Random r = new Random();     //定义一个随机生成数技术,用来生成随机数
		String code = "";   //定义一个空的code 变量用来接收生成的验证码
		for(int i = 0; i <= n; i++){   
			int type = r.nextInt(3);  //验证码包括数字、大小写字母组成
			switch(type){		    //a:    0       1       2
				case 0:	   //      数字   大写字母   小写字母
				code += r.nextInt(10);
					break;
				case 1:
				code += (char)(r.nextInt(26) + 65);
					break;
				case 2:
				code += (char)(r.nextInt(26) + 97);
					break;
			}
		}
		return code;
	}
		
}

(base) wannian07@wannian07-PC:~/Desktop/java/code$ java Cast_Exe_Test2
验证码是: 32NZc0
(base) wannian07@wannian07-PC:~/Desktop/java/code$ java Cast_Exe_Test2
验证码是: 5vbk2U

/*
package String类常用API;

import java.util.ArrayList;
import java.util.Random;

public class 验证码的几种生成方法 {
public static void main(String[] args) {
Random yzm = new Random(); //定义一个随机生成数技术,用来生成随机数
//2,用String常用API-charAit生成验证码
String yzm1 = “1234567890abcdefghijklmnopqrstuvwxwzABCDEFGHIJKLMNOPQRSTUVWXYZ”;//定义一个String变量存放需要的数据,一共58位
String yzm3 = “”;//定义一个空的Atring变量用来接收生成的验证码
for (int i = 0; i < 5; i++) {
int a = yzm.nextInt(58);//随机生成0-57之间的数,提供索引位置
yzm3+=yzm1.charAt(a);//用get 和提供的索引找到相应位置的数据给变量
}
System.out.println(“用String常用API-charAit生成的验证码为:”+yzm3);
}
}
*/


public class DoubleEquivalence {
  static void show(String desc, Double n1, Double n2) {
    System.out.println(desc + ":");
    System.out.printf(
      "%e==%e %b %b%n", n1, n2, n1 == n2, n1.equals(n2));
  }
  @SuppressWarnings("deprecation")
  public static void test(double x1, double x2) {
    // x1.equals(x2) // Won't compile
    System.out.printf("%e==%e %b%n", x1, x2, x1 == x2);
    Double d1 = x1;
    Double d2 = x2;
    show("Automatic", d1, d2);
    Double r1 = new Double(x1);
    Double r2 = new Double(x2);
    show("new Double()", r1, r2);
    Double v1 = Double.valueOf(x1);
    Double v2 = Double.valueOf(x2);
    show("Double.valueOf()", v1, v2);
  }
  public static void main(String[] args) {
    test(0, Double.MIN_VALUE);
    System.out.println("------------------------");
    test(Double.MAX_VALUE,
      Double.MAX_VALUE - Double.MIN_VALUE * 1_000_000);
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java DoubleEquivalence
0.000000e+004.900000e-324 false
Automatic:
0.000000e+00
4.900000e-324 false false
new Double():
0.000000e+004.900000e-324 false false
Double.valueOf():
0.000000e+00
4.900000e-324 false false

1.797693e+3081.797693e+308 true
Automatic:
1.797693e+308
1.797693e+308 false true
new Double():
1.797693e+3081.797693e+308 false true
Double.valueOf():
1.797693e+308
1.797693e+308 false true

class ValA {
  int i;
}

class ValB {
  int i;
  // Works for this example, not a complete equals():
  public boolean equals(Object o) {
    ValB rval = (ValB)o;  // Cast o to be a ValB
    return i == rval.i;
  }
}

public class EqualsMethod {
  public static void main(String[] args) {
    ValA va1 = new ValA();
    ValA va2 = new ValA();
    va1.i = va2.i = 100;
    System.out.println(va1.equals(va2));
    ValB vb1 = new ValB();
    ValB vb2 = new ValB();
    vb1.i = vb2.i = 100;
    System.out.println(vb1.equals(vb2));
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java EqualsMethod
false
true
是因为equals。方法的默认行为是比较引用。如果只想比较内容 你必须像ValB所示的那样重写equals()方法。ValB.equals()方法只包含了解决示例问题所必需的最简代码

4.7逻辑操作符

逻辑操作符“与”(&&)、" 或”( ) “ ”(!) 可以根据参数的逻辑关系

import java.util.*;

public class Bool {
  public static void main(String[] args) {
    Random rand = new Random(47);
    int i = rand.nextInt(100);
    int j = rand.nextInt(100);
    System.out.println("i = " + i);
    System.out.println("j = " + j);
    System.out.println("i > j is " + (i > j));
    System.out.println("i < j is " + (i < j));
    System.out.println("i >= j is " + (i >= j));
    System.out.println("i <= j is " + (i <= j));
    System.out.println("i == j is " + (i == j));
    System.out.println("i != j is " + (i != j));
    // Treating an int as a boolean is not legal Java:
    //- System.out.println("i && j is " + (i && j));
    //- System.out.println("i || j is " + (i || j));
    //- System.out.println("!i is " + !i);
    System.out.println("(i < 10) && (j < 10) is "
       + ((i < 10) && (j < 10)) );
    System.out.println("(i < 10) || (j < 10) is "
       + ((i < 10) || (j < 10)) );
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java Bool
i = 58
j = 55
i > j is true
i < j is false
i >= j is true
i <= j is false
i == j is false
i != j is true
(i < 10) && (j < 10) is false
(i < 10) || (j < 10) is false

“短路 ”的现象。一旦表达式当前部分的计算结果能够明确无误地确定整个表达式的值 表达式余下部分就不会被执行了。

public class ShortCircuit {
  static boolean test1(int val) {
    System.out.println("test1(" + val + ")");
    System.out.println("result: " + (val < 1));
    return val < 1;
  }
  static boolean test2(int val) {
    System.out.println("test2(" + val + ")");
    System.out.println("result: " + (val < 2));
    return val < 2;
  }
  static boolean test3(int val) {
    System.out.println("test3(" + val + ")");
    System.out.println("result: " + (val < 3));
    return val < 3;
  }
  public static void main(String[] args) {
    boolean b = test1(0) && test2(2) && test3(2);
    System.out.println("expression is " + b);
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java ShortCircuit
test1(0)
result: true
test2(2)
result: false
expression is false

4.8字面量

如果程序里使用了一个字面量(literal value ),则编译器能准确地知道它是什么类型的。

public class Literals {
  public static void main(String[] args) {
    int i1 = 0x2f; // Hexadecimal (lowercase)
    System.out.println(
      "i1: " + Integer.toBinaryString(i1));
    int i2 = 0X2F; // Hexadecimal (uppercase)
    System.out.println(
      "i2: " + Integer.toBinaryString(i2));
    int i3 = 0177; // Octal (leading zero)
    System.out.println(
      "i3: " + Integer.toBinaryString(i3));
    char c = 0xffff; // max char hex value
    System.out.println(
      "c: " + Integer.toBinaryString(c));
    byte b = 0x7f; // max byte hex value 0111111;
    System.out.println(
      "b: " + Integer.toBinaryString(b));
    short s = 0x7fff; // max short hex value
    System.out.println(
      "s: " + Integer.toBinaryString(s));
    long n1 = 200L; // long suffix
    long n2 = 200l; // long suffix (can be confusing)
    long n3 = 200;
    // Java 7 Binary Literals:
    byte blb = (byte)0b00110101;
    System.out.println(
      "blb: " + Integer.toBinaryString(blb));
    short bls = (short)0B0010111110101111;
    System.out.println(
      "bls: " + Integer.toBinaryString(bls));
    int bli = 0b00101111101011111010111110101111;
    System.out.println(
      "bli: " + Integer.toBinaryString(bli));
    long bll = 0b00101111101011111010111110101111;
    System.out.println(
      "bll: " + Long.toBinaryString(bll));
    float f1 = 1;
    float f2 = 1F; // float suffix
    float f3 = 1f; // float suffix
    double d1 = 1d; // double suffix
    double d2 = 1D; // double suffix
    // (Hex and Octal also work with long)
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java Literals
i1: 101111
i2: 101111
i3: 1111111
c: 1111111111111111
b: 1111111
s: 111111111111111
blb: 110101
bls: 10111110101111
bli: 101111101011111010111110101111
bll: 101111101011111010111110101111
4.8.1字面量里的下划线
可以在数字字面量里使用下划线 这样更易于阅读。

public class Underscores {
  public static void main(String[] args) {
    double d = 341_435_936.445_667;
    System.out.println(d);
    int bin = 0b0010_1111_1010_1111_1010_1111_1010_1111;
    System.out.println(Integer.toBinaryString(bin));
    System.out.printf("%x%n", bin);               // [1]
    long hex = 0x7f_e9_b7_aa;
    System.out.printf("%x%n", hex);
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java Underscores
3.41435936445667E8
101111101011111010111110101111
2fafafaf
7fe9b7aa
规则:

  1. 只能使用单个下划线 不能连续使用多个
  2. 数字的幵头或结尾不能有下划线
  3. F、D L这样的后缀周围不能有下划线
  4. 在二进制或十六进制标识符b x的周围不能有下划线。
    4.8.2科学记数法 又称“指数记数法”
public class Exponents {
  public static void main(String[] args) {
    // Uppercase and lowercase 'e' are the same:
    float expFloat = 1.39e-43f;
    expFloat = 1.39E-43f;
    System.out.println(expFloat);
    double expDouble = 47e47d; // 'd' is optional
    double expDouble2 = 47e47; // Automatically double
    System.out.println(expDouble);
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java Exponents
1.39E-43
4.7E48

4.9 按位操作符

按位操作符用来操作整数基本数据类型中的单个二进制位 bit)0按位操作符会对两个参数中对应的二进制位执行布尔代数运算 并生成一个结果。

4.10移位操作符

左移位操作(《)会将操作符左侧的操作数向左移动 移动的位数在操作符右侧指定 低位补0 。“有符号”的右移位操作符 >> 则按照操作符右侧指定的位数将操作符左侧的操作数向右移动。“有符号”的右移位操作符使用了 “ 号扩展” 如果符号为正 则在高位插入0, 否则在高位插入1。Java还新增加了一种“无符号”的右移位操作 >>> ,它使用“扩展” 无论符号为正还是为负 都在高位插入0。

import java.util.*;

public class BitManipulation {
  public static void main(String[] args) {
    Random rand = new Random(47);
    int i = rand.nextInt();
    int j = rand.nextInt();
    printBinaryInt("-1", -1);
    printBinaryInt("+1", +1);
    int maxpos = 2147483647;
    printBinaryInt("maxpos", maxpos);
    int maxneg = -2147483648;
    printBinaryInt("maxneg", maxneg);
    printBinaryInt("i", i);
    printBinaryInt("~i", ~i);
    printBinaryInt("-i", -i);
    printBinaryInt("j", j);
    printBinaryInt("i & j", i & j);
    printBinaryInt("i | j", i | j);
    printBinaryInt("i ^ j", i ^ j);
    printBinaryInt("i << 5", i << 5);
    printBinaryInt("i >> 5", i >> 5);
    printBinaryInt("(~i) >> 5", (~i) >> 5);
    printBinaryInt("i >>> 5", i >>> 5);
    printBinaryInt("(~i) >>> 5", (~i) >>> 5);

    long l = rand.nextLong();
    long m = rand.nextLong();
    printBinaryLong("-1L", -1L);
    printBinaryLong("+1L", +1L);
    long ll = 9223372036854775807L;
    printBinaryLong("maxpos", ll);
    long lln = -9223372036854775808L;
    printBinaryLong("maxneg", lln);
    printBinaryLong("l", l);
    printBinaryLong("~l", ~l);
    printBinaryLong("-l", -l);
    printBinaryLong("m", m);
    printBinaryLong("l & m", l & m);
    printBinaryLong("l | m", l | m);
    printBinaryLong("l ^ m", l ^ m);
    printBinaryLong("l << 5", l << 5);
    printBinaryLong("l >> 5", l >> 5);
    printBinaryLong("(~l) >> 5", (~l) >> 5);
    printBinaryLong("l >>> 5", l >>> 5);
    printBinaryLong("(~l) >>> 5", (~l) >>> 5);
  }
  static void printBinaryInt(String s, int i) {
    System.out.println(
      s + ", int: " + i + ", binary:\n   " +
      Integer.toBinaryString(i));
  }
  static void printBinaryLong(String s, long l) {
    System.out.println(
      s + ", long: " + l + ", binary:\n    " +
      Long.toBinaryString(l));
  }
}

4.11 三元操作符

public class TernaryIfElse {
  static int ternary(int i) {
    return i < 10 ? i * 100 : i * 10;
  }
  static int standardIfElse(int i) {
    if(i < 10)
      return i * 100;
    else
      return i * 10;
  }
  public static void main(String[] args) {
    System.out.println(ternary(9));
    System.out.println(ternary(10));
    System.out.println(standardIfElse(9));
    System.out.println(standardIfElse(10));
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java TernaryIfElse
900
100
900
100

4.12字符串操作符+ 和+

public class StringOperators {
  public static void main(String[] args) {
    int x = 0, y = 1, z = 2;
    String s = "x, y, z ";
    System.out.println(s + x + y + z);
    // Converts x to a String:
    System.out.println(x + " " + s);
    s += "(summed) = "; // Concatenation operator
    System.out.println(s + (x + y + z));
    // Shorthand for Integer.toString():
    System.out.println("" + x);
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java StringOperators
x, y, z 012
0 x, y, z
x, y, z (summed) = 3
0

4.13使用操作符时常犯的错误

4.14类型转换操作符

public class CastingNumbers {
  public static void main(String[] args) {
    double above = 0.7, below = 0.4;
    float fabove = 0.7f, fbelow = 0.4f;
    System.out.println("(int)above: " + (int)above);
    System.out.println("(int)below: " + (int)below);
    System.out.println("(int)fabove: " + (int)fabove);
    System.out.println("(int)fbelow: " + (int)fbelow);
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java CastingNumbers
(int)above: 0
(int)below: 0
(int)fabove: 0
(int)fbelow: 0

public class RoundingNumbers {
  public static void main(String[] args) {
    double above = 0.7, below = 0.4;
    float fabove = 0.7f, fbelow = 0.4f;
    System.out.println(
      "Math.round(above): " + Math.round(above));
    System.out.println(
      "Math.round(below): " + Math.round(below));
    System.out.println(
      "Math.round(fabove): " + Math.round(fabove));
    System.out.println(
      "Math.round(fbelow): " + Math.round(fbelow));
  }
}

(base) wannian07@wannian07-PC:~/Desktop/java/example/operators$ java RoundingNumbers
Math.round(above): 1
Math.round(below): 0
Math.round(fabove): 1
Math.round(fbelow): 0

  • 27
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值