java中的关系和逻辑运算:
运算符 |
用法 |
返回true的情况 |
> |
op1>op2 |
大于 |
>= |
op1>=op2 |
大于等于 |
< |
op1<op2 |
小于 |
<= |
op1<=op2 |
小于等于 |
== |
op1==op2 |
等于 |
!= |
op1!=op2 |
不等于 |
1》逻辑运算返回的都是boolean型的数据,即true or false;
2》逻辑运算是比较的对象(op)在计算机中内存的地址;
下面是一个例子:
public class FunnyTest {
public static void main(String[] args) {
char c='c';
System.out.println("+c ="+ +c);
byte b='c';
System.out.println("Initialized value +b ="+ +b);
System.out.println("(b==c) ="+(b==c));
byte w='w';
System.out.println("Initialized value +w ="+ +w);
b=w;
System.out.println("binds with w +b ="+ +b);
System.out.println("(c<=w) ="+(c<=w));
System.out.println("(c>=w) ="+(c>=w));
System.out.println("(b==c) ="+(b==c));
}
}
outprint:
Initialized value +c =99
Initialized value +b =99
(b==c) =true
Initialized value +w =119
binds with w +b =119
(c<=w) =true
(c>=w) =false
(b==c) =false
备注:此处‘+’ & ‘-’作为一元运算符
运算符 |
用法 |
描述 |
+ |
+op |
如果op是一个byte,short,char,op变成int 型 |
- |
-op |
取op的相反数 注:boolean型不适用 |
补充说明:4种条件运算符
运算符 |
用法 |
返回true的情况 |
&& |
op1&&op2 |
op1 和op2都是true |
|| |
op1||op2 |
op1 或者op2是true |
! |
!op |
op为false |
^ |
op1^op2 |
op1 和op2的逻辑值不同 |
条件运算符连接的关系是 逻辑关系
----------------------------------------------------------------------------------------------------------------------------------
有趣的代码:
public class FunnyTest {
public static void main(String[] args) {
double largeNum=Math.exp(4000.0);
double posDivZero=10.0/0.0;
double negDivZero=-10.0/0.0;
double zeroDivZero=0.0/0.0;
char c='c';
System.out.println("Initialized value +c ="+ +c);
System.out.println(largeNum);
System.out.println(posDivZero);
System.out.println(negDivZero);
System.out.println(zeroDivZero);
System.out.println("+c = "+ +c);
System.out.println("-c = "+ -c);
}
}
outprint:
Initialized value +c =99
Infinity
Infinity
-Infinity
NaN
+c = 99
-c = -99
分享到:
相关推荐
Idiot‘s_Guides_to_Beginning_Programming.pdf
An Idiot's Guide to C++ Templates
Idiot
The Complete Idiot’s Guide to iPad and iPhone App Development .pdf
The Complete Idiot's Guide to Improving Your IQ.pdf
傻瓜指南系列-如何作小生意]The.Complete.Idiot's.Guide.-.Small.Business.pdf
Explorer扩展编程的指导资料,包括一个chm教程以及其对应的源代码。 Part I - A step-by-step tutorial on writing shell extensions Part II - A tutorial on writing a shell extension that operates on ...
idiot
详细说明Windows Shell的编程指南,可以处理Windows Explorer资源管理器很多的交互,比如文件复制和剪切,鼠标右键菜单的添加,任意文件的图标的修改和叠加,系统文件拷贝进度条的控制等等,文件属性自定义扩展页和...
An Idiot’s guide to Support vector machines (SVMs)
依赖关系和安装 Jnario至少需要带有和 。 您可以从此更新站点http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/进行安装。 可以从以下更新站点http://www.jnario.org/updates/snapshot/...
加密已有文件,可以临时加密和永久解密;加密特定文件夹
This guide helps environmentally conscious people make real-world ...·More than 80 regional and local green building programs are in place in the U.S. and more are being promoted in the private sector
HTML Reference for dummies, idiot, beginner
most simple adding for those idiot
这是关于Java基础的入门教程,主要是数据结构,txt版的
CSS 2 Reference for begginer, idiot, dummies
American Idiot is the seventh studio album by American rock band Green Day
笨蛋 物联网ID