Arithmetic Operators
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.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.
- POSITIVE_INFINITY
- 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:-
ArithmeticException is a RuntimeException but not Compile Time Error.
ArithmeticException is possible in only integral types (byte, short, int, long).
ArithmeticException is caused by only two operators which are "/" & "%" only.
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