-->

Wednesday, February 26, 2014

How to Use Operators with Strings in Java Programming

Operator + with strings

You have used the operator + with numbers. When you use + with numbers, the result is also a number. However, if you use operator + with strings, it concatenates them e.g.

5 + 6 results in to 11.
“5” + “6” results in to “56”.
“17” + “A, V. Vihar” result in to “17 A, V. Vihar”
“abc” + “123” results in to “abc 123”
“” + 5 + “xyz” results in to “5xyz”
“” + 5 results in to “5”

(In above two expressions Java would internally convert 5 in to “5” first and then concatenate with “xyz” and “” respectively.)

Increment/Decrement Operators (++, --)

Java includes two useful operators not generally found in other computer languages (expect C and C++). These are the increment and decrement operators, ++ and --. The operators ++ adds 1 to its operand, and – subtracts one.
In other words,
a = a + 1;
is the same as
++a ; or a++;
And
a = a – 1
is the same as
--a ; or a --;

However, both the increment and decrements come in to two varieties: they may either precede of=r fallow the operand. The prefix version comes before the operand (as in ++ a or -- a) and the post-fix version comes after the operand (as in a++ or a--). The two version have the same effect upon the operand, but they differ when they take place in an expression.

Wednesday, February 19, 2014

Types and Examples of Binary Operators in Java Programming part-2

Operators that act upon two operands are referred to as Binary Operators. The operands of a binary operator are distinguished as the left or right operand. Together, the operator and its operands constitute an expression.

Addition operator (+). The arithmetic binary operator ads the values of its operands and the result is the sum of the values of its operands and the result is the sum of the values of its two operands. For example,
4 + 20 result in 24.
A + 5 (where a = 2) result in 7.
A + b (where a = 4, b = 6) result in 10.
Its operands may be of integer (byte, short, char, int, long) or float (float, double) types.

Subtraction operator (-). The – operator subtracts the second operand from the first. For example,
14 – 3 evaluates to 11
A – b (where a = 7,  b = 5) evaluates to 2.
The operands may be of integer or float types.

Multiplication operator (*). The * operator multiplies the values of its operands. For example,
3 * 4 evaluates to 12.
b * 4 (where b = 6) evaluates to 24.
a * c (where a = 3, c = 5) evaluates to 15.
The operands may be of integer or float types.

Division operator (/). The / operator divides its first operand by the second. For example,
100 / 5 evaluates to 20.
a / 2 (a = 16) evaluates to 8.
a / b (a =15.9, b = 3) evaluates to 5.3.
The operands may be of integer or float types.

Modulus operator (%). The % operator finds the modulus of its first operand relative to the second. That is, it produces the remainder of dividing the first by the second operand. For example,

19 % 6 evaluates to 1, since 6 goes into 19 three times with a reminder 1. Similarly 7.6 % 2.9 results into 1.8 and – 5% -2 result into -1.

Thursday, February 13, 2014

Types and Examples of Operators in Java Programming part-1

In Java programming, the operations (specific tasks) are represented by operators and the objects of the operations (s) are referred to as operands.
Java’s rich set of operators comprise of arithmetic, relational, logical and certain other type of operators. Let us discuss these operators in detail.

Arithmetic Operators

To do arithmetic, Java use operators. It provides operators for five basic arithmetic calculations: addition, subtraction, multiplication, division and remainder which are +, -, *, / and % respectively. Each of these operators is a binary operator i.e., it requires two values (operands) to calculate a final answer. A part from these binary operators, Java provides two unary arithmetic operators (that require one operand) also which are unary +, and unary -.

Unary Operators

Operators that act on one operand are referred to us as Unary Operators.

Unary +. The operators unary ‘+’ precedes an operand. The operand (the value on which the operator operates) of the unary + operator must have arithmetic type and the result is the value of the argument.
For example

if a = 5 then +a means 5.
if a = o then +a means 0.
If a = -4 then +a means -4.

Unary-. The operator unary – precedes an operand. The operand of the unary – operator must have arithmetic type and the result is the negation of its operand’s value.
For example

If a = 5 then –a means -5
if a = o then +a means 0. (There is no quantity known as -0)
If a = -4 then +a means -4.

This operators reverse the sing of the operand’s value.

Binary Operators
© Copyright 2013 Computer Programming | All Right Reserved