Operators

With few exceptions, the operators available in C# are syntactically and operationally the same as those in Java. Table 4-10 summarizes the operators available in both languages and highlights the differences in bold.

Table 4-10. Operator Comparison

Category

Java

C#

Arithmetic

+ - * / %

+ - * / %

Logical

&& ||

&& || true false

Bitwise

& | ^ ! ~

& | ^ ! ~

String concatenation

+ +=

+ +=

Increment and decrement

++ --

++ --

Bitwise shift

<< >> ⋙

<< >>

Relational

== != < > <= >=

== != < > <= >=

Assignment

= += -= *= /= %= &=

|= ^= <<= >>= ⋙=

= += -= *= /= %= &= |= ^= <<= >>=

Member access

.

.

Indexing

[]

[]

Cast

()

()

Conditional

?:

?:

Delegate concatenation and removal

N/A

+ -

Object creation

new

new

Type information

instanceof

is sizeof typeof

Overflow exception control

N/A

checked unchecked

Indirection and address

N/A

* -> [] &

The following list summarizes the differences between the Java and C# operators:

  • Logical (true and false). C# considers true and false to be operators as well as literals. C# allows the true and false operators for a class or struct to be overridden. Operator overloading is discussed in the Members section in Chapter 5.

  • Bitwise shift (>>>). There is no equivalent in C# of Java’s zero-fill right-shift operator.

  • Delegate operations (+ and -)The delegate concatenation and removal operators are new to C#, as are delegates.

  • Type information (is, sizeof, and typeof)

    • The C# is operator is the equivalent of the Java instanceof operator.

    • The sizeof operator returns the bytes occupied by a value type in memory. It has no equivalent in Java and can be used only in an unsafe code block. See the Unsafe Code section in Chapter 6 for details.

  • The typeof operator is a useful way to get a System.Type object for the class name specified as the operand. The Java equivalent is the static method Class.forName or the public static Class field that is inherited by every type.

  • Overflow exception control. The overflow exception control operators (checked and unchecked) have no equivalent in Java. They control how overflow is handled when carrying out numeric operations, and they’re discussed in the Statements section later in this chapter.

  • Pointer operators. Indirection and address operators are related to pointer operation and have no equivalents in Java. See Unsafe Code in Chapter 6 for details.

Precedence and Associativity

There are some subtle differences in the precedence and associativity of operators in C#. These differences have been highlighted in bold in Table 4-11.

Table 4-11. Operator Precedence and Associativity

Java

C#

Associativity

[] . ()

[] . () x++ x-- new typeof checked unchecked

left to right

(unary)+ (unary)- ! ~ ++ -- (cast) new

(unary)+ (unary)- ! ~ ++x --x (cast)

right to left

* / %

* / %

left to right

+ -

+ -

left to right

<< >> ⋙

<< >>

left to right

< <= > => instanceof

< > <= => is as

left to right

== !=

== !=

left to right

&

&

left to right

^

^

left to right

|

|

left to right

&&

&&

left to right

||

||

left to right

?:

?:

Java: left to right C#: right to left

= += -= *= /= %= &=

= += -= *= /= %= &=

right to left

|= ^= <<= >>= ⋙=

|= ^= <<= >>=

 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset