Operators and Expressions
Programming is mostly about operations and evaluation of data. In Java,
multiple operators are available for to use. Some of them deal with manipulating
numbers, including basic Mathematical operations. Some deals with strings. And
some will provide you with program control flow.
On the other hand, take note of the term expressions. Expressions are
combinations of literals, value-giving keywords or methods, operators, and
variables that can be evaluated. For example:
int a = 1 + 1;
In the example, two operators were used. First is the assignment operator (=).
The assignment operator allows associating a variable to a literal. Second is the
addition operator (+). The addition operator adds the two numbers beside it. The
expression in the statement is 1 + 1, which can be evaluated. The assignment
operator will take the evaluated value of the expression to the variable.
Just like in Mathematics, operators in programming languages and Java have an
order of precedence — an order which prioritizes the operators that should be
evaluated first. Below is a table of most of the available operators in Java and
their order of precedence.
Order
of
Precedence
Operator Symbol
Description
/
Purpose
Associativity
1
( )
Invokes methods,
group
variables,
and literals
Left to Right
[ ]
Initialize or access
an array
.
Selects
class
member
or
methods
2
++ --
Increment
and
decrement
value
of the literal or
variable on its left
by 1 if placed to
the right (postfix)
3
++ --
Increment
and
decrement
value
of the literal or
variable
on
its
Right to Left
right
by
1
if
placed to the left
(prefix)
+ -
Unary minus and
plus: indicates the
sign
of
a
numerical variable
or literal if placed
before the variable
or literal (prefix)
!
Logical
NOT
Negates
truth
values. Returns 1
if value is 0. And
returns 0 if value
is not 0.
~
Bitwise
NOT
negates digits in a
binary literal or
variable
4
* / %
Multiplication,
division, modulo
(division
that
returns
the
remainder instead
of the quotient)
Left to Right
5
+ -
Addition,
subtraction
if
placed
in
the
middle
of
two
variables
or
literals
+
Sting
Concatenation
if
placed
in
the
middle
of
two
variables or string
literals
(join
strings together)
6
>> << >>>
Bitwise
signed
right
shift,
left
shift,
unsigned
right shift
7
> >=
Less than, less
than or equal to
(usually used for
relational
comparison
in
conditional
statements)
< <=
Greater
than,
Greater than or
equal to (usually
used for relational
comparison
in
conditional
statements)
8
== !=
Is equal to, is not
equal to (usually
used for relational
comparison
in
conditional
statements)
9
&
Logical
and
Bitwise AND
10
^
Logical
and
Bitwise XOR
11
|
Logical
and
Bitwise OR
12
&&
Logical
conditional AND
13
Logical
||
conditional OR
14
c ? t : f
Ternary
conditional ?
Right to Left
15
=
Assignment
operator
+= -=
Add and assign,
subtract
and
assign
*= /= %=
Multiply
and
assign, divide and
assign,
modulo
and assign
<<= >>= >>>=
Bitwise left shift
and assign, signed
right
shift
and
assign,
unsigned
right
shift
and
assign
&= ^= |=
AND and assign,
XOR and assign,
OR and assign
Note that the lower the order of precedence, the higher the priority of the
operator. Some operators were not included and some are not explained fully
since you will not be needing them anytime soon. The operators that you will be
mostly using in your apps are the mathematical, conditional, assignment, and
relational/conditional operators. In some cases, you might need to use logical
operators.
Statements, Physical Line, and Logical Line
A statement is composed of all or any of those parts combined. In literature, a
statement is a sentence — an imperative one. Take note that it is essential that
you separate the elements in your statements with spaces. It is not imperative,
but usage of space in coding improves the readability of your source code.
Every statement in your source code will tell your computer or Android device
to do something. A simple declaration and assignment of a variable is a
statement. Statements are also called logical lines.
In Java, a statement will be considered as such if it ends with the semicolon (;)
separator. Whenever you finish writing a logical line, never forget to include a
semicolon. If not, you will receive a syntax error.
On the other hand, a physical line is a line of code in programming. A physical
line can or cannot be a statement. As you might have noticed in source code
editors, every line in the editor is numbered. A line of text or code is a physical
line.
Below is an example of the initial Java code of a new app made in Android
Studio:
Do'stlaringiz bilan baham: |