Java - Arithmetic Operators / Chapter -2 /Operator & Assignments

javabychetan.blogspot.com

1.If we are applying any arithmetic operators between two variables a and b ,then the result type always will be max(int ,type of a,type of b).In java, every arithmetic operator returns minimum type is the int type.let's understand more about this by the following table.


javabychetan.blogspot.com
     example 1:-
    System.out.println('a'+'b');

    output is =195                          hint :-(a=>97 ,b=>98 unicode values)

     example 2:-
    System.out.println('a'+0.16)
    output is => 97+0.16  => 97.16


2.Infinite Role:

  • In integral data types(byte,short,int,long) doesn't have any representation for infinity.if we are applying then we won't get any compile time errors but while running the program  we will get RuntimeException saying"ArithmeticExceptionDevide by Zero".
          example :-         System.out.println(20/0);
                                   RuntimeException  :  ArithmeticExceptionDevide by Zero"
  •  But in floating point, arithmetic data types (float & double ) infinity concept is there to represent infinity.In floating point data types result is infinity then also we won't get any RuntimeException.         It contains two constant.
  1. POSITIVE_INFINITY
  2. NEGATIVE_INFINITY
          example:-     
                           System.out.println(20/0.0)       o/p     POSITIVE_INFINITY
                           System.out.println(-20/0.0)       o/p     NEGATIVE_INFINITY

 3.UNDEFINED RESULT

  • In integral arithmetic (byte,short, int,long) there is no way to represent undefined results.That's why if anywhere undefined result situation occurs that we  won't get any compile time errors but while running the program  we will get RuntimeException saying"ArithmeticExceptionDevide by Zero".
           example:-                    System.out.println(0/0);                                             RuntimeException  :  ArithmeticExceptionDevide by Zero"
  • But in floating point data types (float & double) there is a way to represent undefined results .Both Float & Double classes contain a NaN constant to represent undefined results.We won't get any RuntimeException in floating point data types. 
          example:-                  
                                System.out.println(0.0/0);        output   NaN
                                System.out.println(-0.0/0);       output   NaN


 Points To Remember:-

  1. ArithmeticException is a RuntimeException but not Compile Time Error.

  2. ArithmeticException is possible in only integral types (byte,short,int,long).

  3. ArithmeticException is caused by only two operators which are "/" & "%" only.

 

Back:  (Chapter 1 / Operators)Increment and Decrement Operators
Next:  Chapter -3 /Operator & Assignments /String Concatenation Operators




No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com