Showing posts with label Operators. Show all posts
Showing posts with label Operators. Show all posts

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 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:-

Java String Concatenation Operators / Chapter -3 /Operator & Assignments



java string


 Java String Concatenation Operator




  1. This (+) is the only overloaded operator in java.Sometimes it acts as "Arithmetic operators" and Sometimes it acts as "String Concatenation Operators".

  2. If both arguments are number type then it acts as "Arithmetic Addition Operator".

  3. If at least one argument is String type then (+)operators act as "String Concatenation Operators".



example 1:-
String a="javabychetan";
int b=10,c=20,d=30;
System.out.println(a+b+c+d);        o/p is              javabychetan102030
System.out.println(b+c+d+a);       o/p is               60javabychetan
System.out.println(b+c+a+d);       o/p is               30javabychetan30
System.out.println(b+a+c+d);       o/p is               10javabychetan2030

  •   explanation of the first System.out.println(a+b+c+d);






java string

javabychetan.com



Java - String Concatenation Operators / Chapter -3 /Operator & Assignments

javabychetan.blogspot.com

  1. This (+) is the only overloaded operator in java.Sometimes it acts as "Arithmetic operators" and Sometimes it acts as "String Concatenation Operators".
  2. If both arguments are number  type then it acts as "Arithmetic Addition Operator".
  3. If at least one argument is String type then (+)operators act as "String Concatenation Operators".
example 1:-
                String a="javabychetan";
                int b=10,c=20,d=30;
               System.out.println(a+b+c+d);        o/p is              javabychetan102030
               System.out.println(b+c+d+a);       o/p is               60javabychetan
               System.out.println(b+c+a+d);       o/p is               30javabychetan30
               System.out.println(b+a+c+d);       o/p is               10javabychetan2030
       
  •                explanation of the first System.out.println(a+b+c+d);
           
javabychetan.blogspot.com

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com