Reading:  

Quick introduction to Java Programming language - Part 1


Operators in Java

Operator is a symbol that is used to do some specific operations.Types of operators in java:

  • Arthematic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Comparison Operators
  • Unary Operators

Arthematic Operators:

These operators are used to perform athematic operations like addition, substraction, division etc.We will get the number as result from this operations.

Here “+” symbols is also used for concatenation purpose.If any operand is string then “+” acts as a concatenation.

Example:

10+30 = 40
"abc"+10 ="abc10"
"ab”+"cd"="abcd"

Example:

 

public class AddNumbs {
public static void main(String[] args){
int a =10;
int b = 8;
int sum=a+b;
System.out.println("the sum of two number is:" + sum); //here, “+” is acting as concatenation
}
}

Relational Operators:

These operators is used to compare the relation between two variables. We will get the boolean values(true/false) as result from this operation.

Relation Operators are:

Operators

Description

Greater than

Lesser than

>=

Greater than or equal

<=

Lesser than or equal

==

Comparison

!=

Not equal

=

Assignment

 

Example:

public class Example {
public static void main(String[] args){
int a =10;
int b = 8;
System.out.println( a == b ); // returns false
System.out.println( a != b ); // returns true
}
}

 

Logical Operators: 

These operators takes two boolean inputs and we will get one boolean output.Logical Operators are:

Operators

Description

&&

Logical AND

||

Logical OR

!

Logical NOT

 

The AND works: The OR works: The NOT works:
True && True = True
True && False = False
False && True = False
False && False = False
True || True = True
True || False = True
False || True = True
False || False = False
!True = False
!False = True

Example:

public class Example {
public static void main(String[] args){
int a =10;
int b = 8;
System.out.println( (a >= 9) && (b <= 10)); // returns true
System.out.println( (a > b) || (b> 10) ); // returns true
}
}

 

Assignment Operators: 

 

These operator is used to assign the values.Assignment Operators are:

Operators

Description

=

Assignment operator

/=

Divide and assignment operator

*=

Multiply and assignment operator

-=

Subtract and assignment operator

+=

Add and assignment operator

%=

Modulus and assignment operator

<<=

Left shift AND assignment operator

>>=

Right shift AND assignment operator

|=

Bitwise inclusive OR and assignment operator

&=

Bitwise AND assignment operator

^=

Bitwise exclusive OR and assignment operator

Example:

public class AddNumbs {
public static void main(String[] args){
int a = 0;
int b = 8;
a+=b; // a now have evalue 8
System.out.println("Value of a is :" + a);
}
}

Unary Opertors:

These operators will work for only one operand.

Types of Unary Operators:

  • Increment ++;
  • Decrement --;

Example: Increment ++

public class AddNumbs {
public static void main(String[] args){
int a = 0;
a++;
System.out.println("Value of a is :" + a); // 1
}
}

Example: Decrement --

public class AddNumbs {
public static void main(String[] args){
int a = 1;
a--;
System.out.println("Value of a is :" + a); // 0
}
}

 

Bitwise Operators:

Java provides bitwise operators and bit shift operations on integral types.

Bitwise Binary Operations:

Four bit operators in Java.

  • ~ - Unary complement
  • & - Bitwise and
  • ^ - Bitwise exclusive or
  • | - Bitwise inclusive or
  • <<-Binary Left Shift Operator
  • >>-Binary Right Shift Operator
  • >>>-Shift right zero fill operator

Examples:

1010 & 0101 = 0000
1100 & 0110 = 0100

1010 | 0101 = 1111
1100 | 0110 = 1110

~1111 = 0000
~0011 = 1100

1010 ^ 0101 = 1111
1100 ^ 0110 = 1010

 

Conditional Operator:

are ternary operators which has three operands.


Syntax:
(Expression) ? (true) : (false);


Example:

int big = (a>b) ? a:b

 

Main method example

.....
public static void main(String args[])
{
int a=100;
int b = 20;
int big=(a>b)?a:b;
System.out.println(“big value:”+big); //output: big value:100
}
.....

 

Instanceof Operator:

These operatorused to check the object is of a particular type or not.This operator returnsBoolean value true/false.

Syntax:

(Object reference variable) instanceof (class type)

Example:

public class Test{
public static void main(String args[]){
String subject="Maths";
boolean result =subjectinstanceofString;//comparing the values
System.out.println( result ); // Output: True
}
}

In next chapter we will explore looping statements

 

 

 

 

 

Description

This tutorial is targetted for beginers seeking a quick get on with guide on Java programming language. 3 parts include 

  1. Java basics
  2. Object oriented programming
  3. Advance concepts

Each part is subdivided in multiple chapters.

Java basics has the following chapters

  1. Introduction
  2. Environment Setup
  3. Basic Syntax
  4. Objects and Classes
  5. Basic Data Types
  6. Variable Types
  7. Modifier Types
  8. Basic Operators
  9. Loops
  10. Decision Making
  11. Numbers Class
  12. Character and String Class
  13. Arrays
  14. Date and Time
  15. Regular Expression
  16. Methods
  17. Streams, Files and I/O
  18. Exceptions Handling

Thanks for reading and as always, your feedback is very important to us. Let us know how we can improve and if you found any issues with this write up, send us a correction.



Audience

Beginners or students seeking a refresher on Java language

Author: Subject Coach
Added on: 9th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!