Total members 11895 |It is currently Sat Dec 28, 2024 9:21 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Arithmetic Operators in Java :
Arithmetic operators are most well known programming basics. It is simple consists of summation, subtract, multiplication, division and mod. In java these operators are applied on all primitives of types short, integer, long, float, and double.

Plus- Operator (+)


Plus operator is used to sum variables as follows:
Example:
java code
int x=5;
int y=43;
int z=x+y;

You sum variables from different datatypes but to be attention to the lose of precision and casting:
java code
float x=54.3f;
int y=4;
long z=53l;
double sum=x+y+z;

Or using casting :
java code
float x=54.3f;
int y=4;
long z=53l;
int sum=(int) (x+y+z);

You can use the plus operator in suffix or prefix manner but here you work on one variable only, as follows:

Suffix , add the double plus the operators before the variable name:
Code:
int y=++x;

This means that the variable X one is increased by one before assigning its value to variable y.
Prefix, add the double plus operators after the variable name:
java code
int y=x++;

This means that the variable X one is increased by one after assigning its value to variable y.
You can use the prefix and suffix at the same time as:


Mines - Operator (-)


Used to subtract two variables
Example :
java code
int x=5;
int y=4;
int z=x-y


It also has suffix and prefix same as Plus operator

Example :
Code:
--x or x--

but it decreases the value of x by 1 .

Star- Operator (*)


Binary operator to multiply two variables or values :
Example :
java code
int x=5;
int y=4;
int z=x*y;


multiply the value of x by y .

Divide- Operator (/)


Example :
Code:
x/y


divide the value x by y .

Mod- Operator (%)


Mod is used to get the reminder of division between two numbers.
Example :
Code:
x%y

For Example:
java code
int x=13;
int z=54;
int y;
y=z%x;
System.out.println(y);

The output is :
Code:
2

Get the remainder of dividing x ,y



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Arithmetic Operators in java
 Results of Java expressions for arithmetic operations     -  
 Arithmetic data comparison and decompression java code     -  
 Comparison Operators in java     -  
 Bitwise operators in java     -  
 Program for "Selection of arithmetic operations" in Java     -  
 Arithmetic Promotion     -  
 Arithmetic data comparison in C#     -  
 PHP operators     -  
 Using Assignment Operators     -  
 Bitwise operators     -  



Topic Tags

Java Basics






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com