0% found this document useful (0 votes)
13 views11 pages

Java Operators Overview and Examples

Here are the programs to solve the problems: 1. length = 15 breadth = 27 area = length * breadth perimeter = 2 * (length + breadth) print("Area is:", area) print("Perimeter is:", perimeter) 2. num1 = 35 num2 = 75 if num1 == num2: print("Numbers are equal") else: print("Numbers are not equal")

Uploaded by

milliongate1122
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)
13 views11 pages

Java Operators Overview and Examples

Here are the programs to solve the problems: 1. length = 15 breadth = 27 area = length * breadth perimeter = 2 * (length + breadth) print("Area is:", area) print("Perimeter is:", perimeter) 2. num1 = 35 num2 = 75 if num1 == num2: print("Numbers are equal") else: print("Numbers are not equal")

Uploaded by

milliongate1122
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

JAVA

OPERATORS
Operators
are symbols that carry out mathematical or logical functions
on an operand or operands such as literals, constants,
variables and objects.

Unary Operators
bitwise complement (~), negation (-), increment (++) and
decrement (- -) have only one operand. They can change
their operand's value without the use of an assignment
operator (=).
Binary Operators
have exactly two operands and uses the assignment
operator for the result.

Ternary Operators
has at most three operands. In Java, the only ternary
operator is the conditional operator (? :).
Arithmetic Operators
Operators Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
++ Increment
-- Decrement
Relational Operators
are considered as binary operators since they
show the relationship between two operands. It
yield a true or false value.
Operators Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not Equal to
Logical Operators
as logical operators are governed by logical rules,
truth tables are necessary to evaluate logical
operations. It is strongly suggested that the
programmer keeps the ff. truth tables in mind
when evaluating logical operations.

Operators Description
! NOT
& AND
| OR
&& short-circuit AND
|| short-circuit OR
AND
The binary operator AND operator returns false
except when both operands have a true value.

Operand1 Operand2 Result


false & true false
false & false false
true & false false
true & true true
OR
The binary operator OR operator evaluates to true
except when both operands are false.

Operand1 Operand2 Result


false | true true
true | true true
true | false true
false | false false
XOR
The binary operator XOR (^) returns true except
when both operands does not have the same
value.

Operand1 Operand2 Result


true ^ false true
false ^ true true
false ^ false false
true ^ true false
NOT
The unary NOT (!) operator is complimentary.

Operand2 Result
! false true
! true false
Coding Time:
1. Length and breadth of a rectangle are
15 and 27 respectively. Create a
program that will calculate the area
and perimeter of the rectangle.
2. Create a program to check if the two
numbers 35 and 75 are equal.

Common questions

Powered by AI

The ternary operator in Java is the only operator that takes three operands and is used for conditional evaluations. Its syntax is: condition ? expr1 : expr2; where 'condition' is evaluated, if true, 'expr1' is returned, otherwise 'expr2' is returned. For example, in the code `int result = (a > b) ? a : b;`, if 'a' is greater than 'b', 'result' will be assigned the value of 'a', otherwise it will be assigned the value of 'b' .

The XOR operator, denoted by '^', returns true if exactly one of its operands is true, but not both. This differs from the standard OR operator '|', which returns true if at least one of the operands is true. XOR is useful for operations where you want to toggle between two states without both being true. For example, XOR can be used in situations where you want a condition to be true if only one of two conditions is met (e.g., if you want to ensure exactly one input is provided, but not both).

In Java, a program can calculate the area and perimeter of a rectangle using the formulae: area = length * breadth and perimeter = 2 * (length + breadth). Here's a simple implementation: ```java int length = 15; int breadth = 27; int area = length * breadth; int perimeter = 2 * (length + breadth); System.out.println("Area: " + area); System.out.println("Perimeter: " + perimeter); ``` This code calculates the area and perimeter for a rectangle with length 15 and breadth 27 .

Unary operators in Java perform operations on a single operand. Examples include bitwise complement (~), negation (-), increment (++), and decrement (--). They can modify the operand's value without using an assignment operator, which is distinct from binary operators that require two operands to perform an operation. Unary operators are especially useful for simple operations like incrementing or negating values. Binary operators, on the other hand, operate on two operands and perform operations like addition (+), subtraction (-), and logical comparisons (e.g., ==, !=).

To determine if both inputs to a function are true in a Java program, you would use the logical AND operator '&&'. Here's an example: ```java public boolean areBothTrue(boolean input1, boolean input2) { return input1 && input2; } ``` In this example, the function `areBothTrue` takes two boolean inputs and returns true only if both inputs are true. This utilizes the short-circuit evaluation property of the '&&' operator: if the first input is false, the second is not evaluated .

Truth tables are essential tools for understanding logical operations because they provide a systematic way to visualize how logical operators like AND, OR, and NOT work. By using truth tables, a programmer can determine the outcome of any logical expression based on possible combinations of its operands' truth values. This helps prevent logical errors in code by allowing developers to anticipate how a logical statement will evaluate before implementing it. For instance, the AND and OR operators produce specific true or false results based on operand values, which can be confidently predicted and tested using truth tables .

Developers may choose to use bitwise operators over logical operators when they require direct manipulation of data at the binary level, such as for low-level programming tasks, optimizing performance, or when working with binary data streams. Bitwise operations are generally faster since they directly operate on the processor's binary. For example, developers can use the bitwise AND '&' to clear specific bits in a binary number, like turning off a flag in a bitmask: `value = value & ~FLAG;`, where FLAG has only one bit set. This method efficiently manipulates specific bits without affecting others .

In Java, relational operators are used to compare two numbers to determine their relationship. The '==' operator checks if two numbers are equal, while '!=' checks if they are not equal. Other operators include '<' for less than, '<=' for less than or equal to, '>' for greater than, and '>=' for greater than or equal to. These operators evaluate to a boolean value of true or false. For example, given two numbers x and y, you can use 'if (x == y)' to check equality, 'if (x != y)' for inequality, 'if (x > y)' to check if x is greater, and so on .

The short-circuit logical operators '&&' and '||' in Java stop evaluating as soon as the result is determined. For '&&', if the first operand is false, the second is not evaluated because the result will be false regardless. Similarly, for '||', if the first operand is true, the second is not evaluated because the whole expression will be true. In contrast, non-short-circuit operators '&' and '|' evaluate both operands regardless of the first operand's result, which may be necessary for expressions where both sides have side effects that must be executed .

Programmers might prefer using increment (++) and decrement (--) operators for their simplicity, readability, and performance efficiency. These operators increase or decrease a variable's value by one in a more concise manner, reducing the chances of errors compared to using explicit addition (e.g., x = x + 1) or subtraction (e.g., x = x - 1). Furthermore, compilers often optimize these operations better, potentially resulting in faster execution in performance-critical sections of code .

You might also like