java operators_[Thinking In Java]代码整理之操作符(Operators)(二)

指数表示法(Exponential notation)

使用e或E跟随一个整数表示幂指数,如果是负值,则需要跟f或d表示小数;如果为整数则跟L表示long,不跟则表示为int。下面看一个例子1-1 指数表示法的例子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);

}

}

程序运行的结果如下:

3988ffbc9d695131d065f68d15ecc8e7.png

注意:在科学工程中,‘e’指的是自然对数,它的值接近于2.718(在Java语言中用Math.E来表示),但是在代码1-1 中,e指的的‘10的次方’,这点在Java编程语言中应该注意。逻辑运算符(Logical operators)

逻辑运算符包括:与(&&)、或(||)、非(!)1-2 逻辑运算符的例子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)) );

}

}

程序运行的而结果如下:

80ed3b5e35024e5b7b75640d95d405c2.png位操作符(Bitwise operators)

位操作符主要有以下几种:&   (按位与)

|   (按位或)

∧  (按位非)

~  (取反)

注意:前三种位操作符可以与“=”结合使用,但是“~”是单目运算符,不能与“=”结合使用。另外位操作符的“&”只是对两个而进制数进行按与操作,得到一个二进制的结果,不具有“短路”的功能。而逻辑运算符“&&”具有“短路”的功能,当第一个条件为假时,便不再对后面的条件进行判断。

注:文章的代码摘自 Thinking in Java(Fourth Edition)英文版,作者 [美]Bruce Eckef,刘中兵 评注。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值