Showing posts with label Core Java. Show all posts

Program for Bitwise Operators.

0
03 Java Operators.
Program for Bitwise Operators:
     //XORDemo.java 
     class XORDemo
             {
                     public static void main(String[] args)
                      {
                        System.out.println(5 & 6);
                        System.out.println(5 | 6);
                        System.out.println(5 ^ 6);

                         System.out.println(true & false);

                         System.out.println(true | false);
                         System.out.println(true ^ true);
                          }
         }

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 Home
Continue reading →

Program for Switch case example.

0
03 Java Operators.
Program for Switch case example:
//SwitchDemo.java
class SwitchDemo
   {
static void m1(int a)
{
System.out.println("before switch");

switch(a)
{
case 1:
System.out.println("in case1 start");
System.out.println("in switch");
System.out.println("in switch");
System.out.println("in case 1 end");

case 2:

System.out.println("in case 2 start");
System.out.println("in switch");
System.out.println("in switch");
System.out.println("in case 2 end");
break;

case 3:

System.out.println("in case 3 start");
System.out.println("in switch");
System.out.println("in switch");
System.out.println("in case 3 end");
break;

                        default :

System.out.println("in default start");
System.out.println("in switch");
System.out.println("in switch");
System.out.println("in default end");
}

System.out.println("after switch");

}
public static void main(String[] args)
{
//m1(1);
/*
there is no break in case 1: hence case 1: and case 2: both will be executed

before switch

in case1 start
in switch
in switch
in case 1 end
in case 2 start
in switch
in switch
in case 2 end
after switch
*/

//m1(2);

/*
before switch
in case 2 start
in switch
in switch
in case 2 end
after switch
*/
m1(20);
m1(30);
/*
before switch
in default start
in switch
in switch
in default end
after switch
*/
}
}

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 Next
Continue reading →

Program for Relational Operators.

0
03 Java Operators.
Program for Relational Operators.
//RelationalOperatorsDemo.java 
public class RelationalOperatorsDemo
{
    public static void main(String args[])
          {
                     int x = 10, y = 5;

                     System.out.println("x > y : "+(x > y));
                     System.out.println("x < y : "+(x < y));
                     System.out.println("x >= y : "+(x >= y));
                     System.out.println("x <= y : "+(x <= y));
                     System.out.println("x == y : "+(x == y));
                     System.out.println("x != y : "+(x != y));

                     System.out.println(x > y);
                     System.out.println("x > y: "+ x > y);
                     System.out.println("x > y: "+ 10 > 5);
                     System.out.println("x > y: "+ "10" > 5);
                     System.out.println("x > y: 10" > 5);

                     System.out.println(x < y + x > y);

                     System.out.println(10 < 5 + 10 > 5);
                     System.out.println(10 < 15 > 5);

                                 //System.out.println(true > 5);


                     System.out.println((x < y) + (x > y));


                     System.out.println((10 < 5) + (10 > 5));
                     System.out.println((false) + (true));

                     System.out.println((x < y) + (x > y) + "");


                     System.out.println((10 < 5) + (10 > 5));

                     System.out.println((false) + (true) + "");

                     System.out.println((x < y) + ""+ (x > y) );

                     System.out.println((10 < 5) + "" +(10 > 5));
                     System.out.println((false) + "" +(true));
                     System.out.println("false" + "" +(true));
                     System.out.println("false" + true);
                     System.out.println("false" + "true");
                     System.out.println("falsetrue");

                     System.out.println(""+(x < y) +  (x > y) );
              }
  }


 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 Next
Continue reading →

Program for Logical Operators.

0
03 Java Operators.
Program for Logical Operators:
//LogicalOperatorsDemo.java 
   public class LogicalOperatorsDemo
            {
                   public static void main(String args[])
                         {
                                   boolean x = true;
                                   boolean y = false;

                                   System.out.println("x & y : "+(x & y));

                                   System.out.println("x && y : "+(x && y));
                                   System.out.println("x | y : "+(x | y));
                                   System.out.println("x || y: "+(x || y));
                                   System.out.println("x ^ y : "+(x ^ y));
                                   System.out.println("!x : "+(!x));
                         }
      }

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 Next
Continue reading →

Program for Increment And Decrement Operators.

0
03 Java Operators.
Program for Increment And Decrement Operators:
++ : is called as increment operator because it increaments variable value by 1.
--   : is called as increment operator because it decreses variable value by 1.
we use these operators either before or after variable.
Prefix increment/ decrement operators
=============================
If we use these operators before variable, they are called prefix operators.

Postfix increment/ decrement operators
==============================
If we use these operators after variable, they are called postfix operators.

Difference:
========
If use them as prefix operators, JVM first increaments / decrements its value then JVM uses the current value in the expression.

If use them as postfix operators, JVM first uses the current value then it increaments / decrements that variable value.

//IncrementAndDecrementOperators.java 
class IncrementAndDecrementOperators
      {
            public static void main(String[] args)
                   {
                         int a = 10;
                         a++;
                         System.out.println(a);//11

                         int b = 20;

                         ++b ;
                         System.out.println(b);//21
                         System.out.println();

/*

Note: we cannot observe the difference in prefix and postfix operators if we donot use the value at the same statement or line.
check the below code, you can find the difference in both operators
*/

                         int x = 30;

                         System.out.println(x++);
                         System.out.println(x);

                         System.out.println();


                         int y = 40;

                         System.out.println(++y);
                         System.out.println(y);

                         System.out.println();


                         int z = 5;
                         System.out.println(z++ + ++z + --z + z-- + z - z-- +z);

  }

    }


 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 Next
Continue reading →

Program for Equality Operators.

0
03 Java Operators.
Program for Equality Operators:
//EqualityOperators.java 
class EqualityOperators
{
      public static void main(String[] args)
{
System.out.println(10 == 10);
System.out.println(10 == 5);

System.out.println();

System.out.println(10 != 10);
System.out.println(10 != 5);

System.out.println();

System.out.println(true == true);
System.out.println(true == false);

System.out.println();

System.out.println(true != true);
System.out.println(true != false);

System.out.println();

System.out.println('a' == 'b');
System.out.println('a' != 'b');

//scjp bits

System.out.println(10 == 10.0);
System.out.println(97 == 'a');

//System.out.println(97 == "a");
//CE: incomparable types: int and String

System.out.println("a" == "a");
System.out.println(0 == 0);
System.out.println(0 == 0.0);
System.out.println(-0.0 == 0.0);
System.out.println(-10 == 10);

int a = 10;
int b = 20;

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));

System.out.println(a = b);
System.out.println(a = 20);
System.out.println(a);
System.out.println(20);

System.out.println(a == b);
System.out.println(a);
System.out.println(b);
}
}

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
Continue reading →

Program for Arithmetic Operators.

0
03 Java Operators.
Program for Arithmetic Operators:
//ArithmeticOperators.java 
class ArithmeticOperators
  {
public static void main(String[] args)
{
System.out.println(10 + 20 * 2 + 40 / 20 % 2 * 30 - 10);

System.out.println(10 + (20 * 2) + 40 / 20 % 2 * 30 - 10);
System.out.println(10 + 40 + (40 / 20) % 2 * 30 - 10);
System.out.println(10 + 40 + 2 % 2 * 30 - 10);
System.out.println(10 + 40 + (2 % 2) * 30 - 10);
System.out.println(10 + 40 + 0 * 30 - 10);
System.out.println(10 + 40 + (0 * 30) - 10);
System.out.println(10 + 40 + 0 - 10);
System.out.println((10 + 40) + 0 - 10);
System.out.println((50 + 0) - 10);
System.out.println(50 - 10);
System.out.println(40);

//1. we cannot divide integer by ZERO it leads to exception java.lang.ArithmeticException: /by Zero

//System.out.println(10/0); RE: java.lang.ArithmeticException: /by Zero
//System.out.println(0/0); RE: java.lang.ArithmeticException: /by Zero
System.out.println(0/10); //0

//but we can divide integer number with 0.0, because internally that integer number will be converted into double type
System.out.println(10/0.0);     // System.out.println(10.0/0.0); Infinity

//2. we can divide floating point numbers (float and double) by zero, output is Infinity or -Infinity
//3. if we divide 0.0 by 0 output is NaN (Not a Number)

System.out.println(10.0/0);    //Inifinity
System.out.println(0.0/0);      //NaN
System.out.println(0.0/10);    //0.0

System.out.println(-10.0/0);   //-Inifinity
System.out.println(-0.0/0);     //NaN
System.out.println(-0.0/10);   //-0.0
}
   }

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 Next
Continue reading →

Program for Compound Operator.

0
03 Java Operators.
Program for Compound Operator:
//CompoundOperator.java 
class CompoundOperator
 {
public static void main(String[] args)
{
int a = 10;
int b = a + 10;
int c = a += 10;

System.out.println(a);
System.out.println(b);
System.out.println(c);

int a = 10;

a += 10;
=> a = a + 10;
=> a = 10 + 10;
System.out.println(a);//20

a += 'a';

a = a + 'a';
a = 10 + 'a';
a = 10 + 97;
a = 107;

System.out.println(a);//107

a += "a";

a = a + "a";
a = 10 + "a";
a = "10" + "a";
a = "10a";

//on string we can only apply += operator,
//+= operator is also overloaded
String s = "a";
s += "b"; //=> s = s + "b";
System.out.println(s);//ab

s += 10;
System.out.println(s);//ab10

//s -= 10;//=> s = s - 10;
//System.out.println(s);

s += 10 - 20; //=> s = s + (10-20);
System.out.println(s);
}
          }

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 Next
Continue reading →

Recent Posts