Remember in the midterm midterm, Professor Jory asked a question about operator as a bonus question? In fact, there are so many more different kinds of operators than what we learnt in class. Addition, subtraction, etc. are only a subset of operators, called arithmetic operators.
Unary operators can be increment or decrement. (++), (-), (--) are all examples of unary operators.
Binary operators has six major categories as listed in the picture above. We covered most of the arithmetic operator during the class. Assignment operator is (=) and it is used when you set the values of data, e.g. String name = "Cathy." Logical operators are (&&),(||), and (!). Relational operators are (==),(!=),(>),(<),(<=) or (>=), which we also discussed in the classroom. Compound operator performs operation on the two operands before assigning the result to the first operand. Compound operator works on both arithmetic and bitwise operator. There are 11 different kinds of operators and (+=) is the one we often use throughout this semester. The other compound operators include (-+),(*=),(/=),(%=),(&=),(|=),(^=),(<<=),(>>=),(>>>=).
The only operator we have never seen before is bitwise operator. Bitwise operator works on all integer data types including int, long, short, char, and byte. It converts all the integers to bits and perform bit-by-bit operations on integers. An example given on this website (https://www.tutorialspoint.com/java/java_basic_operators.htm) is a = 60 and b = 13. In binary world, a = 0011 1100 and b = 0000 1101. Bitwise operators include (&),( |), (^),(~),(<<),(>>) and (>>>). (&) is binary and, which copies a bit to the result if it exists in both operands, otherwise it will copy 0. (a&b)=0000 1100. (~) flips the bits, so (~a) returns 1100 0011. (<<) is binary shift and moves all the bits to the left. (a<<2) returns 1111 0000. (>>>) is zero fill right operator. The left operand is shifted by the number in the right operands and zero fills all the empty bits. (a>>>2) returns 0000 1111.
Ternary operator takes three operands and conditional operator is a ternary operator. The syntax for ternary operator is (?:) expressionA?expressionB:expressionC. Expression A is implicitly converted to a boolean. If it is true, expression B will be evaluated, otherwise expression C will be evaluated. A good example would be compute the minimum value of x and y.
minimum = (x < y) ? x : y;
--------------------------------------------------------------------------------------------------------------------------
Picture References:1.https://www.google.com/search?q=java+operator&biw=1280&bih=583&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjvzZnSoKvQAhXBylQKHcw5CLEQ_AUICygA#imgrc=G73VcUwBb5rsVM%3A
Writing References:
1. http://www.computernotes.in/java/core-java/operators-in-java/
2. https://www.tutorialspoint.com/java/java_basic_operators.htm
3. http://www.c4learn.com/java/java-operators/
4. https://www.tutorialspoint.com/cplusplus/unary_operators_overloading.htm
5. https://msdn.microsoft.com/en-us/library/e4213hs1.aspx
6. http://alvinalexander.com/java/edu/pj/pj010018
--------------------------------------------------------------------------------------------------------------------------

Great article! I really liked how you expanded on a topic from the midterm exam. You explained it in a really beneficial and helpful way. I also like that you added by introducing bitwise operators. Well done on a well researched and well written article!
ReplyDeleteThank you for expanding our knowledge of operator! I really like how every time you expand from classes, quizzes and tests. Bitwise operator is definitely gonna be useful in the future of computer science course.
ReplyDeleteReally great article! It was such a great expansion on some of the knowledge that we already have from the midterm. I also liked how you gave really clear and concrete examples for some of the operators. Could some of these operators that we have not used end up saving our program time, thus making it a better performing program?
ReplyDeleteBitwise operator is interesting! But I am a little confused about (~) operator which flips the bit. Does it just change 1 to 0, and 0 to 1?
ReplyDeleteIn addition, ternary operator is useful. I remember that Dr. Jory used ternary operator in the unit test for ShiftCipher.java of programming assignment 07.
Nice article. I think its cool to learn about operators, and where they are derived from. I think it is important to understand more about the functions and operators we are using, rather than simply just using them and assuimg they work
ReplyDelete