JAVA OPERATOR II

As promised in my last post, I will be sharing sample codes showing how Java operators behave. I have named the Java classes in the sample codes after the operator class and I also added the result of each operation on the variables.

Sample Class showing Arithmetic Operations: Output: Pretty easy right? Yes, I told you it is same with basic mathematical operations.

NOTE: (+) sign in Java signifies concatenation and not addition when used between two strings.  e.g. “Alpha”  + “Omega” = AlphaOmega.

Sample Class showing Relational Operations: Output: Relational operators are used for comparison, sometimes while typing your code, you might reach a point where you need to make a decision, the output 0f this decision might be what determines what to do next e.g. If gender equals to Male, set name to Jack, else, set name to Jill. In this kind of situation, the relational operator becomes very useful. The output of a relational operation is always boolean.

Note: On line 14, 21 and 28 you don’t necessarily have to include the “if” condition, you can just go like

if (50==10) {

} else {

}

The compiler understand the “else” to mean the opposite condition ie. (50 !=10).

Sample Class showing Assignment Operations: Output: Assignment Operators are more like shorthand operators in Java. Instead of going all the way to say x = x + 7 you can just say x += 7. Cool right? I think so too. They also behave same way as the full expression only that they are shortened format.

Sample Class showing Logical Operations: Output: Logical operators only work on boolean variables (true or false). However, the boolean variables could be output of a relational expression e.g.

if ( (x > y)  &&  (y != 0) ) .

Let us assume x=5 and y=2,

the expression will then become              if ( ( 5 > 2 )  &&  ( 2 != 0 ) )

==>   ( true && true)     => True

Sample Class showing Unary Operations: Output: Unary plus and minus only add the (+) and (-) sign respectively to variables.

Note: Increment and decrement are special operators whose return value depend on the position of the operator. Hence we have pre- increment/decrement and post- increment/decrement.

When increment/decrement operation is applied before (pre) variable, incremented value of the variable is returned. E.g. Assume x= 5, ++5 will return 6 and –5 will return 4

When increment/decrement operation is applied after (post), the computer save the original value of the variable, perform the operation and then return the saved value and not the incremented value. E.g. Assume x= 5, x++ will return 5 and –x will return 6.

— x will return 6 because the value of x has been changed previously when  (x++) operation was performed on it despite the fact that it returns 5(saved value) after the operation. Also the effect of (–x) operation is not reflecting yet in the result as the saved value of x is what is returned. Ok, you should try this yourself and read my explanation again to have full understanding.

Sample Class showing Bitwise and Bit Shift Operations: Output: Bitwise and Bit Shift Operators converts variables to bit and manipulate them using specified operator.

We have come to the end of lesson on Java Operators, endeavour to practice using the operators yourself and see how they behave and refer to my earlier post should you require more explanation. Happy coding!!!

1. nidcomsolutions on said: