Featured Posts
Recent Articles

Sample code that use Arithmetic Operators in Java

The Java programming language provides operators that perform addition, subtraction, multiplication, and division. There's a good chance you'll recognize them by their counterparts in basic mathematics. The only symbol that might look new to you is "%", which divides one operand by another and returns the remainder as its result.

+ additive operator (also used for String concatenation)
- subtraction operator
* multiplication operator
/ division operator
% remainder operator


 The following program, ArithmeticDemo, tests the arithmetic operators.

class ArithmeticDemo {

public static void main (String[] args){

int result = 1 + 2; // result is now 3
System.out.println(result);

result = result - 1; // result is now 2
System.out.println(result);

result = result * 2; // result is now 4
System.out.println(result);

result = result / 2; // result is now 2
System.out.println(result);

result = result + 8; // result is now 10
result = result % 7; // result is now 3
System.out.println(result);

}
}

class ConcatDemo {
public static void main(String[] args){
String firstString = "This is";
String secondString = " a concatenated string.";
String thirdString = firstString+secondString;
System.out.println(thirdString);
}
}

Share and Enjoy:

0 comments for this post

Leave a reply

We will keep You Updated...
Sign up to receive breaking news
as well as receive other site updates!
Subscribe via RSS Feed subscribe to feeds
Recent Stories
Blog Archives
Recent Comments
Tag Cloud