Skip to main content

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





Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...