COMP6598 - Week 3 - Elementary Programming (Operator)
COMP6598 - Week 3 - Elementary Programming (Operator)
Introduction to Programming
>> Right shift with sign 10101110 >> 2 yields 11101011 The operator shifts bit in the first operand right
extension 00101110 >> 2 yields 00001011 by the number of bits specified in the second
operand, filling with the highest (sign) bit on the
left
>>> Unsigned right shift 10101110 >>> 2 yields 00101011 The operator shifts bit in the first operand right
with zero extension 00101110 >>> 2 yields 00001011 by the number of bits specified in the second
operand, filling with Os on the left
Example
RELATIONAL / COMPARISON
OPERATIONS
Relational Operator
• The Relational operators determine if one operand is greater than, less
than, equal to, or not equal to another operand.
• Relational Operator can also be combined with Logical Operator
Java Mathmatics Name Example (radius is 5) Result
Operator Symbol
< < Less than radius < 0 false
<= ≤ Less than or equal to radius <= 0 false
> > Greater than radius > 0 true
>= ≥ Greater than or equal radius >=0 true
to
== = Equal to radius == 0 false
!= ≠ Not equal to radius != 0 true
Example
MATH OPERATIONS
Math Class
• The Math class contains the methods needed
to perform basic mathematical functions.
• The Math class can be categorized as
– trigonometric methods : sin, cos, tan, asin, acon,
atan, toDegrees, toRadians
– exponent methods : exp, log, log10, pow, sqrt
– service methods : rounding, min, max, absolute,
and random methods
Example - Trigonometri
Example Exponent
Example service method
OPERATOR PRECEDENCE
Operator Precedence
Precedence Operator
var++ and var– (Postfix)
+, - (Unary plus and minus). ++var and –var (Prefix)
(type) (Casting)
! (Not)
*, / . % (Multiplication, division and remainder)
+ , - (Binary addition and substraction)
<. <= , >, >= (Comparison)
==, != (equality)
^(exclusive OR)
&& (AND)
|| (OR)
=, +=, -=, *=, /=, %= (assignment Operator)
Exercise
• Which data types that requires the largest
amount of memory ?
• If today is Monday, what will be the day in
the next 50 days ?
• Write an expression that obtains a random
integer between 20 and 100
REFERENCES
References
• Daniel Liang, Y., 2013, Introduction to java programming,
vol.09, Pearson Education, New Jersey. Chapter 3Wrapper
Class
http://en.wikipedia.org/wiki/Primitive_wrapper_class
http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.
ii.doc/ad/rwrjwrap.htm
http://java.sun.com/j2se/1.3/docs/api/java/lang/Math.html
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
Thank You