Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Operators: A Comprehensive Guide for Beginners, Exercises of Java Programming

r rtgrgrfgfdgfdgfgff fgffgfgf

Typology: Exercises

2020/2021

Uploaded on 01/20/2021

shubha-agarwal-1
shubha-agarwal-1 🇮🇳

5 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java - Basic Operators
Java - Basic Operators
Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups
Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups −
Arithmetic Operators
Arithmetic Operators
Relational Operators
Relational Operators
Bitwise Operators
Bitwise Operators
Logical Operators
Logical Operators
Assignment Operators
Assignment Operators
Misc Operators
Misc Operators
The Arithmetic Operators
The Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators −
Assume integer variable A holds 10 and variable B holds 20, then
Assume integer variable A holds 10 and variable B holds 20, then −
Show Examples
Show Examples
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java Operators: A Comprehensive Guide for Beginners and more Exercises Java Programming in PDF only on Docsity!

Java - Basic OperatorsJava - Basic Operators

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups −Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups −

Arithmetic Operators Arithmetic Operators Relational Operators Relational Operators Bitwise Operators Bitwise Operators Logical Operators Logical Operators Assignment Operators Assignment Operators Misc Operators Misc Operators

The Arithmetic OperatorsThe Arithmetic Operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators −Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators −

Assume integer variable A holds 10 and variable B holds 20, then −Assume integer variable A holds 10 and variable B holds 20, then −

Show ExamplesShow Examples

OperatorOperator (^) DescriptionDescription (^) ExampleExample

  • (Addition) + (Addition) Adds values on either side of the operator.Adds values on either side of the operator. A + B will give 30A + B will give 30
  • (Subtraction) - (Subtraction) Subtracts right-hand operand from left-hand operand.Subtracts right-hand operand from left-hand operand. A - B will give -10A - B will give -
  • (Multiplication) * (Multiplication) Multiplies values on either side of the operator.Multiplies values on either side of the operator. (^) A * B will give 200A * B will give 200 / (Division) / (Division) Divides left-hand operand by right-hand operand.Divides left-hand operand by right-hand operand. (^) B / A will give 2B / A will give 2 % (Modulus) % (Modulus) Divides left-hand operand by right-hand operand and returnsDivides left-hand operand by right-hand operand and returns remainder. remainder. B % A will give 0B % A will give 0 ++ (Increment) ++ (Increment) Increases the value of operand by 1.Increases the value of operand by 1. B++ gives 21B++ gives 21 -- (Decrement) -- (Decrement) Decreases the value of operand by 1.Decreases the value of operand by 1. B-- gives 19B-- gives 19 The Relational OperatorsThe Relational Operators

There are following relational operators supported by Java language.There are following relational operators supported by Java language.

Assume variable A holds 10 and variable B holds 20, then −Assume variable A holds 10 and variable B holds 20, then −

Show ExamplesShow Examples

Show ExamplesShow Examples

OperatorOperator^ DescriptionDescription^ ExampleExample & (bitwise and) & (bitwise and) Binary AND Operator copies a bit to the result if it exists in both operands.Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100(A & B) will give 12 which is 0000 1100 | (bitwise or)| (bitwise or)^ Binary OR Operator copies a bit if it exists in either operand.Binary OR Operator copies a bit if it exists in either operand.^ (A | B) will give 61 which is 0011 1101(A | B) will give 61 which is 0011 1101 ^ (bitwise XOR) ^ (bitwise XOR) Binary XOR Operator copies the bit if it is set in one operand but not both.Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001(A ^ B) will give 49 which is 0011 0001 ~ (bitwise compliment) ~ (bitwise compliment) Binary Ones Complement Operator is unary and has the effect of 'flipping'Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. bits. (~A ) will give -61 which is 1100 0011 in 2's (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. complement form due to a signed binary number. << (left shift) << (left shift) Binary Left Shift Operator. The left operands value is moved left by theBinary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000A << 2 will give 240 which is 1111 0000

(right shift) >> (right shift) Binary Right Shift Operator. The left operands value is moved right by theBinary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. number of bits specified by the right operand. A >> 2 will give 15 which is 1111A >> 2 will give 15 which is 1111

(zero fill right shift) >>> (zero fill right shift) Shift right zero fill operator. The left operands value is moved right by the Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up number of bits specified by the right operand and shifted values are filled up with zeros. with zeros. A >>>2 will give 15 which is 0000 1111A >>>2 will give 15 which is 0000 1111 The Logical OperatorsThe Logical Operators

The following table lists the logical operators −The following table lists the logical operators −

Assume Boolean variables A holds true and variable B holds false, then −Assume Boolean variables A holds true and variable B holds false, then −

Show ExamplesShow Examples

OperatorOperator^ DescriptionDescription^ ExampleExample && (logical and)&& (logical and)^ Called Logical AND operator. If both the operands are non-zero, then the conditionCalled Logical AND operator. If both the operands are non-zero, then the condition becomes true. becomes true. (A && B) is false(A && B) is false || (logical or) || (logical or) Called Logical OR Operator. If any of the two operands are non-zero, then the conditionCalled Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. becomes true. (A || B) is true(A || B) is true ! (logical not)! (logical not) Called Logical NOT Operator. Use to reverses the logical state of its operand. If aCalled Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. condition is true then Logical NOT operator will make false. !(A && B) is true!(A && B) is true The Assignment OperatorsThe Assignment Operators

Following are the assignment operators supported by Java language −Following are the assignment operators supported by Java language −

Show ExamplesShow Examples

variable x = (expression)? value if true : value if falsevariable x = (expression)? value if true : value if false

Following is an example −Following is an example −

ExampleExample

public public classclass TestTest {{ public public staticstatic voidvoid (^) mainmain((StringString (^) argsargs[])[]) (^) {{ int int aa,, bb;; a a (^) == 1010 ;; b b == ((aa ==== 11 )) ?? 2020 :: 3030 ;; System System..outout..printlnprintln(( "Value of b is : ""Value of b is : " ++ (^) bb );); b b (^) == ((aa ==== 1010 )) ?? 2020 :: (^3030) ;; System System..outout..printlnprintln(( "Value of b is : ""Value of b is : " ++ (^) bb );); } } } }

This will produce the following result −This will produce the following result −

OutputOutput

Value of b is : 30 Value of b is : 30 Value of b is : 20 Value of b is : 20 instanceof Operatorinstanceof Operator

This operator is used only for object reference variables. The operator checks whether the object is of a particular type (class type or interface type). instanceofThis operator is used only for object reference variables. The operator checks whether the object is of a particular type (class type or interface type). instanceof

operator is written as −operator is written as −

( Object reference variable ) instanceof ( Object reference variable ) instanceof (class/interface type)(class/interface type)

If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result will be true.If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result will be true.

Following is an example −Following is an example −

ExampleExample

Live Demo Live Demo

publicpublic classclass TestTest {{ public public staticstatic voidvoid (^) mainmain((StringString (^) argsargs[])[]) (^) {{ String String namename == "James""James";; // following will return true since name is type of String // following will return true since name is type of String boolean boolean (^) resultresult (^) == namename (^) instanceofinstanceof StringString;; System System..outout..printlnprintln(( resultresult );); } } } }

This will produce the following result −This will produce the following result −

OutputOutput

true true

This operator will still return true, if the object being compared is the assignment compatible with the type on the right. Following is one more example −This operator will still return true, if the object being compared is the assignment compatible with the type on the right. Following is one more example −

ExampleExample

class class VehicleVehicle {}{} public public classclass CarCar extendsextends VehicleVehicle {{ public public staticstatic voidvoid mainmain((StringString argsargs[])[]) {{ Vehicle Vehicle aa == newnew CarCar();(); boolean boolean (^) resultresult (^) == aa instanceofinstanceof CarCar;; System System..outout..printlnprintln(( resultresult );); } } } }

This will produce the following result −This will produce the following result −

OutputOutput

Live Demo Live Demo Live Demo Live Demo

CategoryCategory^ OperatorOperator^ AssociativityAssociativity Postfix Postfix expression++ expression--expression++ expression-- Left to rightLeft to right Unary Unary ++expression –-expression +expression –expression ~ !++expression –-expression +expression –expression ~! Right to leftRight to left Multiplicative Multiplicative * / %* / % Left to rightLeft to right AdditiveAdditive^ + -+ -^ Left to rightLeft to right Shift Shift (^) << >> >>><< >> >>> Left to rightLeft to right Relational Relational < >< > <= >= instanceof<= >= instanceof Left to rightLeft to right EqualityEquality == !=== != Left to rightLeft to right Bitwise ANDBitwise AND && Left to rightLeft to right Bitwise XOR Bitwise XOR ^^ Left to rightLeft to right Bitwise ORBitwise OR^ ||^ Left to rightLeft to right Logical ANDLogical AND^ &&&&^ Left to rightLeft to right Logical ORLogical OR^ ||||^ Left to rightLeft to right Conditional Conditional ?:?: Right to leftRight to left Assignment Assignment = += -= *= /= %= ^= |= <<= >>= >>>== += -= *= /= %= ^= |= <<= >>= >>>= Right to leftRight to left What is Next?What is Next?

The next chapter will explain about loop control in Java programming. The chapter will describe various types of loops and how these loops can be used in JavaThe next chapter will explain about loop control in Java programming. The chapter will describe various types of loops and how these loops can be used in Java

program development and for what purposes they are being used.program development and for what purposes they are being used.