JAVA

1. Java-overview 2. Features of Java 3. History of Java 4. HW SW Req for Java SE 7 5. Java Environment Setup 6. Basic Requirement 7. Comments 8. My First Program in Java 9. Line Break 10. Escape Sequences 11. Literals in Java 12. Identifiers in Java 13. Variables in Java 14. Data-Types in Java 15. Declare Variables 16. Reserved Key Words 17. printf() 18. Chained & Embedded state 19. Prog.to print sum of 2 nos 20. Scanner class 21. Prg.to print sum & average 22. Area & Perimeter of Rectangle 23. Area Circumference of Circle 24. Prg.for Simple Calculator 25. Operatos in Java 26. Ex. Of Operators 27. Swap two numbers 28. Hierarchy of Operators 29. if( ) statement 30. Prg.to check +ve,-ve or zero 31. if..else statement 32. Prg.to check Odd or Even 33. Prg.to print larger of 2 nos 34. Prg.to print largest of 3 nos 35. Prg.to check divisibility 36. Prg.to check print range 37. for. . . loop statement 38. prg.to print series of nos 39. Prg.to check divisibility 40. prg.to print sum of nos 41. Prg.to print Pyramid 42. prg.for Factorial 43. prg.to fill screen 44. prg.for largest/smallest no 45. prg.to print reverse no 46. prg.to add each digit 47. prg.for sum of series 48. prg.for fibonacci series 49. prg.to check Prime nos 50. prg. Prime no from 1 to 100 51. prg.Specified Prime nos 52. while( ) statement 53. do. . While statement 54. break & continue statement 55. switch case statement 56. Array in Java 57. print reverse order using array 58. Ascending/Descending order 59. prg.to search no in Array 60. Multidimensional Array 61. String in Java 62. String with spaces 63. prg.to print string in reverse 64. prg. to count A,E,I,O,U vowel 65. User defined methods 66. Methods program example 67. General Purpose Programs 68. Loan & EMI calculation prg. 69. Table print prg. 70. Leap year program 71. lower to UPPER case prg. 72. Age Distribution prg. 73. Bank note calculation prg. 74. Simple Interest prg. 75. Compound Interest prg. 76. Simple Depreciation prg. 77. Reducing bal.Depreciation prg. 78. Marksheet prg. 79. Income Tax prg. 80. Time calculator prg. 81. Distance converter prg. 82. Volume Air Calculation prg. 83. Time to fill Water Tank prg. 84. Salary Calculation prg. 85. Total Sale Calculation prg. 86. Male/Female percentage prg. 87. Library Rent prg. 88. Office Expance prg. 89. Total Salary Calculation prg. 90. Profit or Loss prg. 91. Total Profit/Loss prg. 92. Palindrome program 93. Java Interview Part1 94. Java Interview Part2 95. Java Interview Part3 96. Java Interview Part4 97. Java Interview Part5 98. Java Interview Part6 99. Java Interview Part7 100. Java Interview Part8 101. Java Interview Part9 102. Java Interview Part10 103. Java Interview Part11
Pr.Pg Next Pg

Operators in java tutorials

  • The variables, which are declared and defined, are the operands which are operated upon by the operators.

  • Operators specify what operations are to be performed on the operands.

  • Some operators require two operands, while few operators require only one operand.

  • Java includes many operators, which fall into different categories as below:

  1. Arithmetic Operators

  2. Unary Operators

  3. Relational Operators

  4. Assignment Operators

  5. Logical Operators

  6. Bitwise Operators

Arithmetic Operators

  • There are 5 arithmetic operators in Java

  • Following table shows all the arithmetic operators supported by Java. Assume variable A holds 20 and variable B

  • holds 30 than:

Operator

Description

Example

+

Adds two operands

A + B will give 50

-

Subtracts second operand from the first

A - B will give -10

*

Multiply both operands

A * B will give 600

/

Divide numerator by de-numerator

A / B will give 0

%

Modulus Operator and remainder of after an integer division

A % B will give 20

 

Unary Operators

  • As operator’s works on single operands, they are called unary operators.

  • Most common unary operator is unary minus, where a minus sign precedes a numeric variable or expression.

  • Unary minus operator is different from arithmetic minus (Substation). Arithmetic substation or minus operator requires two operands.

Example

-200

- ( a + b )

- 4 * ( 2 + 3 )

  • Other Unary Operator

  1. Increment operator ++The increment operator ++adds 1 to operand.

  2. Decrement operator - - The decrement operator -- subtracts 1 to operand.

  • Both Increment and Decrement operator can be used in two different ways.

  • Pre: If unary operator precedes the operand for example (++ operand or - - operand), then value of operand will be increased / decreased by one, before it is utilized within program.

  • Post: If unary operator follows the operand for example (operand++ or operand- -), then value of operand will be increased / decreased by one, after the execution of statement within program.

Relational Operators

  • The relational operators allow you to compare two values.

  • You can determine if the two values are equal or not and can check which of the two values is the larger.

  • All of the relational operations return a Boolean result.

  • In the case of the 'equal to' and 'not equal to' operations, any two compatible data types can be compared.

  • However, for the comparison operators that determine if one value is greater than another, the data type must support the concept of ordering.

  • This includes all of the basic numeric types but not Boolean values as it is not sensible to consider 'true' to be either greater than or less than 'false'.

Operator

Meaning

Less than

Greater than

= =

Equal to

< =

Less than or Equal to

> =

Greater than or Equal to

! =

Not equal to

 

Assignment Operators

  • Simple assignment operators: The equal (=) sign is used for assigning a value to variable. The left-hand side has to be a variable and right-hand side of equal sign has to be value of variable.

Example:

a = 5; // this statement will assign 5 to variable a.

b = 2 + 10; // this statement will solve right hand side equation and assign 12 to b.

x = y = z = 20; //this example of multiple assignment, it will assign 20 to x, y and z.

  • Shorthand assignment operator: The plus equal (+=), minus equal (-=), multiply equal (*=), divide equal (/=), modulo equal (%=), the use of shorthand assignment operator has three advantages listed below:

  • What appears on the left-hand side need not be repeated and therefore it becomes easier to write. The statement is more concise and easier to read.

  • You can also use shorthand assignment operator as follow. Assume A = 5 and B=2 then check following

Operator

Regular Expression

Shorthand assignment,

Expression can be written as

Result

+ =

A = A + B ;

A += B;

7

- =

A = A – B ;

A - = B;

3

* =

A = A * B ;

A * = B;

10

/ =

A = A / B ;

A / = B;

2

% =

A = A % B ;

A % = B;

1

 

Logical Operators

  • The logical operator && logical AND, || meaning OR, ! meaning NOT.

  • The logical operator &&and || are used when we want to test more than one condition and makes decisions.

  • When result of both operands are true (T), && (AND) operators gives true result, otherwise it gives false.

  • When result of any operands is true (T), || (OR) operators gives true result, otherwise it gives false.

  • ( ! ) NOT operators reverse Boolean result.

  • Truth table for each logical operators are below:

Result of A

Condition

Result of B

Condition

AND condition

A && B

OR condition

A || B

Not Condition

! A

T

T

T

T

F

T

F

F

T

F

F

T

F

T

T

F

F

F

F

T

 

Bitwise Operators

  • Bitwise operator works on bits and performs bit by bit operation. The truth tables for &&, ||, and ^ are as Follows:

p

q

p && q

p || q

p ^ q

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

1

1

 

Assume if a=50 and b=15 now in binary format they will be shown as below:

a = 110010

b = 1111

 

Now let apply Binary operator in a and b

110010

1111

- - - --------

a &b = 0010 which is 2

a | b = 1111 which is 63

a ^b = 1101 which us 61

  • The Bitwise operators supported by Java are listed in the following table. Assume variable A holds 60 and variable B holds 13 than:

Operator

Description

Example

&

Binary AND Operator copies a bit to the result

if it exists in both operands.

(A & B) will give 12 which is 0000 1100

|

Binary OR Operator copies a bit if it exists in

either operand.

(A | B) will give 61 which is 0011 1101

^

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

~

Binary Ones Complement Operator is unary

and has the effect of 'flipping' bits.

(~A ) will give -60 which is 1100 0011

<< 

Binary Left Shift Operator. The left operands

value is moved left by the number of bits

specified by the right operand.

A << 2 will give 240 which is 1111 0000

>> 

Binary Right Shift Operator. The left operands

value is moved right by the number of bits

specified by the right operand.

A >> 2 will give 15 which is 0000 1111

Pr.Pg border                                              Next Pg