Java equals, comparison, and condition operators (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/12/15/2819844.html

English from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

Equal and comparison Operators

It is equivalent to a comparison operator to determine whether an operand is greater than, less than, equal to, or not equal to another operand. The main operators are likely to be familiar to you. Remember, when determining whether two native types are equivalent, you must use "=" instead of "= ".

 
= Equal! = Not equal to> greater than> = greater than or equal to <less than <= less than or equal

The followingProgram,Comparisondemo test comparison operator:

Class comparisondemo {public static void main (string [] ARGs) {int value1 = 1; int value2 = 2; If (value1 = value2) system. out. println ("value1 = value2"); If (value1! = Value2) system. Out. println ("value1! = Value2 "); If (value1> value2) system. out. println ("value1> value2"); If (value1 <value2) system. out. println ("value1 <value2"); If (value1 <= value2) system. out. println ("value1 <= value2 ");}}

Output:

 
Value1! = Value2value1 <value2value1 <= value2

 

Conditional Operators

& | The operator executes conditions and conditions or operations on two Boolean operands. These operators are short-circuited, which means that the second operand is calculated only when necessary.

 
& Conditions and | condition or

The following programConditionaldemo1, test the following OPERATOR:

Class conditionaldemo1 {public static void main (string [] ARGs) {int value1 = 1; int value2 = 2; If (value1 = 1) & (value2 = 2) system. out. println ("value1 is 1 and value2 is 2"); If (value1 = 1) | (value2 = 1) system. out. println ("value1 is 1 or value2 is 1 ");}}

Another conditional operator is? : It can be considered as the stenographer of the if-then-else statement. It is the only ternary operator. In the following example, the operator can be considered as follows:Somecondition is true. value1 value is assigned to result. Otherwise, value2 value is assigned to result.

The following program,Conditionaldemo2, test? : Operator:

 
Class conditionaldemo2 {public static void main (string [] ARGs) {int value1 = 1; int value2 = 2; int result; Boolean somecondition = true; Result = somecondition? Value1: value2; system. Out. println (result );}}

BecauseSomecondition is true, so the program outputs "1" in the screen. Use? : The operator replaces the if-then-else statement. If this causes yourCodeMore readable. For example, when the expression is compact and has no side effects (for example, assigning values ).

Type comparison operator instanceof

The instanceof operator compares an object with a specified type. It can be used to determine whether an object is an instance of a class, a subclass instance, and an instance of a class that implements a specific interface.

The following program,Instanceofdemo defines a parent class (named as parent) and a simple interface (the command isMyinterface), A subclass that inherits the parent class and implements the myinterface interface (named child)

Class instanceofdemo {public static void main (string [] ARGs) {parent obj1 = new parent (); parent obj2 = new child (); system. out. println ("obj1 instanceof parent:" + (obj1 instanceof parent); system. out. println ("obj1 instanceof child:" + (obj1 instanceof child); system. out. println ("obj1 instanceof myinterface:" + (obj1 instanceof myinterface); system. out. println ("obj2 instanceof parent:" + (obj2 instanceof parent); system. out. println ("obj2 instanceof child:" + (obj2 instanceof child); system. out. println ("obj2 instanceof myinterface:" + (obj2 instanceof myinterface);} Class Parent {} class Child extends parent implements myinterface {} interface myinterface {}

Output:

Obj1 instanceof parent: trueobj1 instanceof child: falseobj1 instanceof myinterface: falseobj2 instanceof parent: trueobj2 instanceof child: trueobj2 instanceof myinterface: True

UseWhen using the instanceof operator, remember that null is not an instance of any class.

Related Article

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.