Skip to main content

Operators and Expressions

Introduction

    Java Supports a rich set of operators.  An operator is a symbol that tells the computer to perform certain mathamatical or logical manipulataion. Operators are use in programes to manipulate data and variables. Usually it is part of mathmatical or logical expressions. There are some categories of java operators as below:

  1.     Arithmatic Opertaors
  2.     Relational Operators
  3.     Logical Operators
  4.     Assignments Operators
  5.     Increment and Decrement Operators.
  6.     Conditional Operators
  7.     Bitwise Operators
  8.     Special Operators.

1. Arithmatic Operators:

    Arithmatic operators are use to construct mathemaical expressions an in algebra. Java supports all the basic arithmatic operators.


    Arithmatic operators are use as below:

        a-b

        a-b=10

        -14%3=-2

Example code is below:

    public class ArithmaticOperations {

public static void main(String[] args) {

// TODO Auto-generated method stub

float a=20.5f, b=4.4f;

System.out.println(" a = "+a);

System.out.println(" b = "+b);

System.out.println(" a+b = "+(a+b));

System.out.println(" a-b = "+(a-b));

System.out.println(" a*b = "+(a*b));

System.out.println(" a/b = "+(a/b));

System.out.println(" a%b = "+(a%b));

}

}

Output:
     a = 20.5
     b = 4.4
     a+b = 24.9
     a-b = 16.1
     a*b = 90.200005
     a/b = 4.659091
     a%b = 2.8999996

2. Relational Operators:
    We compare two quantities, and depending on there relation, take certain decision. For example, If we compare age of two persons, or comparing price of two items and so on we use relational operators. An expression such as
    a < b    or    x < 18
Code:
public class RelatinalOperators {
public static void main(String[] args) {
float a=15.05f, b=15.0f , c=15.0f;
System.out.println(" a = "+a);
System.out.println(" b = "+b);
System.out.println(" c = "+c);
System.out.println(" a < b = "+(a<b));
System.out.println(" a > b = "+(a>b));
System.out.println(" a == c = "+(a==b));
System.out.println(" a <= c = "+(a<=b));
System.out.println(" a >= c = "+(a>=b));
System.out.println(" b != c = "+(b!=c));
}
}

Output: 
 a = 15.05
 b = 15.0
 c = 15.0
 a < b = false
 a > b = true
 a == c = false
 a <= c = false
 a >= c = true
 b != c = false

3. Logical Operators:
    Java has three logical operators:
The logical operators && and || are use when we wnat to form compound conditions by combining two or more relations. For example,
    a > b && a || b

4. Assignment Operators:
    Assignment operator are use to assign the value of an expression to a variable.  For example,
    v op = exp;
Where v is variable, exp is an expression and op is a java binary operator.

5. Increment and Decrement Operators:
    Java has two very useful operators generally not found in other languages. These are increment and decrement operators.
        ++    and   -
while operator ++ ands 1 and operator - subtarcts 1. Both are unary operators. For example,
    m++    OR        ++m
    a--        OR        --a;
    a= m++        OR        a=++m
    b= a--            OR        b=--a
    The increment and decrement operators extensively use in for and while loops.
    m=5;
    y=++m;
So in above example the y having 6 value because m value will be increment first by 1 and then assign to y. so the y and m value is 6
    m=5;
    y=m++;
So in above example the y haveing the 5 value because it is assign first and then increment the value of m by one.
So the m value will be 6 and the y value will be 5.

6. Conditional Operators: 
    If there is any ternary operator avalabel then it is calld conditional operators. The charactor pair with ? : is a ternary operators availabe in java.
    exp1 ? exp2 : exp 3
Here the exp1, exp,2 and exp3 are the expression. In normal if....else function is as follows:
if(a>b)
    x=a;
else
    x=b;
In above example if first statement, a>b is true then the a value will assign to x, if first condition, a>b is false b value will be assign to x. We can do the same thing into conditional operators like,
        x=(a>b) ? a : b;

7. Bitwise Operators:
    Java supports some of the special operators known as Betwise Operators for manipulation of data at values of bit level. These operators are use for testing the bits, shifting to right or left according to conditions or requirements. These operators are not applied on float or double.

8. Special Operators:
    Java supports special operators such as instanceof operators and member selection operators ( . ).

Instanceof is an object reference operators and return true if the object on the left hand side is an instance of the class given in right hand side. For example,
    person instanceof student
is true if the person belogs to the class student, otherwise return false.

The dot operator is use to access the instance variables and methods of class objects. For example,
        person.age;
        person.salary();

Comments

Popular posts from this blog

Introduction of Java

 What is Java? Java is an object-oriented programming language developed by Sun Microsystem in 1995. It is a combination of C and C++ features with some essential and additional concepts. Java is suited for both standalone and web application development. Java can run on any hardware OR software environment which make it platform independent. Java Example:                                                                                           class Simple{     public static void main(String args[]){          System.out.println("Welcome In Java World");     } } In above example the 'Simple' is a Class name, public is access specifier , Main is a method with argument, St...

Java Package

 Introduction      A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Here, we will have the detailed learning of creating and using user-defined packages. Simple example of java package The package keyword is used to create a package in java. //save as Simple.java   package mypack;   public class Simple{    public static void main(String args[]){       System.out.println("Welcome to package");      }   }   How to access package from another package? There are three ways to access the package from outside the package. 1. import package.*; 2. import package.classname; 3. fully qualified name. 1. Using packagename.*      If you use package.* the...

Java Classes, Object and Methods, Constructors

 Defining a class     A class is a user-defined data type or group of object with a template or blueprint that serves to define its properties. It is a logical entity. Once a class type has been define, we can create " Variables " which are similar to basic type declaration. In java, variables are termed as instance of class, which actual object. The basic form class defination is as follows: class classname [extends superclassname]{     [fields declaration]     [method declaration] } The classname and superclassname are valid java identifiers. The keyword extends indicate that superclassname properties extended into classname. It is a pert of inheritance. (We will discuss inheritance in later part.) Everything inside square brackets is optional that means we can use as per the requirements. For example, class Empty{ } In above example, the Empty class is class name. This class does not have any properties and therefor cannot do anything. A java cla...