Ads 468x60px

Monday, July 25, 2011

Operators of JAVA

Operators of JAVA..

 Operators are used to compare values. Java has strict definitions of operators. It doesn't allow for overloading, which is a C developer's common practice of changing the behavior of operators.
Java provides two types of operators: binary and unary. Binary operators are used to compare two values. Unary operators use a single value, for example:
a >= b
The first example uses a binary operator, >=, which compares variables a and b. The second is a unary operator, ++, which increments the value of a by one.
All of Java's binary and unary operators can be found in Table 1.3. They are organized according to the precedence with which they are performed.

Table 1.3. Binary and unary operators.
., (), [] Precedence overriding decimal, parentheses, brackets
!, ~, ++, -- Boolean negation, bitwise complement, increment, decrement
*, /, % Multiplication, division, modulus
+, - Addition, subtraction or unary negation
<<, >>, >>> Left shift, right shift, zero-fill right shift
<, <=, >, >= Less than, less than or equal to, greater than, greater than or equal to
==, != Equals, is not equal to
& Bitwise or Boolean AND
^ Bitwise or Boolean XOR
| Bitwise or Boolean OR
&& Evaluation AND, Logical AND
|| Evaluation OR, Logical OR
?: If…then…else
=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>= Assignment operators
, Comma

Declaring Variables

There are four types of statements to use for variables: declarations, assignments, initializers, and casts.
You must always declare variables before you can use them in your Java program. Variable declarations assign data types to variables. A declaration statement in Java consists of a data type followed by an identifier. Any of the data types listed in the previous table can be used to declare variables, for example:
Boolean IsReady;
float miles;
int x, y, z;
short pages;
Assignments are statements that assign values to variables. These, like declarations, are required before variables can be used. They are called by setting an identifier equal to a value. This value, of course, must be compatible with the data type assigned to the variable identifier. Initializers are assignment statements that are combined with the declaration statement, for example:
Boolean IsReady = false
float miles = 3.62
short pages = 240
If you want certain variable values to remain constant in your code, you can use the final keyword, which ensures that the variable cannot be changed by the code. Its form is this:
final int pages = 500
Casts are statements you use if you need to place a value of one type into a variable of another type. In C++, automatic coercion allows you to do this without declaring that you were aware of this change. In Java, you must explicitly call such an instruction with a cast statement. Cast statements are generally called as follows:
datatype identifier = (datatype) identifier
Java allows casts only between compatible data types.
In this section, you have learned about the data and operators that are used in expressions that are parts of statements. You now understand that statements are organized with blocks and annotated with comments. You have also examined some basic statements that deal with variable declarations. These are the fundamental elements of Java coding. 


Post a Comment