• Home
  • Introduction
  • Compiling and Running an Application
  • Basic Language Elements
  • Object Oriented Programming
  • objects
  • Bitwise operators
  • Samples
  • Java Control Flow Statements

Bitwise operators

Java provides Bit wise operators to manipulate the contents of variables at the bit level.
These variables must be of numeric data type ( char, short, int, or long). Java provides seven bitwise
operators. They are AND, OR, Exclusive-OR, Complement, Left-shift, Signed Right-shift, and Unsigned Right-shift.

The result of applying bitwise operators between two corresponding bits in the operands is shown in the Table below.



A
1
1
0
0
B
1
0
1
0
~A
0
0
1
1
A&B
1
0
0
0
A | B
1
1
1
0
A ^ B
0
1
1
0


Compound operators

The compound operators perform shortcuts in common programming operations. Java has eleven compound assignment operators.

Syntax:

argument1 operator = argument2.

The above statement is the same as, argument1 = argument1 operator argument2. An example program is shown below that demonstrates the different Compound operators in java.

Conditional operators

The Conditional operator is the only ternary (operator takes three arguments) operator in Java. The operator evaluates the first argument and, if true, evaluates the second argument. If the first argument evaluates to false, then the third argument is evaluated. The conditional operator is the expression equivalent of the if-else statement. The conditional expression can be nested and the conditional operator associates from right to left:(a?b?c?d:e:f:g) evaluates as (a?(b?(c?d:e):f):g)

Type conversion

allows a value to be changed from one primitive data type to another. Conversion can occur explicitly, as specified in the program, or implicitly, by Java itself. Java allows both type widening and type narrowing conversions.


In java Conversions can occur by the following ways:

• Using a cast operator (explicit promotion)
• Using an arithmetic operator is used with arguments of different data types (arithmetic promotion)
• A value of one type is assigned to a variable of a different type (assignment promotion)


Operator Precedence

The order in which operators are applied is known as precedence. Operators with a higher precedence are applied before operators with a lower precedence. The operator precedence order of Java is shown below. Operators at the top of the table are applied before operators lower down in the table. If two operators have the same precedence, they are applied in the order they appear in a statement.
That is, from left to right. You can use parentheses to override the default precedence.



postfix
unary
creation/caste
multiplicative
additive
shift
relational
equality
bitwise AND
bitwise exclusive OR
bitwise inclusive OR
logical AND
logical OR
ternary
assignment
[] . () expr++ expr–
++expr –expr +expr -expr ! ~
new (type)expr
* / %
+ -
>>>>>
<<= >>= instanceof
== !=
&
^
|
&&
||
?:
= “op=”


Example

In an operation such as,
result = 4 + 5 * 3
First (5 * 3) is evaluated and the result is added to 4 giving the Final Result value as 19. Note that ‘*’ takes higher precedence than ‘+’ according to chart shown above. This kind of precedence of one operator over another applies to all the operators.



SAMPLE PROGRAMS

Sample1 Printing Hello World
class HelloWorld
{
   public static void main(String args[])
    {
      System.out.println("Hello World");
    }

}

Output
Hello World

Sample2

Getting arguement from command line
public class UseArgument {

    public static void main(String[] args) {
         System.out.print("Hi, ");
         System.out.print(args[0]);
         System.out.println(". How are you?");
    }

}

Output

java UseArgument Ram
Hi, Ram.How are you?

Sample 3

Getting year from command line to find the leap year or not
public class LeapYear {
     public static void main(String[] args) {
     int year = Integer.parseInt(args[0]);
     boolean isLeapYear;

     // divisible by 4
     isLeapYear = (year % 4 == 0);

     // divisible by 4 and not 100
     isLeapYear = isLeapYear && (year % 100 != 0);
     // divisible by 4 and not 100 unless divisible by 400
     isLeapYear = isLeapYear || (year % 400 == 0);
     System.out.println(isLeapYear);
  }
}

Output

java LeapYear 2004
true
java LeapYear 1900
false


Copyright 2019, Nationalinstituteonline.com

Powered by National3c.com