0% found this document useful (0 votes)
8 views

Operator in JAVA.docx - Google Docs

Java operators are symbols used to perform operations on variables, categorized into unary, binary, and ternary types. There are eight types of operators in Java: arithmetic, relational, logical, assignment, increment/decrement, bitwise, ternary, and special operators. Each operator serves specific functions, such as performing calculations, comparing values, or manipulating bits.

Uploaded by

Sukhwinder Singh
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)
8 views

Operator in JAVA.docx - Google Docs

Java operators are symbols used to perform operations on variables, categorized into unary, binary, and ternary types. There are eight types of operators in Java: arithmetic, relational, logical, assignment, increment/decrement, bitwise, ternary, and special operators. Each operator serves specific functions, such as performing calculations, comparing values, or manipulating bits.

Uploaded by

Sukhwinder Singh
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
You are on page 1/ 8

‭Q1. Explain various operators available in JAVA.


‭Ans‬‭:-Java operators are symbols that are used to perform‬‭operations on variables and manipulate the‬
v‭ alues of the operands. Each operator performs specific operations.eg:- 5+ 4 Here 5 and 4 are operands,‬
‭and + is operator.‬

‭Operators may be unary, binary or ternary. Unary operators operate on single operand. eg :- ++ and --.‬

‭Binary operators operate on two operands.eg:- all arithmetic operators except + and -.‬

‭Ternary operator operates on three operands.‬

‭There are eight types of operators available in JAVA:-‬

‭1. Arithmetic Operators‬

‭2. Relational operators‬

‭3. Logical Operators‬

‭4. Assignment Operators‬

‭5. Increment / Decrement Operators‬

‭6. Bitwise Operators‬

‭7. Ternary Operators‬

‭8. Special Operators‬

‭ . Arithmetic operators :-‬‭Those operator which are‬‭used for mathematical calculations are known as‬
1
‭arithmetic operator. The various arithmetic operator are :‬

‭(i) (+) Addition or unary plus‬

‭(ii) (-) subtraction or unary minus‬

‭(iii) (*) multiplication‬

‭(iv) ( / ) division‬

‭(v) (%) modulus.‬

‭Example:-‬

‭class arith‬

‭{‬

‭public static void main( String args[] )‬

‭{‬

‭int a=4,b=6,c,d,e,f,g;‬

‭c=a+b;‬
‭System.out.println(" Addition of a and b is " +c);‬

‭d=a-b;‬

‭System.out.println(" Subtraction of a and b is " +d);‬

‭e=a*b;‬

‭System.out.println(" Multiplication of a and b is " +e);‬

‭f=a/b;‬

‭System.out.println(" Division of a and b is " +f);‬

‭g=a%b;‬

‭System.out.println(" Remainder of a and b is " +g);‬

‭}‬

‭}‬

‭ . Relational operators:-‬‭Relational operators are‬‭used to compare the values of operands of same data‬
2
‭type and return either True or False. Relational operator are binary operators. The relational operators‬
‭are:-‬

‭(i) (==) Equals to‬

‭(ii) (<=) Less than or equal to‬

‭(iii) (>=) Greater than or equal to‬

‭(iv) (<) Less than‬

‭(v) (>) greater than‬

‭(vi) ( !=) Not equal to‬

‭Example:-‬

‭class relation‬

‭{‬

‭public static void main(String args[])‬

‭{‬

‭int a=5,b=7;‬

‭System.out.println(" a is equal to b:" + ( a=b));‬

‭System.out.println(" a is not equal to b:" + ( a!=b));‬

‭System.out.println(" a is less than to b:" + ( a<b));‬


‭System.out.println(" a is less than or equal to b:" + ( a<=b));‬

‭System.out.println(" a is greater than to b:" + ( a>b));‬

‭System.out.println(" a is greater than or to b:" + ( a>=b));‬

‭}‬

‭}‬

‭ . Logical Operators:-‬‭Logical operators are used‬‭to combining two or more conditions or complement‬
3
‭the evaluation of the original condition under consideration. The logical Operators are:-‬

‭(i) ( && ) Logical AND‬‭:- Return true if both condition‬‭are true, otherwise false.‬

‭(ii) ( || ) Logical OR:-‬‭Return true if at least one‬‭condition is true, otherwise false.‬

‭(iii) ( ! ) Logical NOT:-‬‭returns true when a condition‬‭is false, otherwise false‬

‭Example:-‬

‭class logic‬

‭{‬

‭public static void main( String args[] )‬

‭{‬

‭int a=4,b=7,c=11;‬

‭System.out.println(" AND Logical :-" +(a>b&&b<c));‬

‭System.out.println(" OR Logical :-" +(a>b||b<c));‬

‭System.out.println(" NOT Logical :-" +(b<c));‬

‭}‬

‭}‬

‭ . Assignment Operator:-‬‭Assignment Operators are‬‭used to assign the result of an expression or value‬


4
‭to a variable. Its symbol is'='.‬

‭eg:- int a;‬

‭a=7; Here '=' is assignment Operator‬

‭The assignment Operators in JAVA are:-‬

‭(i) += Addition assignment‬

‭(ii) -= Subtraction assignment‬

‭(iii) *= Multiplication assignment‬


‭(iv) /= Division assignment‬

‭(v) %= Remainder assignment.‬

‭Example:-‬

‭class assign‬

‭{‬

‭public static void main( String args[] )‬

‭{‬

‭int a =5;‬

‭System.out.println(" Addition assignment is"+ (a+=2));‬

‭System.out.println(" Subtraction assignment is"+ (a-=2));‬

‭System.out.println(" Multiplication assignment is"+ (a*=2));‬

‭System.out.println(" Division assignment is"+ (a/=2));‬

‭System.out.println(" Remainder assignment is"+ (a%=2));‬

‭}‬

‭}‬

‭ . Increment /Decrement Operators:-‬‭Increment /Decrement‬ ‭Operators are unary operators. These are‬
5
‭also called auto- increment or auto-decrement operators. The increment operator adds 1 to the‬
‭operand. eg:- '++i' is equivalent to i=i+1 or i+=1.‬

‭The Decrement Operator subtracts 1 from the operand. eg:- '--i' is equivalent to i=i-1 or i-=1.‬

‭There are two types of increment/ decrement operators:-‬

‭1. Post-increment and pre-increment operators‬

‭2.post-decrement and pre-decrement operators.‬

‭1.a)Post-increment operator:-‬

‭ hen placed after the variable name, the value of the operand is incremented but the previous value is‬
W
‭retained temporarily until the execution of this statement and it gets updated before the execution of‬
‭the next statement.‬

‭Syntax‬‭:-‬

‭i++‬

‭ ).pre-increment operator:-‬‭When placed before the‬‭variable name, the operand’s value is incremented‬
b
‭instantly.‬
‭Syntax‬‭:-‬

‭++i‬

‭Example‬‭:-‬

‭class PrePost‬

‭{ ‬

‭public static void main(String args[]) ‬

‭{ ‬

‭int i = 5; ‬

‭i++; ‬

‭System.out.println(i); ‬

‭++i; ‬

‭System.out.println(i); ‬

‭System.out.println(++i); ‬

‭System.out.println(i++); ‬

‭System.out.println(i); ‬

‭} ‬

‭} ‬

‭ . a)Post-decrement operator‬‭:-When placed after the‬‭variable name, the value of the operand is‬
2
‭decremented but the previous value is retained temporarily until the execution of this statement and it‬
‭gets updated before the execution of the next statement.‬

‭Syntax‬‭:-‬

‭i--‬

‭ ) pre- decrement operator:-‬‭When placed before the‬‭variable name, the operand’s value is‬
b
‭decremented instantly.‬

‭Syntax‬‭:-‬

‭--i‬

‭Example‬‭:-‬

‭class PrePostDe ‬

‭{ ‬

‭public static void main(String args[]) ‬


‭{ ‬

‭int i = 10; ‬

‭i--; ‬

‭System.out.println(i); ‬

‭--i; ‬

‭System.out.println(i); ‬

‭System.out.println(--i); ‬

‭System.out.println(i--); ‬

‭System.out.println(i); ‬

‭} ‬

‭} ‬

‭ .Ternary Operator:-‬‭Ternary operator is also called‬ ‭conditional operator. This operator takes three‬
6
‭operands. The first operand should be an expression and returns boolean value. Second operand or‬
‭expression is executed when result of first operand is true, But if result of first operand is false then,‬
‭third operand or expression is executed.‬

‭Syntax‬‭:-‬

‭Exp1?Exp2:Exp3‬

‭Example‬‭:-‬

‭class ter‬

‭{‬

‭public static void main( String args[])‬

‭{‬

‭int a=5,b=7;‬

‭Result=(a<b)? a is lesser: a is greater;‬

‭}‬

‭}‬

‭ .Bitwise operator:-‬‭Bitwise Operators works on bits‬‭and perform bit by bit operation. They can be used‬
7
‭with any of the integer types. They are used when performing update and query operations of the Binary‬
‭indexed trees. The bitwise Operators are:-‬

‭(i) >> Shift- right‬‭:- Inserts 0 from left and‬‭right most digit is removed( once).‬
‭(ii) << Shift- left:-‬‭Inserts 0 from right and left most digit is removed(once)‬

‭(iii) ~ complement‬‭:- The digit 0 is replaced with‬‭1 and 1 with 0.‬

‭(iv) & Bitwise AND‬‭:- Bit wise AND operation is‬‭performed. if both digits are 1s,it is 1.‬

‭(v) | Bitwise OR:-‬‭Bit wise OR operation is‬‭performed. if either digit is 1, it is 1.‬

(‭ vi) ^ Bitwise Exclusive OR‬‭:-Bitwise Exclusive‬‭OR operation is performed. if one bit is 0 and other is 1,‬
‭it is 1.‬

‭Example‬‭:-‬

‭class bitw‬

‭{‬

‭public static void main( String args[] )‬

‭{‬

‭int a=15,b=13;‬

‭System.out.println("Shift-right is" +(a>>1));‬

‭System.out.println("Shift-left is" +(a<<1));‬

‭System.out.println("Complement is" +(~a));‬

‭System.out.println("Bitwise AND is" +(a&b));‬

‭System.out.println("Bitwise OR is" +(a|b));‬

‭System.out.println("Bitwise Exclusive OR is" +(a^b));‬

‭}‬

‭}‬

‭8. Special Operators:-‬‭JAVA provides two special operators.‬

‭1. ‘instanceof’ operator‬

‭2. Dot operator (.).‬

‭ . 'instanceof' operator:-‬‭It is also called object‬‭reference variables. The operator checks whether the‬
1
‭object belongs to a particular class or interface type.‬

‭Syntax‬‭:-‬

‭<instanceOperatorobject> instanceof <instanceOperatorClass>‬

‭2.Dot operator (.):-‬‭It is used to access the instancr‬‭variable or methods of the objects of a class.‬

‭Syntax‬‭:-‬
‭<instanceOperatorobject>.<instanceVariable>‬

‭OR‬

‭<instanceOperatorobject>.<methods>‬

‭Example‬‭:-‬

‭class main‬

‭{‬

‭int a;‬

‭public static void main( String args[])‬

‭{‬

‭main obj= new main();‬

‭Obj.a=5;‬

‭System.out.println("instanceof operator is"+ obj instanceof main());‬

‭System.out.println("Dot operator is"+ obj.a);‬

‭}‬

‭}‬

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy