Assignment operator part 2

Assignment operator part 2

                                                    Operator & Assignments

1-:String Concatination Operator(+)

2-:Relational Operators(>,>=,<,<=)

3-:Equality Operators(==,!=)

                                                    String Concatenation operator 

The only overloaded operator in java is (+) operator some time it accesses arithmetic addition operator some time it access String concatenation operator.

if at least one argument is String Type then + operator access concatenation operator and if both arguments are Number type then + operator access arithmetic addition operator.

Example-:

/*

    String a="Sum";

    int b=10,c=20,d=30;

    System.out.print(a+b+c+d);//Sum102030

    System.out.print(b+c+d+a);//60Sum

    System.out.print(b+c+a+d);//30Sum30

    System.out.print(b+a+c+d);//10Sum2030

*/

Example 2-:

consider the following declaration .

/*

    String a="Sum";

    int b=10,c=20,d=30;

    a=b+c+d;//compile time error-:incompatible types found int required java.lang.String

    a=a+b+c;//Sum1020

    b=a+c+d;/compile time error-:incompatible types found String required int

    b=b+c+d;//60

*/

                                                Releational Operators

less than (<)

less than equal(<=)

grater than(>)

grater than equal(>=)

case1-:we can apply relational operator for every primitive type except boolean.

/*

 System.out.print(10>7);//true

 System.out.print(10>'a');//false

 System.out.print('a'<976);//true

System.out.print('a'>'A');//true

System.out.print(true>false);//compile time error-:operator > cannot be applied to boolean,boolean

*/

case2-:we can't apply relational operator for Object types .

Example-:

/*

System.out.print("Sum123">"Sum");//compile time error-:Operator  > can't be applied java.lang.String ,java.lang.String

*/

case4-:nesting of relational operators is not allowed otherwise we will get compile time error

/*

   System.out.print(10<20<30);//compile time error-:operator can't be applied to boolean ,int

*/


                                                        Equality Operators

we can apply equality operators for every primitive type including the boolean type also.

Example-:

/*

System.out.print(10==20);//flalse

System.out.print('a'=='b');//false

System.out.print('a'==97.0);//true

System.out.print(false==false);//true

System.out.print(10==20);

*/

case1-:we can apply equality operator for object types also

for object references r1,r2

/*

if(r1==r2 )

return true 

*/

if and only if both reference to same object(reference Comparison  are address Comparison).



Example-:

/*

  Thread t1=new Thread();

  Thread t2=new Thread();

  Thread t3=t1;

   System.out.print(t1==t2);//false

   System.out.print(t1==t3);//true

*/




Example2-:

if we apply an equality operator for object type then complusory there should be relation b/w argument types(either child to parent or parent to child  or same type) otherwise we will get compile time error saying incomparable type java.lang.String and java.lang.Thread

/*

Thread t=new Thread();

Object o=new Object();

String s=new String("Java");

System.out.print(o==t);

System.out.print(o==s);

System.out.print(s==t)//compile time error-: incomparable type java.lang.String and java.lang.Thread

*/

Ouestion-: differention b/w ==operator and .equals() method?

ans-:in genral we can use (==)operator for refernce comprasion(address comprasion) and .equals() for containten comprasion.

Example-:

/*

String s=new String("Sum");

String str=new String("sub");

System.out.print(s==str);//false

System.out.print(s.equals(str));//true 

*/


case 2-:for any object reference are ,

  obj==null is always false but null==null is always true





Previous Post Next Post

Most Recent