Wednesday 12 September 2018

Operators In JAVA ( Part 2 )




In this post, we discuss some programs and try to understand some core concepts(Remarkable points). One more thing I would like to share with you...

I am not able to write all the programs using all operators .. I can write some important one (That follows some different rules).

But please you guys, practice as much as you can, this is the only way to learn to programming.


 Assignment Operator 

-- Assignment has LOWEST Priority.
-- it is denoted by " = " (equal to sign)
-- always value assign from RIGHT to LEFT


Now, There are three cases may arise.


case 1 - Destination and Source will be EQUAL.

If this is the scenario, then there is no problem.

Let's Take an EXAMPLE -

suppose you have a glass of water ... and you want to transfer it in the different glass with the same type, then you can easily do it without any problem.

case 2 - Destination is GREATER THAN Source.

If this is the scenario, then there is no problem.
It is also called Widening Conversion. 


Let's Take an EXAMPLE -

suppose you have a small glass of water ... and you want to transfer it in a large glass, then you can easily do it without any problem.




case 3 - Destination is SMALLER THAN Source.

If this is the scenario, then you are in big problem.
It is also called Narrow Conversion.
The main problem is that when you are doing this your data may be loose.
And Compiler will not allow Narrow Conversion Directly.

Let's Take an EXAMPLE -

suppose you have a large glass of water ... and you want to transfer it in a small glass, then if water is full in the large glass then some water will be lost during transfer, so you can't do it easily. It is a problem.


To Solve this problem we use TYPE CASTING.

Definition -  Forcing Compiler to allow Narrow Conversion.




Modulus Operator


-- This operator helps to give us the reminder.
-- % (per cent sign) is used for modulus operator. 

System.out.println( 5 % 2 );       // output = 1
System.out.println( -5 % 2 );      // output = 1
System.out.println( 5 % -2 );      // output = -1
System.out.println(-5 % -2);      // output = -1 

Note - In Modulus Operator, Reminder Sign is depends on DENOMINATOR. 



Shift Operator

  1. a>>>n
    • Shift bits of  "a" n times right filling with 0 from left.
  2. a>>n
    • Shift bits of "a" n times right filling with sign bit from left
  3. a<<n
    • Shift bits of "a" n times left filling with 0 from the right.




Now There are some points that you must know -


  1. When every you try to store negative number in your program, then those negative numbers store in 2's compliment.
  2. By default all the numerical value are of type int (means to say that all the calculation internally done in 32 bit memory)
  3. By default all the floating point are of type double (means to say that all the calculation internally done in 64 bit memory) 
  4. if both the operand are either int type or smaller then int (calculation done in 32 bit)
  5. if one or both operand bigger then int type then smaller type promoted to bigger type.
  6. any Integer arithmetic (except /0 or %0 ) always gives result within the range.
  7. Any Integer value  divided by zero gives -- ArithmeticException
  8. Floating point Numbers goes INFINITY.
    • System.out.println(5.0/0); // INFINITY
    • System.out.println(5/0.0); // INFINITY
    • System.out.println(5.0/0.0); // INFINITY
  9. Any Operation which is not possible -- for that java gives NaN (Not a Number) 
  10. we can not use/assign ++ or --  to any constant, it can be only for variable.




Question - Why float f = 10.5; gives error?
Answer - 
//float f = 10.5;  //ERROR
 // error because all the operation done in double (64 bit).So we have to typecast it.
// These are the 3 ways to do it.
float fl = 55.5f;
float f2 = 55.55F;
float  f3 = (float) 555.55;

Question - Full Form & Example of NaN?

Answer - NaN ==> Not a Number 
It comes when you are trying to do the operation at are not possible mathematically.
like finding the square root of a negative number.


        System.out.println(2.0 % 0);
        System.out.println(0.0 / 0);
        System.out.println(Math.sqrt(-1));


*Question - When we use "=" or "= ="?

Answer - " = " used for assignment, from right to left.
" ==  " used for comparison. [will descuss it later]

*Question - How can you say Compound Operators are implicit typecasting?



short s=5;
//short result =s+s;// ERROR Bcoz all the calculation done in 32 bit (int);
short result = (short)(s+s); // type casting required
System.out.println(result);

Or

instead off writing short result = (short)(s+s);
we can also write -- short result +=(s+s); // Called Short Hand Operator.








No comments:

Post a Comment