[Go to site: main page, start]

0% found this document useful (0 votes)
4 views9 pages

Java Module 1 3

The document provides an overview of Java operators, expressions, and type conversions. It categorizes operators into arithmetical, relational, logical, bitwise, and assignment types, along with examples and precedence rules. Additionally, it explains implicit and explicit type conversions, detailing how Java handles different data types during operations.

Uploaded by

nagadhanush700
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Java Module 1 3

The document provides an overview of Java operators, expressions, and type conversions. It categorizes operators into arithmetical, relational, logical, bitwise, and assignment types, along with examples and precedence rules. Additionally, it explains implicit and explicit type conversions, detailing how Java handles different data types during operations.

Uploaded by

nagadhanush700
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OPERATORS

EXPRESSIONS
TYPE CONVERSIONS

Java Fundamentals | Module 1.3


OPERATORS

Operators

Arithmetical Equality Relational Logical Bit-wise Assignment

Unary Binary Ternary Simple Compound Expression

Type of Operator Operator Symbols with Meanings


Unary: + – ++ ––
Arithmetical Binary: + – * / %
Ternary: ?:
Simple: =
Assignment Compound: += –= *= /= %= &= ^= |=

> < >= <=


Relational

== (Equal to) != (Not equal)


Equality

&& (AND) || (OR) ! (NOT)


Logical

& | ~ ^ >> << >>>


Bitwise

, (comma) . (member) instanceof


Others
OPERATORS – EXAMPLES
Arithmetic operators Relational operators Logical operators

Operator Name Example Operator Action Example Operator Action Example / Result

+ Addition 12 + 4.9 → 16.9 == Equal 5 == 5 → true ! Logical NOT !(5==5) → false

– Subtraction 3.98 – 4 → –0.02 != Not equal 5 != 5 → false && Logical AND 5<6 && 6<6 → false

* Multiplication 2 * 3.4 → 6.8 < Less than 5 < 5.5 → true || Logical OR 5<6 || 6<5 → true

/ Division 9 / 2.0 → 4.5 <= Less/equal 5 <= 5 → true

% Remainder 13 % 3 → 1 > Greater 5 > 5.5 → false

>= Greater/equal 6.3 >= 5 → true

Bitwise operators Conditional (Ternary) operator

Operator Action

~ Bitwise Negation Syntax: expression1 ? expression2 : expression3

int min = (m < n) ? m : n;


& Bitwise AND

| Bitwise OR

^ Bitwise XOR Comma / instanceof operators

<< Left Shift


Comma: evaluates left-to-right in for-loops:
>> Right Shift (signed) for (int i=0, j=10; i<j; i++, j--) { ... }

instanceof: checks object type at runtime:


>>> Right Shift (unsigned)
if (obj instanceof String) { ... }
OPERATORS PRECEDENCE AND ASSOCIATIVITY
Highest Priority
Operators Associativity Note
( ) [ ] ->.

() [] . ++ -- (postfix) L to R Highest
! ~ ++ – – (type) * & sizeof

++ -- (prefix) ! ~ (type) new R to L */%

+–
* / % L to R
<< >>
+ – L to R
< <= > >=
<< >> >>> L to R == !=

< <= > >= instanceof L to R &

^
== != L to R
|
& L to R
&&
^ L to R
||

| L to R ?:

&& L to R = += –= *= /= etc.

, Lowest
|| L to R

?: R to L

= += -= *= /= %= &= ^= |= <<= >>= >>>= R to L

, L to R Lowest
COMPARATIVE CHART: OPERATORS IN C vs JAVA

Category Operator C Java

Arithmetic Floor Division — — (use [Link]())

Arithmetic Power — — (use [Link]())

Arithmetic Increment ++ ++

Arithmetic Decrement -- --

Assignment Power & assign — — (use [Link]())

Logical AND && &&

Logical OR || ||

Logical NOT ! !

Membership in / not in — instanceof (type check only)

Identity is / is not — == for primitives, .equals() for objects

Pointer Address-of & ✓ — (no pointers; references used instead)

Pointer Dereference * ✓ — (automatic via references)

Pointer Struct member -> ✓ — (use . for all object access)

Bitwise Unsigned right shift >>> — >>> (Java only)

Ternary ?: ✓ ✓

Key Java-only additions:


>>> unsigned right shift | instanceof type checking | String + concatenation | No pointer operators | .equals() for object equality
EXPRESSIONS
An expression is a combination of operators and operands that evaluates to a single value.

3 * 4 % 5

Lvalue Rvalue

Consider the assignment: a = b;

Refers to the address that 'a' represents. Means the content of the address that b represents.

Known at compile time. Not known until runtime.

Says where to store the value. Tells what is to be stored.

Cannot be an expression or constant. Can be an expression or constant.

Types of expressions in Java:


Arithmetic: a + b * c | Boolean: x > 0 && y < 10 | Assignment: x = 5 | Method call: [Link](4.0)
TYPE CONVERSIONS
Java performs automatic (implicit) and manual (explicit) type conversions.
If operands in an expression have different types, Java promotes the smaller type to the larger type.
In assignment, the right-side value is converted to the type of the left-side variable.

// Widening (automatic): smaller → larger double (largest)


int i = 10; ← wider
double d = i; // int promoted to double automatically
[Link](d); // 10.0 float

// Narrowing (explicit cast required): larger → smaller


double f = 10.7; long
int j = (int) f; // explicit cast – truncates decimal
[Link](j); // 10
int
// Mixed expression promotion
int x = 7, y = 5;
double z = (double) x / y; // explicit cast → 1.4 short / char
double z2 = x / y; // integer division → 1.0 (NOT 1.4)

byte (smallest)
← narrower
TYPE CONVERSIONS – IMPLICIT (WIDENING)
Implicit (Widening) Type Conversion
The value on the right side of the assignment is automatically converted to the type of the left side (target variable).

int i = 10;
float f;
f = i; // i is promoted to float automatically 10.000000
[Link]("%f%n", f); // 10.000000

int j;
double d2 = 10.7;
j = (int) d2; // explicit cast needed to narrow 10
[Link]("%d%n", j); // 10

byte short int long float double

char

Widening: byte → short → int → long → float → double (also char → int)
TYPE CONVERSIONS – EXPLICIT (NARROWING / CASTING)
Explicit Type Conversion (Casting)
The programmer manually forces conversion using the cast operator. Syntax:

(targetType) expression

With explicit cast Without explicit cast


int x = 7, y = 5; int x = 7, y = 5;
float z; float z;
z = (float) x / y; z = x / y; // integer division!
[Link]("%f%n", z); [Link]("%f%n", z);

1.400000 1.000000

Promotion
char c; in mixed expressions: Rule: result type = widest type in expression
long double
int j;
float f; double
double d, r;
r = (c * j) + (f / j) - (f + d); float
// r → double (largest type in expression)

unsigned long int

long int

unsigned int

int

short

You might also like