Type Here to Get Search Results !

Arithmetic Operators in java

0

Arithmetic Operators

Java provides eight Arithmetic operators. They are for addition, subtraction, multiplication, division, modulo(or remainder), increment (or add 1), decrement (or subtract 1), and negation. An example program is shown below that demonstrates the different arithmetic operators in java.

The binary operator + is overloaded in the sense that the operation performed is determined by the type ofthe operands. When one of the operands is a String object, the other operand is implicitly converted to itsstring representation and string concatenation is performed.

String message = 100 + “Messages”; //”100 Messages”

Ex:
public class ArithmeticOperatorsDemo {
public ArithmeticOperatorsDemo() {
int x, y = 10, z = 5;
x = y + z;
System.out.println("+ operator resulted in " + x);
x = y - z;
System.out.println("- operator resulted in " + x);
x = y * z;
System.out.println("* operator resulted in " + x);
x = y / z;
System.out.println("/ operator resulted in " + x);
x = y % z;
System.out.println("% operator resulted in " + x);
x = y++;
System.out.println("Postfix ++ operator resulted in " + x);
x = ++z;
System.out.println("Prefix ++ operator resulted in " + x);
x = -y;
System.out.println("Unary operator resulted in " + x);
// Some examples of special Cases
int tooBig = Integer.MAX_VALUE + 1; // -2147483648 which is
// Integer.MIN_VALUE.
int tooSmall = Integer.MIN_VALUE - 1; // 2147483647 which is
// Integer.MAX_VALUE.
System.out.println("tooBig becomes " + tooBig);
System.out.println("tooSmall becomes " + tooSmall);
System.out.println(4.0 / 0.0); // Prints: Infinity
System.out.println(-4.0 / 0.0); // Prints: -Infinity
System.out.println(0.0 / 0.0); // Prints: NaN
double d1 = 12 / 8; // result: 1 by integer division. d1 gets the value
// 1.0.
double d2 = 12.0F / 8; // result: 1.5
System.out.println("d1 is " + d1);
System.out.println("d2 iss " + d2);
}
public static void main(String args[]) {
new ArithmeticOperatorsDemo();
}
}



Post a Comment

0 Comments

Recent-post