Operators
Operators are the symbols defining a certain operation to be performed.

Assignment Operators
An assignment operator assigns a value to a certain variable after evaluating the expression to be assigned.

The assignment operator, "=", sets the variable on its left equal to the value on its right. This code creates the variable a and sets it equal to 5

int a;
a = 5;

Fairly simple, right? Here is where it can get a slightly more tricky.

int a;
int b;
b = 6;
a = b; //a equals 6

At first glance, you might think that the last statement is setting a equal to the letter "b", but instead it is setting a equal to the value of b, which is 6. Essentially, a holds the value of 6. You can also set a variable and declare it on the same line:

int a = 6;

However, you cant say char myChar = y; to set a character to "y" That would set the character variable myChar to the value stored in the character variable y. To set myChar equal to the character y, use a character literal a technical term for "a character in single-quotes": char myChar = "y";. Note that a char primitive can only hold a single character. char myChar = "Yo"; is illegal.

Finally, whats so special about floats and doubles? Doubles and floats can have decimals: float myFloat = 5.1;. (The term "float" is short for "floating-point number," referring to the decimal point.)

Now, why would you want to set a variable? You would need to set a variable for further use, i.e., to access at a later time.

Addition Operator +
The addition operator returns the sum of the the values to the left, and the value to the right of it.

If you want to evaluate the expression 23 plus 75, you would type:

23+75

The addition operator also works with variables:

myInt+yourInt

Note, this operator is not to be confused with the concatenation operator, which we will discuss later.

Subtraction -, multiplication *, division /, and modulus %operators
Just like the addition operator, the subtraction, multiplication, division, and modulus operators are used as follows:

int a = 9;
 
a-1; // evaluates to 8
a/3; // evaluates to 3
a*2; // evaluates to 18
a%4; // evaluates to 1

Subtraction, multiplication, and division, you have seen before, but the modulus operator is much less commonly used. Actually, it is commonly used, but is known under a different name, the remainder. Nine divided by four gives a remainder of one, so therefore 9%4 (pronounced "9 mod 4") evaluates to 1.

When more than one of the operators are used in the same statement, for example:

int a, b, c, d;
a = a-b/c*d;

the Java language would use the order-of-operations in this case.

Plus-equals
The plus-equals operator ("+=") adds the value on the right, to the variable on the left, and then assigns that value back into the variable on the left.

Example:

int a = 6; // assigns the value 6 to variable a
a += 5; // adds 5 to a, and assigns that value back into a, now a is 11

Minus-equals, multiply-equals, divide-equals, and mod-equals
Minus-equals, multiply-equals, divide-equals, and mod-equals works in the same way as plus-equals, except instead of addition, they do subtraction, multiplication, division, and modulus, respectively.

Concatenation operator
The concatenation operator (+) looks exactly like the addition operator, however it performs a different operation. The concatenation operator concatenates or "puts together," for lack of a better term, two Strings.

String myString = "Hello, ";
String yourString = "world.";
String ourString = myString + yourString; // evaluates to "Hello, world."

 




Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)