Java while loop / Chapter -3/Flow Control



javabychetan.blogspot.com


Iterative Statement:-
This is the second part of the flow control.

a.) while loop:-
If we don't know no. of iteration in advance then we should go for the while loop.

Syntax:-


while(b)
{
where b should be boolean type
Action
}


1. The arguments should be the boolean type. If we are trying to provide any other type then we will get compile time error.

Example:-

Java-while loop / Chapter -3/Flow Control

javabychetan.blogspot.com

2.Iterative Statement:-
This is the second part of the flow chart.


a.) while loop:-
If we don't know no. of iteration in advance then we should go for the while loop.

Syntax:-

while(b)
{
                                                         where b should be boolean type
  Action
}


1. The arguments should be the boolean type . If we are trying to provide any other type then we will get compile time error.

Example:-

Java switch / Chapter -2 / Flow Control



Java switch

java switch statement:-





  • This is the second part of selection Statement. if -else we have already discussed in previous chapter 1 / Flow Control.



  • If multiple options are available then it is not recommended to use nested if-else.To meet this requirement we should go for the switch ( ) Statement.


Syntax:-

                    switch ( x )

                    {


                     case 1: Action -1

                                 break;


                     case 2: Action -2

                                 break;



                     case 3: Action -3

                                 break;




                     case 4: Action -4

                                 break;



                     default : default Action


                      }

Java-switch ( ) / Chapter -2 / Flow Control

2. switch ( ):- 
  • This is the second part of selection Statement . if -else we have already discussed in previous chapter 1 / Flow Control.
  • If multiple options are available then it is not recommended to use  nested if-else.To meet this requirement we should go for the switch ( ) Statement.
Syntax:-
                    switch ( x )
                    {

                     case 1: Action -1
                                 break;


                     case 2: Action -2
                                 break;

                     case 3: Action -3
                                 break;

                     case 4: Action -4
                                 break;
                     
                     default : default Action
                     
                      }

Java if else statement / Chapter -1/ Flow Control


Flow Controls shows the execution flow of the program.
Flow Controls describes that the order in which the statement will be executed at run -time.


Flow Control is divided into 3 parts:-



  1. Selection Statement

  2. Iterative Statement

  3. Transfer Statement





Java-if - else / Chapter -1/ Flow Control

Flow Controls shows the execution flow of the program.
Flow Controls describes that the order in which the statement will be executed at run -time.

Flow Control is divided into 3 parts:-

  1. Selection Statement
  2. Iterative Statement
  3. Transfer Statement


Java Evolution order of operands / Chapter -13/ Operator & Assignments



Evolution order of operands

Java Evolution Order Of Operands


In JAVA we have only operator precedence but not operand precedence before applying any operator all operands will be evaluated from Left →Right.

example:-


class Test
{
      public static void main(String [ ] args)
      {
           System.out.println(m1(1) + m1(2) * m1(3)  / m1(4) + m1(5)  * m1(6) );
      }
      public static int m1(int i)
      {
             System.out.println( i );
return i;
           }
}

Java-Evolution order of operands / Chapter -13/Operator & Assignments

In JAVA we have only operator precedence but not operand precedence before applying any operator all operands will be evaluated from Left →Right.

example:-


class Test
{
      public static void main(String [ ] args)
      {
           System.out.println(m1(1) + m1(2) * m1(3)  / m1(4) + m1(5)  * m1(6) );
           }
      public static int m1(int i)
      {
             System.out.println( i );
                   return i;
           }
}


Java Operator Precedence / Chapter -12/Operator & Assignments

 Java Operator Precedence

Java Operator Precedence



If multiple operators are being used in a statement then which operator will execute first.
To solve this problem here is the complete list of operators with their precedence.


1.  Unary Operator:-
These operators will be applied only one operand.


Java Operator Precedence

2.Arithmetic Operator:-


These operators will be applied only two operands.


Java Operator Precedence

 

 

 

 

3. Shift Operators:-


Java Operator Precedence

Java-Operator Precedence / Chapter -12/Operator & Assignments


If multiple operators are being used in a statement then which operator will execute first.
To solve this problem here is the complete list of operators with their precedence.








1.  Unary Operator:-
These operators will be applied only one operand.










2.Arithmetic Operator:-

These operators will be applied only  two operands.










3. Shift Operators:-





Java new Operators and [ ] operator / Chapter -11/Operator & Assignments




java new Operator:-


1. "java new operator" is used to create objects.It allocates the memory to object.



example:-

Test     t    = new    Test ( );

2. After creating an object constructor will be executed to perform initialization of objects.Hence constructor is not meant for object creation, it is meant for initialization of objects.


3.In java, there is only "new" keyword for allocating the memory but not the "delete" keyword to deallocating the memory, because the destruction of the useless object (to clean or free the memory) is the responsibility of Garbage Collector.


Java-new Operators and [ ] operator / Chapter -11/Operator & Assignments

           
 

   new Operator:-

1. "new operator" is used to create objects.It allocates the memory to object.

example:-

Test     t    = new    Test ( );

2. After creating an object constructor will be executed to perform initialization of objects.

Hence constructor is not meant  for object creation , it is meant for initialization of objects.


3.In java, there is only "new" keyword for allocating the memory but not "delete" keyword because the destruction of the useless object (to clean or free the memory) is the responsibility of Garbage Collector.


Java Ternary Operators / Chapter -10/Operator & Assignments /



java ternary operator or conditional operator


In whole JAVA conditional operator is the only ternary operator.


How To Use It?
It is very simple to use.
First, it checks the condition, if the condition is true then it executes next statement after ? symbol .
If the condition is false then it executes statement after (colon) : the symbol.


Syntax with an example.

example 1.

int a=(10<20) ? 50 : 60;

System.out.println(a);

Output is 50

Java-Conditional Operators / Chapter -10/Operator & Assignments /

In whole JAVA this is the only  ternary operator .

How To Use It?
It is very simple to use.
First, it checks the condition, if the condition is true then it executes next statement after ? symbol .
If the condition is false then it executes statement after : symbol.

Syntax with an example.


example 1.

int a=(10<20) ? 50 : 60;

System.out.println(a);

Output is 50


Java Assignment Operators / Chapter -9/Operator & Assignments


java assignment operatorJava Assignment  Operators


In Java, there are three assignment operators.Assignment operator always follows Right To Left Associativity.



  1. Simple Assignment



  2. Chained Assignment



  3. Compound Assignment



 

1.Simple Assignment:-
We can perform simple assignment at the time of declaration.


example:-

int a=10;

2.Chained Assignment :-
We can't  perform simple assignment at the time of declaration.


example:-
int a,b,c,d;
a=b=c=d=20;
System.out.println(a+"....."+b+"....." +c+"....."+d)

output is

Java-Assignment Operators / Chapter -9/Operator & Assignments


In Java, there are three assignment operators.Assignment operator always follows Right To Left Associativity.

  1. Simple Assignment
  2. Chained Assignment
  3. Compound Assignment
1.Simple Assignment:-
We can perform simple assignment at the time of declaration.
example:-


int a=10;


2.Chained Assignment :-
We can't  perform simple assignment at the time of declaration.
example:-
int a,b,c,d;
a=b=c=d=20;
System.out.println(a+"....."+b+"....." +c+"....."+d)

output is

Java-Logical Operators (& &, | | ) / Chapter -8/Operator & Assignments



java &&, java ||, logical operator

java &&, java ||, logical operator
Short Circuit Operators are quite similar to bitwise operators except for the following differences.



java &&, java ||, logical operator

Case 1.

x&&y    =>>  y will be evaluated if and only if x is true.If x is false then y won't be evaluated.

 

Case 2.

x  | | y   =>> y will be evaluated if and only if x is false.If x is true then y
won't be evaluated.

 


Example 1.

Java-Short Circuit Operators (& &, | | ) / Chapter -8/Operator & Assignments


Short Circuit Operators are quite similar to bitwise operators except for the following differences.
Case 1.
x&&y    =>>  y will be evaluated if and only if x is true.If x is false then y won't be evaluated.

Case 2.
x  | | y   =>> y will be evaluated if and only if x is false .If x is true 
then y won't be evaluated.


Example 1.

Java Bitwise Operators (&,|,^,~,!) / Chapter -7/Operator & Assignments



java bitwise operator


java bitwise operator


In JAVA there are five bitwise operators available.



  1. Bitwise AND ( & )     [both boolean and integral type]

  2. Bitwise OR    (  |  )      [both boolean and integral type]

  3. Bitwise XOR  ( ^ )      [both boolean and integral type]

  4. Bitwise Complement Operator ( ~ )   [only for integral types]

  5. Bitwise Complement Operator ( ! )   [only for boolean types]





1. Bitwise AND ( & ):-


If we apply bitwise AND (&) between two arguments then if and only if it will return true if both arguments are true otherwise it will return false.


example for boolean:-

 


System.out.println(true & true );         output  is    true

 

System.out.println(true & false);         output  is    false

 



System.out.println(false & false);        output  is    false
example for integrals:-

Java - Bitwise Operators (&,|,^,~,!) / Chapter -7/Operator & Assignments

In JAVA there are five bitwise operators available. 

  1. Bitwise AND ( & )     [both boolean and integral type]
  2. Bitwise OR    (  |  )      [both boolean and integral type]
  3. Bitwise XOR  ( ^ )      [both boolean and integral type]
  4. Bitwise Complement Operator ( ~ )   [only for integral types]
  5. Bitwise Complement Operator ( ! )   [only for boolean types]



1. Bitwise AND ( & ):-

If we apply bitwise AND (&) between two arguments then if and only if it will return true if both arguments are true otherwise it will return false.

example for boolean:-


System.out.println(true & true );         output  is    true



System.out.println(true & false);         output  is    false



System.out.println(false & false);        output  is    false


example for integrals:-

Java instanceof Operator / Chapter -6 / Operator & Assignments


java instanceof

Java instanceof  Operator


1.We can apply instanceof operator is only for objects types. the instanceof operator checks whether the given objects is related to a particular type or not.


java instanceof

 example:-


java instanceof

 

Java - instanceof Operators / Chapter -6 / Operator & Assignments

1.We can apply instanceof operator is only for objects types. instanceof operator checks whether the given objects is related to a particular type or not.

 example:-

 

Java Equals Operators / Chapter -5/Operator & Assignments


java equals operators

Java Equals Operator


1. The result of Equality Operator is boolean either true or false.These are Equality operators in java.

= =, !=
 2.We can apply Equality Operators for primitive type and boolean type.
example:-
System.out.println(10==20) ;       output is             false
System.out.println(10!=20) ;        output is              true
System.out.println('a'=='b') ;      output is             false
System.out.println('a'==97) ;        output is              true
System.out.println(false==false) ;  output is            true

3. We can apply equality operator for objects.
If both references are pointing to the same object then equality operator returns true.
If both references are not pointing to the same object then equality operator returns false.
example:-
Test t1=new Test( );
Test t2=new Test( );
Test t3=t1;

                  System.out.println(t1==t2) ; output is false
System.out.println(t1==t3) ; output is true



Difference between Equality Operator and .equals( ) method



In java Equality operators( == , != )are meant for reference comparison(address comparison).
.equals( ) method is meant for content comparison .
example:-
String s1= new String("javabychetan");
String s2 =new String ("javabychetan");




        System.out.println(s1==s2) ;             output is false


        System.out.println(s1.equals(s2)) ;           output is true




Java - Equality Operators / Chapter -5/Operator & Assignments

1. The result of Equality Operator is boolean either true or false.These are Equality operators in java.

          = =, !=

 2.We can apply Equality Operators for primitive type and boolean type.

example:- 

System.out.println(10==20) ;       output is             false

System.out.println(10!=20) ;        output is              true

 System.out.println('a'=='b') ;      output is             false

System.out.println('a'==97) ;        output is              true

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

 

3. We can apply equality operator for objects.

If both references are pointing to the same object then equality operator returns true.

If both references are not  pointing to the same object then equality operator returns false.

 example:-

Java Relational Operators / Chapter -4 /Operator & Assignments



relational operators


java relational operators


1.These are the relational operators in java.


                          < ,<=,>,>=


2.These operators can be used for every primitive data type. But we can't apply these operators for boolean type.


example:-


System.out.println(10<20);    output  is       true


System.out.println('b'<20);    output  is         false   (hint: b's unicode value is greater )


System.out.println('b'<99.7);    output  is      true (hint: b's unicode value is lower)


System.out.println(true<false);   Compile Time Error(Cannot Applied to Boolean)


 


3.Relational Operators cannot be applied on Objects types.


example:-

Java - Relational Operators / Chapter -4 /Operator & Assignments

javabychetan.blogspot.com

1.These are the relational operators in java.

                          < ,<=,>,>=

2.These operators can be used for every primitive data type. But we can't apply these operators for boolean type.

example:-

System.out.println(10<20);    output  is       true

System.out.println('b'<20);    output  is         false   (hint: b's unicode value is greater )

System.out.println('b'<99.7);    output  is      true (hint: b's unicode value is lower)

System.out.println(true<false);   Compile Time Error(Cannot Applied to Boolean)

 

3.Relational Operators cannot be applied on Objects types.

example:-

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com