Explain Various Operator in Java, Also Comment How They Are Different from C++.

Topics: C, Relational operator, Programming constructs Pages: 5 (1312 words) Published: April 15, 2012
explain various operator in java, also comment how they are different from c++. Operators
After having seen the basic types in Java and how to declare variables of each type, we are now going to show what basic operations can be performed with and on variables of the basic types. Below is a table summarizing Java operators organized by the type of operation the operator is used for. There are 8 types of operator that is follow :-

1 )- Arithmetic Operators
2)- Logic Operators
3)- Bitwise Operators
4)- Assignment Operators
5)- The Conditional Operators
6)- Increment and decrement operators
7)- Special operator
8)- Relational operator

Description of these operators :-
Arithmetic Operators
The arithmetic operators -, +, * and / can be used to perform respectively subtraction, addition, multiplication and division with integers and reals. Example :-
2int result = 3 * 56;
double answer = 3.14 + 2.72;
Logic Operators
The logic operators &&, ||, ! and ^ can be used to perform respectively logical and, or, not and exclusive-or on boolean operands. Example:
2System.out.println(true || false); /* outputs true */ System.out.println(! true); /* outputs false */
Bitwise Operators
The bitwise operators &, |, ^, ~, and >>> can be used to perform bitwise and, or, exclusive-or, negation, left shift and right shift on integer operands. Note that there are two types of right shift: >> and >>>. The difference being that >> copies the sign bit into the high-order positions and >>> copies zeros into the high-order positions. Example:

2System.out.println(12 & 5); /* outputs 4 because 1100 & 0101 = 0100 = 4 */ System.out.println(3 >= all require a location (a variable) on the left side and an expression of the same type (or one that could be promoted to the same type) on the right side. The basic assignment operator = evaluates the expression on the right and stores the resulting value in the location on the left. Example:

2int foo;
foo = 5 + 3; /* evaluate 5 + 3, store the result in foo */
The Conditional Operators
The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed. && Conditional-AND

|| Conditional-OR
The following program, ConditionalDemo1, tests these operators: class ConditionalDemo1 {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
if((value1 == 1) && (value2 == 2))
System.out.println("value1 is 1 AND value2 is 2"); if((value1 == 1) || (value2 == 1))
System.out.println("value1 is 1 OR value2 is 1"); }
Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson). This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result." The following program, ConditionalDemo2, tests the ?: operator: class ConditionalDemo2 {

public static void main(String[] args){
int value1 = 1;
int value2 = 2;
int result;
boolean someCondition = true;
result = someCondition ? value1 : value2;
Because someCondition is true, this program prints "1" to the screen. Use the ?: operator instead of an if-then-else statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments). Increment and decrement operators

Increment and decrement operators are unary operators that add or subtract one from their operand, respectively. They are commonly implemented in imperative...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Essay about Operators in C
  • how macroeconomics is different from microeconomics. Research Paper
  • Essay on Operator Overloading in C++
  • How a Resume Is Different from a Job Application Essay
  • How Is Discrimination Different from Prejudice and Stereotyping? Essay
  • Java vs. C++ Essay
  • Essay on How Different Is Old Labour from New Labour
  • Essay about TDA31 1 3 Explain How Different Social

Become a StudyMode Member

Sign Up - It's Free