SlideShare a Scribd company logo
Using Java Operators
Assignment Operator (=)
lvalue = rvalue;
• Take the value of the rvalue and
store it in the lvalue.
• The rvalue is any constant,
variable or expression.
• The lvalue is named variable.
w = 10;
x = w;
z = (x - 2)/(2 + 2);
Mathematical Operators
• Addition +
• Subtraction -
• Multiplication *
• Division /
• Modulus %
Simple Arithmetic
public class Example {
public static void main(String[] args) {
int j, k, p, q, r, s, t;
j = 5;
k = 2;
p = j + k;
q = j - k;
r = j * k;
s = j / k;
t = j % k;
System.out.println("p = " + p);
System.out.println("q = " + q);
System.out.println("r = " + r);
System.out.println("s = " + s);
System.out.println("t = " + t);
}
}
> java Example
p = 7
q = 3
r = 10
s = 2
t = 1
>
Shorthand Operators
+=, -=, *=, /=, %=
Common Shorthand
a = a + b; a += b;
a = a - b; a -= b;
a = a * b; a *= b;
a = a / b; a /= b;
a = a % b; a %= b;
Shorthand Operators
public class Example {
public static void main(String[] args) {
int j, p, q, r, s, t;
j = 5;
p = 1; q = 2; r = 3; s = 4; t = 5;
p += j;
q -= j;
r *= j;
s /= j;
t %= j;
System.out.println("p = " + p);
System.out.println("q = " + q);
System.out.println("r = " + r);
System.out.println("s = " + s);
System.out.println("t = " + t);
}
}
> java Example
p = 6
q = -3
r = 15
s = 0
t = 0
>
Shorthand Increment and Decrement
++ and --
Common Shorthand
a = a + 1; a++; or
++a;
a = a - 1; a--; or
--a;
Increment and Decrement
> java example
p = 6
q = 6
j = 7
r = 6
s = 6
>
public class Example {
public static void main(String[] args) {
int j, p, q, r, s;
j = 5;
p = ++j; // j = j + 1; p = j;
System.out.println("p = " + p);
q = j++; // q = j; j = j + 1;
System.out.println("q = " + q);
System.out.println("j = " + j);
r = --j; // j = j -1; r = j;
System.out.println("r = " + r);
s = j--; // s = j; j = j - 1;
System.out.println("s = " + s);
}
}
Arithmetic
Operators
A Change of Topic
Casting
long
64 bit
l
int
32 bit
i
long
64 bit
int
32 bit
l
i
int i;
i = (int)l;
l = (long)i;
Moving Between Buckets
"Casting"
long l;
l = i;
long
64 bit
int
32 bit
l
i
Widening
Narrowing
Declaration Size and Type Number Range
byte b; // 8 bit integer -2-7
to +2+7
-1
short s; // 16 bit integer -2-15
to +2+15
-1
char c; // 16 bit Unicode 0 to +2+16
int i; // 32 bit integer -2-31
to +2+31
-1
long l; // 64 bit integer -2-63
to +2+63
-1
float f; // 32 bit floating point IEEE754 Standard
double d; // 64 bit floating point IEEE754 Standard
The Primitive Numbers
Casting the Primitives
b = (byte)s;
b = (byte)c;
b = (byte)i;
b = (byte)l;
b = (byte)f;
b = (byte)d;
i = (int)b;
i = (int)s;
i = (int)c;
i = (int)l;
i = (int)f;
i = (int)d;
l = (long)b;
l = (long)s;
l = (long)c;
l = (long)i;
l = (long)f;
l = (long)d;
s = (short)b;
s = (short)c;
s = (short)i;
s = (short)l;
s = (short)f;
s = (short)d;
2'sComplement
c = (char)b;
c = (char)s;
c = (char)i;
c = (char)l;
c = (char)f;
c = (char)d;
PositiveOnly
f = (float)b;
f = (float)s;
f = (float)c;
f = (float)i;
f = (float)l;
f = (float)d;
d = (double)b;
d = (double)s;
d = (double)c;
d = (double)i;
d = (double)l;
d = (double)f;
FloatingPoint
b = (byte)s;
b = (byte)c;
b = (byte)i;
b = (byte)l;
b = (byte)f;
b = (byte)d;
i = b;
i = s;
i = c;
i = (int)l;
i = (int)f;
i = (int)d;
l = b;
l = s;
l = c;
l = i;
l = (long)f;
l = (long)d;
s = b;
s = (short)c;
s = (short)i;
s = (short)l;
s = (short)f;
s = (short)d;
2'sComplement
c = (char)b;
c = (char)s;
c = (char)i;
c = (char)l;
c = (char)f;
c = (char)d;
PositiveOnly
f = b;
f = s;
f = c;
f = i;
f = l;
f = (float)d;
d = b;
d = s;
d = c;
d = i;
d = l;
d = f;
FloatingPoint
Acceptable Implicit Casts
char
16 bit
double
64 bit
float
32 bit
long
64 bit
int
32 bit
short
16 bit
byte
8 bit
Illegal
b = l;
l = f;
c = s;
OK
l = b;
i = c;
f = l
char
16 bit
double
64 bit
float
32 bit
long
64 bit
int
32 bit
short
16 bit
byte
8 bit
char
16 bit
double
64 bit
float
32 bit
long
64 bit
int
32 bit
short
16 bit
byte
8 bit
Automatic Promotion with Arithmetic
Illegal Casts
s = s + b;
s = s + s;
OK
s = (short)(s + b);
s = (short)(s + s);
Arithmetic is never done
in 8 or 16 bit containers.
char
16 bit
double
64 bit
float
32 bit
long
64 bit
int
32 bit
short
16 bit
byte
8 bit
Arithmetic Promotion with Mixed Primitives
Illegal Casts
i = i + l;
f = f + d;
l = l + f;
OK
i = (int)(i + l);
d = (double)(f + d);
l = (long)(l + f);
Arithmetic is done
in the "widest" type.
Implicit Casts in Method Calls
char
16 bit
double
64 bit
float
32 bit
long
64 bit
int
32 bit
short
16 bit
byte
8 bit
Illegal
i = st.indexOf(f);
OK
i = st.indexOf(c);
i = st.indexOf(b);
For: String st; and,
public int indexOf(int ch);
Casting
A Change of Topic
The Logical
and
Relational Operators
Relational Operators
> < >= <= == !=
Primitives
• Greater Than >
• Less Than <
• Greater Than or Equal >=
• Less Than or Equal <=
Primitives or Object References
• Equal (Equivalent) ==
• Not Equal !=
The Result is Always true or false
Relational Operator Examples
public class Example {
public static void main(String[] args) {
int p =2; int q = 2; int r = 3;
Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.println("p < r " + (p < r));
System.out.println("p > r " + (p > r));
System.out.println("p == q " + (p == q));
System.out.println("p != q " + (p != q));
System.out.println("i == j " + (i == j));
System.out.println("i != j " + (i != j));
}
} > java Example
p < r true
p > r false
p == q true
p != q false
i == j false
i != j true
>
Logical Operators (boolean)
&& || !
• Logical AND &&
• Logical OR ||
• Logical NOT !
Logical (&&) Operator Examples
public class Example {
public static void main(String[] args) {
boolean t = true;
boolean f = false;
System.out.println("f && f " + (f && f));
System.out.println("f && t " + (f && t));
System.out.println("t && f " + (t && f));
System.out.println("t && t " + (t && t));
}
} > java Example
f && f false
f && t false
t && f false
t && t true
>
Logical (||) Operator Examples
public class Example {
public static void main(String[] args) {
boolean t = true;
boolean f = false;
System.out.println("f || f " + (f || f));
System.out.println("f || t " + (f || t));
System.out.println("t || f " + (t || f));
System.out.println("t || t " + (t || t));
}
} > java Example
f || f false
f || t true
t || f true
t || t true
>
Logical (!) Operator Examples
public class Example {
public static void main(String[] args) {
boolean t = true;
boolean f = false;
System.out.println("!f " + !f);
System.out.println("!t " + !t);
}
}
> java Example
!f true
!t false
>
Logical Operator Examples
Short Circuiting with &&
public class Example {
public static void main(String[] args) {
boolean b;
int j, k;
j = 0; k = 0;
b = ( j++ == k ) && ( j == ++k );
System.out.println("b, j, k " + b + ", " + j + ", " + k);
j = 0; k = 0;
b = ( j++ != k ) && ( j == ++k );
System.out.println("b, j, k " + b + ", " + j + ", " + k);
}
}
> java Example
b, j, k true 1, 1
> java Example
b, j, k true 1, 1
b, j, k false 1, 0
>
Logical Operator Examples
Short Circuiting with ||
public class Example {
public static void main(String[] args) {
boolean b;
int j, k;
j = 0; k = 0;
b = ( j++ == k ) || ( j == ++k );
System.out.println("b, j, k " + b + ", " + j + ", " + k);
j = 0; k = 0;
b = ( j++ != k ) || ( j == ++k );
System.out.println("b, j, k " + b + ", " + j + ", " + k);
}
}
> java Example
b, j, k true 1, 0
> java Example
b, j, k true 1, 0
b, j, k true 1, 1
>
The Logical and
Relational Operators
A Change of Topic
Manipulating
the Bits 10010111
Logical Operators (Bit Level)
& | ^ ~
• AND &
• OR |
• XOR ^
• NOT ~
Twos Complement Numbers
Base 10 A byte of binary
+127 01111111
+4 00000100
+3 00000011
+2 00000010
+1 00000001
+0 00000000
-1 11111111
-2 11111110
-3 11111101
-4 11111100
-128 10000000
Adding Twos Complements
Base 10 Binary
+3 00000011
-2 11111110
+1 00000001
Base 10 Binary
+2 00000010
-3 11111101
-1 11111111
Logical Operators (Bit Level)
& | ^ ~
int a = 10; // 00001010 = 10
int b = 12; // 00001100 = 12
a 00000000000000000000000000001010 10
b 00000000000000000000000000001100 12
a & b 00000000000000000000000000001000 8
a 00000000000000000000000000001010 10
b 00000000000000000000000000001100 12
a | b 00000000000000000000000000001110 14
a 00000000000000000000000000001010 10
b 00000000000000000000000000001100 12
a ^ b 00000000000000000000000000000110 6
a 00000000000000000000000000001010 10
~a 11111111111111111111111111110101 -11
&
AND
|
OR
^
XOR
~
NOT
Logical (bit) Operator Examples
public class Example {
public static void main(String[] args) {
int a = 10; // 00001010 = 10
int b = 12; // 00001100 = 12
int and, or, xor, na;
and = a & b; // 00001000 = 8
or = a | b; // 00001110 = 14
xor = a ^ b; // 00000110 = 6
na = ~a; // 11110101 = -11
System.out.println("and " + and);
System.out.println("or " + or);
System.out.println("xor " + xor);
System.out.println("na " + na);
}
}
> java Example
and 8
or 14
xor 6
na -11
>
Shift Operators (Bit Level)
<< >> >>>
• Shift Left << Fill with Zeros
• Shift Right >> Based on Sign
• Shift Right >>> Fill with Zeros
Shift Operators << >>
int a = 3; // ...00000011 = 3
int b = -4; // ...11111100 = -4
a 00000000000000000000000000000011 3
a << 2 00000000000000000000000000001100 12
b 11111111111111111111111111111100 -4
b << 2 11111111111111111111111111110000 -16
<<
Left
>>
Right
a 00000000000000000000000000000011 3
a >> 2 00000000000000000000000000000000 0
b 11111111111111111111111111111100 -4
b >> 2 11111111111111111111111111111111 -1
Shift Operator >>>
int a = 3; // ...00000011 = 3
int b = -4; // ...11111100 = -4
>>>
Right 0
a 00000000000000000000000000000011 3
a >>> 2 00000000000000000000000000000000 0
b 11111111111111111111111111111100 -4
b >>> 2 00111111111111111111111111111111 +big
Shift Operator Examples
public class Example {
public static void main(String[] args) {
int a = 3; // ...00000011 = 3
int b = -4; // ...11111100 = -4
System.out.println("a<<2 = " + (a<<2));
System.out.println("b<<2 = " + (b<<2));
System.out.println("a>>2 = " + (a>>2));
System.out.println("b>>2 = " + (b>>2));
System.out.println("a>>>2 = " +
(a>>>2));
System.out.println("b>>>2 = " +
(b>>>2));
}
} > java Example
a<<2 = 12
b<<2 = -16
a>>2 = 0
b>>2 = -1
a>>>2 = 0
b>>>2 = 1073741823
>
Shift Operator >>> and
Automatic Arithmetic Promotion
byte a = 3; // 00000011 = 3
byte b = -4; // 11111100 = -4
byte c;
c = (byte) a >>> 2
c = (byte) b >>> 2
>>>
Right
Fill 0
a 00000011 3
a >>> 2 00000000000000000000000000000000 0
c = (byte) 00000000 0
b 11111100 -4
b >>> 2 00111111111111111111111111111111 1073741823
c = (byte) Much to big for byte 11111111 -1
Which Operators Operate On What
Operators
&& || !
Unary
+ - ++ --
+ - * / %
> < >= <=
== !=
<< >> >>>
& | ^ ~
=
op= etc.
floatdouble
Floating Point
char byteshortintlong
Automatic
Promotion
Except ++ - -
Automatic
Promotion
Except ++ - -
Automatic
Promotion
Except ++ - -
Automatic
Promotion
Except ++ - -
Automatic
Promotion
Except ++ - -
Automatic
Promotion
Except ++ - -
Automatic
Promotion
Automatic
Promotion
Automatic
Promotion
Integral
boolean
Logical Any
Object
+ with
String
Only
Reference
Only
Not Content
Assignment Operator (=) and Classes
Date x = new Date();
Date y = new Date();
x = y;
Assignment Operator (=) and Classes
Date x = new Date();
Date y = new Date();
x = y;
Manipulating
the Bits
A Change of Topic
Some Odds
and Ends
Ternary Operator
? :
If true this expression is
evaluated and becomes the
value entire expression.
Any expression that evaluates
to a boolean value.
If false this expression is
evaluated and becomes the
value entire expression.
boolean_expression ? expression_1 : expression_2
Ternary ( ? : ) Operator Examples
public class Example {
public static void main(String[] args) {
boolean t = true;
boolean f = false;
System.out.println("t?true:false "+(t ? true : false ));
System.out.println("t?1:2 "+(t ? 1 : 2 ));
System.out.println("f?true:false "+(f ? true : false ));
System.out.println("f?1:2 "+(f ? 1 : 2 ));
}
}
> java Example
t?true:false true
t?1:2 1
f?true:false false
f?1:2 2
>
String (+) Operator
String Concatenation
"Now is " + "the time."
"Now is the time."
String (+) Operator
Automatic Conversion to a String
If either expression_1If either expression_1 or expression_2 evaluates
to a string the other will be converted to a string
if needed. The result will be their concatenation.
expression_1 + expression_2
String (+) Operator
Automatic Conversion with Primitives
"The number is " + 4
"The number is " + "4"
"The number is 4"
String (+) Operator
Automatic Conversion with Objects
"Today is " + new Date()
"Today is " + "Wed 27 22:12;26 CST 2000"
"Today is Wed 27 22:12;26 CST 2000"
"Today is " + new Date().toString()
Operator Precedence
+ - ++ -- ! ~ ()
* / %
+ -
<< >> >>>
> < >= <= instanceof
== !=
& | ^
&& ||
?:
= (and += etc.)
Unary
Arithmetic
Shift
Comparison
Logical Bit
Boolean
Ternary
Assignment
Passing Classes to Methods
class Letter {
char c;
}
public class PassObject {
static void f(Letter y) {
y.c = 'z';
}
public static void main(String[] args) {
Letter x = new Letter();
x.c = 'a';
System.out.println("1: x.c: " + x.c); // Prints 1: x.c: a
f(x);
System.out.println("2: x.c: " + x.c); // Prints 2: x.c: z
}
}
Passing Primitives to Methods
class Letter {
char c;
}
public class PassPrimitive {
static void f(char y) {
y = 'z';
}
public static void main(String[] args) {
Letter x = new Letter();
x.c = 'a';
System.out.println("1: x.c: " + x.c); // Prints 1: x.c: a
f(x.c);
System.out.println("2: x.c: " + x.c); // Prints 2: x.c: a
}
}
End of Content
Ad

More Related Content

What's hot (20)

What's new in C# 6 - NetPonto Porto 20160116
What's new in C# 6  - NetPonto Porto 20160116What's new in C# 6  - NetPonto Porto 20160116
What's new in C# 6 - NetPonto Porto 20160116
Paulo Morgado
 
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
"Немного о функциональном программирование в JavaScript" Алексей Коваленко"Немного о функциональном программирование в JavaScript" Алексей Коваленко
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Fwdays
 
Sneaking inside Kotlin features
Sneaking inside Kotlin featuresSneaking inside Kotlin features
Sneaking inside Kotlin features
Chandra Sekhar Nayak
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
Mario Fusco
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
Ganesh Samarthyam
 
Comparing JVM languages
Comparing JVM languagesComparing JVM languages
Comparing JVM languages
Jose Manuel Ortega Candel
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlin
intelliyole
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performance
intelliyole
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
The Software House
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.
Vadim Dubs
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015
Leonardo Borges
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
Mario Fusco
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
Ganesh Samarthyam
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Data
greenwop
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
Mario Fusco
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In Java
Andrei Solntsev
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
Ganesh Samarthyam
 
Functional programming in java
Functional programming in javaFunctional programming in java
Functional programming in java
John Ferguson Smart Limited
 
What's new in C# 6 - NetPonto Porto 20160116
What's new in C# 6  - NetPonto Porto 20160116What's new in C# 6  - NetPonto Porto 20160116
What's new in C# 6 - NetPonto Porto 20160116
Paulo Morgado
 
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
"Немного о функциональном программирование в JavaScript" Алексей Коваленко"Немного о функциональном программирование в JavaScript" Алексей Коваленко
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Fwdays
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
Mario Fusco
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
Ganesh Samarthyam
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlin
intelliyole
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performance
intelliyole
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
The Software House
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.
Vadim Dubs
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015
Leonardo Borges
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Data
greenwop
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
Mario Fusco
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In Java
Andrei Solntsev
 

Viewers also liked (14)

Classes & object
Classes & objectClasses & object
Classes & object
Daman Toor
 
String slide
String slideString slide
String slide
Daman Toor
 
Lab exam 5_5_15
Lab exam 5_5_15Lab exam 5_5_15
Lab exam 5_5_15
Daman Toor
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
Daman Toor
 
Uta005
Uta005Uta005
Uta005
Daman Toor
 
Uta005 lecture3
Uta005 lecture3Uta005 lecture3
Uta005 lecture3
vinay arora
 
Uta005 lecture1
Uta005 lecture1Uta005 lecture1
Uta005 lecture1
vinay arora
 
4 java - decision
4  java - decision4  java - decision
4 java - decision
vinay arora
 
2 java - operators
2  java - operators2  java - operators
2 java - operators
vinay arora
 
6 java - loop
6  java - loop6  java - loop
6 java - loop
vinay arora
 
1 java - data type
1  java - data type1  java - data type
1 java - data type
vinay arora
 
3 java - variable type
3  java - variable type3  java - variable type
3 java - variable type
vinay arora
 
Search engine and web crawler
Search engine and web crawlerSearch engine and web crawler
Search engine and web crawler
vinay arora
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Classes & object
Classes & objectClasses & object
Classes & object
Daman Toor
 
Lab exam 5_5_15
Lab exam 5_5_15Lab exam 5_5_15
Lab exam 5_5_15
Daman Toor
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
Daman Toor
 
4 java - decision
4  java - decision4  java - decision
4 java - decision
vinay arora
 
2 java - operators
2  java - operators2  java - operators
2 java - operators
vinay arora
 
1 java - data type
1  java - data type1  java - data type
1 java - data type
vinay arora
 
3 java - variable type
3  java - variable type3  java - variable type
3 java - variable type
vinay arora
 
Search engine and web crawler
Search engine and web crawlerSearch engine and web crawler
Search engine and web crawler
vinay arora
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Ad

Similar to Operators (20)

Java operators
Java operatorsJava operators
Java operators
Shehrevar Davierwala
 
Pj01 4-operators and control flow
Pj01 4-operators and control flowPj01 4-operators and control flow
Pj01 4-operators and control flow
SasidharaRaoMarrapu
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
Ismar Silveira
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
MSohaib24
 
Operators in java
Operators in javaOperators in java
Operators in java
Then Murugeshwari
 
Vcs16
Vcs16Vcs16
Vcs16
Malikireddy Bramhananda Reddy
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)
Chhom Karath
 
Java Program
Java ProgramJava Program
Java Program
Sudeep Singh
 
Groovy
GroovyGroovy
Groovy
Zen Urban
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
Soumya Behera
 
Functional "Life": parallel cellular automata and comonads
Functional "Life": parallel cellular automata and comonadsFunctional "Life": parallel cellular automata and comonads
Functional "Life": parallel cellular automata and comonads
Alexander Granin
 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs
Abhishek Jena
 
Wap to implement bitwise operators
Wap to implement bitwise operatorsWap to implement bitwise operators
Wap to implement bitwise operators
Harleen Sodhi
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
ShashikantSathe3
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppt
henokmetaferia1
 
NetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf EditionNetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf Edition
Paulo Morgado
 
pointers 1
pointers 1pointers 1
pointers 1
gaurav koriya
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
Lukasz Dynowski
 
Vcs23
Vcs23Vcs23
Vcs23
Malikireddy Bramhananda Reddy
 
Java 스터디 강의자료 - 1차시
Java 스터디 강의자료 - 1차시Java 스터디 강의자료 - 1차시
Java 스터디 강의자료 - 1차시
Junha Jang
 
Pj01 4-operators and control flow
Pj01 4-operators and control flowPj01 4-operators and control flow
Pj01 4-operators and control flow
SasidharaRaoMarrapu
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
Ismar Silveira
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
MSohaib24
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)
Chhom Karath
 
Functional "Life": parallel cellular automata and comonads
Functional "Life": parallel cellular automata and comonadsFunctional "Life": parallel cellular automata and comonads
Functional "Life": parallel cellular automata and comonads
Alexander Granin
 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs
Abhishek Jena
 
Wap to implement bitwise operators
Wap to implement bitwise operatorsWap to implement bitwise operators
Wap to implement bitwise operators
Harleen Sodhi
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
ShashikantSathe3
 
Chapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).pptChapter 2&3 (java fundamentals and Control Structures).ppt
Chapter 2&3 (java fundamentals and Control Structures).ppt
henokmetaferia1
 
NetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf EditionNetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf Edition
Paulo Morgado
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
Lukasz Dynowski
 
Java 스터디 강의자료 - 1차시
Java 스터디 강의자료 - 1차시Java 스터디 강의자료 - 1차시
Java 스터디 강의자료 - 1차시
Junha Jang
 
Ad

More from Daman Toor (10)

Lab exam setb_5_5_2015
Lab exam setb_5_5_2015Lab exam setb_5_5_2015
Lab exam setb_5_5_2015
Daman Toor
 
Nested class
Nested classNested class
Nested class
Daman Toor
 
Inheritance1
Inheritance1Inheritance1
Inheritance1
Daman Toor
 
Inheritance
InheritanceInheritance
Inheritance
Daman Toor
 
Lab exp (creating classes and objects)
Lab exp (creating classes and objects)Lab exp (creating classes and objects)
Lab exp (creating classes and objects)
Daman Toor
 
Practice
PracticePractice
Practice
Daman Toor
 
Parking ticket simulator program
Parking ticket simulator programParking ticket simulator program
Parking ticket simulator program
Daman Toor
 
Lab exp (declaring classes)
Lab exp (declaring classes)Lab exp (declaring classes)
Lab exp (declaring classes)
Daman Toor
 
Lab exp declaring arrays)
Lab exp declaring arrays)Lab exp declaring arrays)
Lab exp declaring arrays)
Daman Toor
 
Java assignment 1
Java assignment 1Java assignment 1
Java assignment 1
Daman Toor
 
Lab exam setb_5_5_2015
Lab exam setb_5_5_2015Lab exam setb_5_5_2015
Lab exam setb_5_5_2015
Daman Toor
 
Lab exp (creating classes and objects)
Lab exp (creating classes and objects)Lab exp (creating classes and objects)
Lab exp (creating classes and objects)
Daman Toor
 
Parking ticket simulator program
Parking ticket simulator programParking ticket simulator program
Parking ticket simulator program
Daman Toor
 
Lab exp (declaring classes)
Lab exp (declaring classes)Lab exp (declaring classes)
Lab exp (declaring classes)
Daman Toor
 
Lab exp declaring arrays)
Lab exp declaring arrays)Lab exp declaring arrays)
Lab exp declaring arrays)
Daman Toor
 
Java assignment 1
Java assignment 1Java assignment 1
Java assignment 1
Daman Toor
 

Recently uploaded (20)

Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 

Operators

  • 2. Assignment Operator (=) lvalue = rvalue; • Take the value of the rvalue and store it in the lvalue. • The rvalue is any constant, variable or expression. • The lvalue is named variable. w = 10; x = w; z = (x - 2)/(2 + 2);
  • 3. Mathematical Operators • Addition + • Subtraction - • Multiplication * • Division / • Modulus %
  • 4. Simple Arithmetic public class Example { public static void main(String[] args) { int j, k, p, q, r, s, t; j = 5; k = 2; p = j + k; q = j - k; r = j * k; s = j / k; t = j % k; System.out.println("p = " + p); System.out.println("q = " + q); System.out.println("r = " + r); System.out.println("s = " + s); System.out.println("t = " + t); } } > java Example p = 7 q = 3 r = 10 s = 2 t = 1 >
  • 5. Shorthand Operators +=, -=, *=, /=, %= Common Shorthand a = a + b; a += b; a = a - b; a -= b; a = a * b; a *= b; a = a / b; a /= b; a = a % b; a %= b;
  • 6. Shorthand Operators public class Example { public static void main(String[] args) { int j, p, q, r, s, t; j = 5; p = 1; q = 2; r = 3; s = 4; t = 5; p += j; q -= j; r *= j; s /= j; t %= j; System.out.println("p = " + p); System.out.println("q = " + q); System.out.println("r = " + r); System.out.println("s = " + s); System.out.println("t = " + t); } } > java Example p = 6 q = -3 r = 15 s = 0 t = 0 >
  • 7. Shorthand Increment and Decrement ++ and -- Common Shorthand a = a + 1; a++; or ++a; a = a - 1; a--; or --a;
  • 8. Increment and Decrement > java example p = 6 q = 6 j = 7 r = 6 s = 6 > public class Example { public static void main(String[] args) { int j, p, q, r, s; j = 5; p = ++j; // j = j + 1; p = j; System.out.println("p = " + p); q = j++; // q = j; j = j + 1; System.out.println("q = " + q); System.out.println("j = " + j); r = --j; // j = j -1; r = j; System.out.println("r = " + r); s = j--; // s = j; j = j - 1; System.out.println("s = " + s); } }
  • 9. Arithmetic Operators A Change of Topic Casting long 64 bit l int 32 bit i
  • 10. long 64 bit int 32 bit l i int i; i = (int)l; l = (long)i; Moving Between Buckets "Casting" long l; l = i; long 64 bit int 32 bit l i Widening Narrowing
  • 11. Declaration Size and Type Number Range byte b; // 8 bit integer -2-7 to +2+7 -1 short s; // 16 bit integer -2-15 to +2+15 -1 char c; // 16 bit Unicode 0 to +2+16 int i; // 32 bit integer -2-31 to +2+31 -1 long l; // 64 bit integer -2-63 to +2+63 -1 float f; // 32 bit floating point IEEE754 Standard double d; // 64 bit floating point IEEE754 Standard The Primitive Numbers
  • 12. Casting the Primitives b = (byte)s; b = (byte)c; b = (byte)i; b = (byte)l; b = (byte)f; b = (byte)d; i = (int)b; i = (int)s; i = (int)c; i = (int)l; i = (int)f; i = (int)d; l = (long)b; l = (long)s; l = (long)c; l = (long)i; l = (long)f; l = (long)d; s = (short)b; s = (short)c; s = (short)i; s = (short)l; s = (short)f; s = (short)d; 2'sComplement c = (char)b; c = (char)s; c = (char)i; c = (char)l; c = (char)f; c = (char)d; PositiveOnly f = (float)b; f = (float)s; f = (float)c; f = (float)i; f = (float)l; f = (float)d; d = (double)b; d = (double)s; d = (double)c; d = (double)i; d = (double)l; d = (double)f; FloatingPoint b = (byte)s; b = (byte)c; b = (byte)i; b = (byte)l; b = (byte)f; b = (byte)d; i = b; i = s; i = c; i = (int)l; i = (int)f; i = (int)d; l = b; l = s; l = c; l = i; l = (long)f; l = (long)d; s = b; s = (short)c; s = (short)i; s = (short)l; s = (short)f; s = (short)d; 2'sComplement c = (char)b; c = (char)s; c = (char)i; c = (char)l; c = (char)f; c = (char)d; PositiveOnly f = b; f = s; f = c; f = i; f = l; f = (float)d; d = b; d = s; d = c; d = i; d = l; d = f; FloatingPoint
  • 13. Acceptable Implicit Casts char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit Illegal b = l; l = f; c = s; OK l = b; i = c; f = l
  • 14. char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit Automatic Promotion with Arithmetic Illegal Casts s = s + b; s = s + s; OK s = (short)(s + b); s = (short)(s + s); Arithmetic is never done in 8 or 16 bit containers.
  • 15. char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit Arithmetic Promotion with Mixed Primitives Illegal Casts i = i + l; f = f + d; l = l + f; OK i = (int)(i + l); d = (double)(f + d); l = (long)(l + f); Arithmetic is done in the "widest" type.
  • 16. Implicit Casts in Method Calls char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit Illegal i = st.indexOf(f); OK i = st.indexOf(c); i = st.indexOf(b); For: String st; and, public int indexOf(int ch);
  • 17. Casting A Change of Topic The Logical and Relational Operators
  • 18. Relational Operators > < >= <= == != Primitives • Greater Than > • Less Than < • Greater Than or Equal >= • Less Than or Equal <= Primitives or Object References • Equal (Equivalent) == • Not Equal != The Result is Always true or false
  • 19. Relational Operator Examples public class Example { public static void main(String[] args) { int p =2; int q = 2; int r = 3; Integer i = new Integer(10); Integer j = new Integer(10); System.out.println("p < r " + (p < r)); System.out.println("p > r " + (p > r)); System.out.println("p == q " + (p == q)); System.out.println("p != q " + (p != q)); System.out.println("i == j " + (i == j)); System.out.println("i != j " + (i != j)); } } > java Example p < r true p > r false p == q true p != q false i == j false i != j true >
  • 20. Logical Operators (boolean) && || ! • Logical AND && • Logical OR || • Logical NOT !
  • 21. Logical (&&) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("f && f " + (f && f)); System.out.println("f && t " + (f && t)); System.out.println("t && f " + (t && f)); System.out.println("t && t " + (t && t)); } } > java Example f && f false f && t false t && f false t && t true >
  • 22. Logical (||) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("f || f " + (f || f)); System.out.println("f || t " + (f || t)); System.out.println("t || f " + (t || f)); System.out.println("t || t " + (t || t)); } } > java Example f || f false f || t true t || f true t || t true >
  • 23. Logical (!) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("!f " + !f); System.out.println("!t " + !t); } } > java Example !f true !t false >
  • 24. Logical Operator Examples Short Circuiting with && public class Example { public static void main(String[] args) { boolean b; int j, k; j = 0; k = 0; b = ( j++ == k ) && ( j == ++k ); System.out.println("b, j, k " + b + ", " + j + ", " + k); j = 0; k = 0; b = ( j++ != k ) && ( j == ++k ); System.out.println("b, j, k " + b + ", " + j + ", " + k); } } > java Example b, j, k true 1, 1 > java Example b, j, k true 1, 1 b, j, k false 1, 0 >
  • 25. Logical Operator Examples Short Circuiting with || public class Example { public static void main(String[] args) { boolean b; int j, k; j = 0; k = 0; b = ( j++ == k ) || ( j == ++k ); System.out.println("b, j, k " + b + ", " + j + ", " + k); j = 0; k = 0; b = ( j++ != k ) || ( j == ++k ); System.out.println("b, j, k " + b + ", " + j + ", " + k); } } > java Example b, j, k true 1, 0 > java Example b, j, k true 1, 0 b, j, k true 1, 1 >
  • 26. The Logical and Relational Operators A Change of Topic Manipulating the Bits 10010111
  • 27. Logical Operators (Bit Level) & | ^ ~ • AND & • OR | • XOR ^ • NOT ~
  • 28. Twos Complement Numbers Base 10 A byte of binary +127 01111111 +4 00000100 +3 00000011 +2 00000010 +1 00000001 +0 00000000 -1 11111111 -2 11111110 -3 11111101 -4 11111100 -128 10000000
  • 29. Adding Twos Complements Base 10 Binary +3 00000011 -2 11111110 +1 00000001 Base 10 Binary +2 00000010 -3 11111101 -1 11111111
  • 30. Logical Operators (Bit Level) & | ^ ~ int a = 10; // 00001010 = 10 int b = 12; // 00001100 = 12 a 00000000000000000000000000001010 10 b 00000000000000000000000000001100 12 a & b 00000000000000000000000000001000 8 a 00000000000000000000000000001010 10 b 00000000000000000000000000001100 12 a | b 00000000000000000000000000001110 14 a 00000000000000000000000000001010 10 b 00000000000000000000000000001100 12 a ^ b 00000000000000000000000000000110 6 a 00000000000000000000000000001010 10 ~a 11111111111111111111111111110101 -11 & AND | OR ^ XOR ~ NOT
  • 31. Logical (bit) Operator Examples public class Example { public static void main(String[] args) { int a = 10; // 00001010 = 10 int b = 12; // 00001100 = 12 int and, or, xor, na; and = a & b; // 00001000 = 8 or = a | b; // 00001110 = 14 xor = a ^ b; // 00000110 = 6 na = ~a; // 11110101 = -11 System.out.println("and " + and); System.out.println("or " + or); System.out.println("xor " + xor); System.out.println("na " + na); } } > java Example and 8 or 14 xor 6 na -11 >
  • 32. Shift Operators (Bit Level) << >> >>> • Shift Left << Fill with Zeros • Shift Right >> Based on Sign • Shift Right >>> Fill with Zeros
  • 33. Shift Operators << >> int a = 3; // ...00000011 = 3 int b = -4; // ...11111100 = -4 a 00000000000000000000000000000011 3 a << 2 00000000000000000000000000001100 12 b 11111111111111111111111111111100 -4 b << 2 11111111111111111111111111110000 -16 << Left >> Right a 00000000000000000000000000000011 3 a >> 2 00000000000000000000000000000000 0 b 11111111111111111111111111111100 -4 b >> 2 11111111111111111111111111111111 -1
  • 34. Shift Operator >>> int a = 3; // ...00000011 = 3 int b = -4; // ...11111100 = -4 >>> Right 0 a 00000000000000000000000000000011 3 a >>> 2 00000000000000000000000000000000 0 b 11111111111111111111111111111100 -4 b >>> 2 00111111111111111111111111111111 +big
  • 35. Shift Operator Examples public class Example { public static void main(String[] args) { int a = 3; // ...00000011 = 3 int b = -4; // ...11111100 = -4 System.out.println("a<<2 = " + (a<<2)); System.out.println("b<<2 = " + (b<<2)); System.out.println("a>>2 = " + (a>>2)); System.out.println("b>>2 = " + (b>>2)); System.out.println("a>>>2 = " + (a>>>2)); System.out.println("b>>>2 = " + (b>>>2)); } } > java Example a<<2 = 12 b<<2 = -16 a>>2 = 0 b>>2 = -1 a>>>2 = 0 b>>>2 = 1073741823 >
  • 36. Shift Operator >>> and Automatic Arithmetic Promotion byte a = 3; // 00000011 = 3 byte b = -4; // 11111100 = -4 byte c; c = (byte) a >>> 2 c = (byte) b >>> 2 >>> Right Fill 0 a 00000011 3 a >>> 2 00000000000000000000000000000000 0 c = (byte) 00000000 0 b 11111100 -4 b >>> 2 00111111111111111111111111111111 1073741823 c = (byte) Much to big for byte 11111111 -1
  • 37. Which Operators Operate On What Operators && || ! Unary + - ++ -- + - * / % > < >= <= == != << >> >>> & | ^ ~ = op= etc. floatdouble Floating Point char byteshortintlong Automatic Promotion Except ++ - - Automatic Promotion Except ++ - - Automatic Promotion Except ++ - - Automatic Promotion Except ++ - - Automatic Promotion Except ++ - - Automatic Promotion Except ++ - - Automatic Promotion Automatic Promotion Automatic Promotion Integral boolean Logical Any Object + with String Only Reference Only Not Content
  • 38. Assignment Operator (=) and Classes Date x = new Date(); Date y = new Date(); x = y;
  • 39. Assignment Operator (=) and Classes Date x = new Date(); Date y = new Date(); x = y;
  • 40. Manipulating the Bits A Change of Topic Some Odds and Ends
  • 41. Ternary Operator ? : If true this expression is evaluated and becomes the value entire expression. Any expression that evaluates to a boolean value. If false this expression is evaluated and becomes the value entire expression. boolean_expression ? expression_1 : expression_2
  • 42. Ternary ( ? : ) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("t?true:false "+(t ? true : false )); System.out.println("t?1:2 "+(t ? 1 : 2 )); System.out.println("f?true:false "+(f ? true : false )); System.out.println("f?1:2 "+(f ? 1 : 2 )); } } > java Example t?true:false true t?1:2 1 f?true:false false f?1:2 2 >
  • 43. String (+) Operator String Concatenation "Now is " + "the time." "Now is the time."
  • 44. String (+) Operator Automatic Conversion to a String If either expression_1If either expression_1 or expression_2 evaluates to a string the other will be converted to a string if needed. The result will be their concatenation. expression_1 + expression_2
  • 45. String (+) Operator Automatic Conversion with Primitives "The number is " + 4 "The number is " + "4" "The number is 4"
  • 46. String (+) Operator Automatic Conversion with Objects "Today is " + new Date() "Today is " + "Wed 27 22:12;26 CST 2000" "Today is Wed 27 22:12;26 CST 2000" "Today is " + new Date().toString()
  • 47. Operator Precedence + - ++ -- ! ~ () * / % + - << >> >>> > < >= <= instanceof == != & | ^ && || ?: = (and += etc.) Unary Arithmetic Shift Comparison Logical Bit Boolean Ternary Assignment
  • 48. Passing Classes to Methods class Letter { char c; } public class PassObject { static void f(Letter y) { y.c = 'z'; } public static void main(String[] args) { Letter x = new Letter(); x.c = 'a'; System.out.println("1: x.c: " + x.c); // Prints 1: x.c: a f(x); System.out.println("2: x.c: " + x.c); // Prints 2: x.c: z } }
  • 49. Passing Primitives to Methods class Letter { char c; } public class PassPrimitive { static void f(char y) { y = 'z'; } public static void main(String[] args) { Letter x = new Letter(); x.c = 'a'; System.out.println("1: x.c: " + x.c); // Prints 1: x.c: a f(x.c); System.out.println("2: x.c: " + x.c); // Prints 2: x.c: a } }