Você está na página 1de 3

ELEMENTARY PROGRAMMING

Brief Outline:
 Variables
 Primitive Data Types
 Operators and their precedence

Variables
Variables are nothing but reserved memory locations to store values, which means when you
declare/create a variable you reserve some space in memory.

All variables belong to different data types. by assigning different data types to variables, you can
store integers, decimals, or characters in these variables.

There are two data types in Java :


1. Primitive Data Types, about which we will learn next.
2. Object Data Types, about which we will learn in Object Oriented Programming section.

Primitive Data Types


Type Contains Defaults Size Range Example
boolean true or false false 1bit NA boolean yes = true;
char Unicode character \u0000 16bits \u0000 to \uFFFF char capA = 'A';
byte Signed integer 0 8bits -128 to 127 byte a = 100;
short Signed integer 0 16bits -32768 to 32767 short s = 10000;
int Signed integer 0 32bits -2147483648 to 2147483647 int i = 100000;
long Signed integer 0 64bits -9223372036854775808 to long l = 100000000;
9223372036854775807

float IEEE 754 floating point 0.0 32bits ±1.4E-45 to ±3.4028235E+38 float f = 23.5;

double IEEE 754 floating point 0.0 64bits ±4.9E-324 to ±1.7976931348623157E+308 double d = 15556.98;

To declare any of these primitive data type to a variable, you just need to use the keyword for the
data type before the variable identifier as shown in the examples.

Operators and their Precedence


Java provides a rich set of operators to manipulate variables. We can divide all the Java operators
into the following groups:
 Arithmetic Operators - are used in mathematical expressions in the same way that they are
used in algebra. Assume integer variable A holds 10 and variable B holds 20, then:
◦ + (Addition) Adds values on either side of the operator. Example: A + B will give 30.
◦ - (Subtraction) Subtracts right hand operand from left hand operand. Example: A – B
will give -10.
◦ * (Multiplication) Multiplies values on either side of the operator. Example: A * B will
give 200.
◦ / (Division) Divides left hand operand by right hand operand. Example: B / A will give
2.
◦ % (Modulus) Divides left hand operand by right hand operand and returns the
remainder. Example: B % A will give 0.
◦ ++ (Increment) Increases the value of operand by 1. Example: A++ will give 11.
◦ - - (Decrement) Decreases the value of operand by 1. Example: B-- will give 19.
 Relational Operators - are used in comparing variables, statements and values. Assume
integer variable A holds 10 and variable B holds 20, then:
◦ == (equal to) Checks if the values of two operands are equal or not, if yes then
condition becomes true. Example: (A == B) is false.
◦ != (not equal to) Checks if the values of two operands are equal or not, if values are not
equal then condition becomes true. Example: (A != B) is true.
◦ > (greater than) Checks if the value of left operand is greater than the value of right
operand, if yes then condition becomes true. Example: (B > A) is true.
◦ < (less than) Checks if the value of left operand is less than the value of right operand, if
yes then condition becomes true. Example: (A < B) is true.
◦ >= (greater than or equal to) Checks if the value of left operand is greater than or
equal to the value of right operand, if yes then condition becomes true. Example: (A >=
B) is not true.
◦ <= (less than or equal to) Checks if the value of left operand is less than or equal to the
value of right operand, if yes then condition becomes true. Example: (A <= B) is true.
 Bitwise Operators – can be applied to the integer types, long, int, short, char and byte.
Bitwise operator works on bits and performs bit-by-bit operation. They are not very
commonly used.
◦ & (bitwise AND)
◦ | (bitwise OR)
◦ ^ (bitwise XOR)
◦ ~ (bitwise compliment)
◦ << (left shift)
◦ >> (right shift)
 Logical Operators – They are used for logical operations. Assume Boolean variables A
holds true and variable B holds false, then
◦ && (logical and) - Called Logical AND operator. If both the operands are non-zero,
then the condition becomes true. Example: (A && B) is false.
◦ || (logical or) - Called Logical OR Operator. If any of the two operands are non-zero,
then the condition becomes true. Example: (A || B) is true.
◦ ! (logical not) - Called Logical NOT Operator. Use to reverses the logical state of its
operand. If a condition is true then Logical NOT operator will make false. Example: !(A
&& B) is true.
 Assignment Operators – These are used for assigning values to variables.
◦ = (simple assignment) - It assigns values from right side operands to left side operand.
Example: C = A + B will assign value of A + B into C.
◦ += (add and assign) - It adds right operand to the left operand and assign the result to
left operand. Example: C += A is equivalent to C = C + A.
◦ -= (subtract and assign) - It subtracts right operand from the left operand and assign the
result to left operand. Example: C -= A is equivalent to C = C – A.
◦ *= (multiply and assign) - It multiplies right operand with the left operand and assign
the result to left operand. Example: C *= A is equivalent to C = C * A.
◦ /= (divide and assign) - It divides left operand with the right operand and assign the
result to left operand. Example: C /= A is equivalent to C = C / A.
◦ %= (modulus and assign) - It takes modulus using two operands and assign the result
to left operand. Example: C %= A is equivalent to C = C % A.
 Miscellaneous Operators -
◦ Conditional Operators - Conditional operator is also known as the ternary operator.
It will be explained in detail in the Decision Making (Conditions and Selections)
module.
Precedence of Operators
Operator precedence determines the grouping of terms in an expression. This affects how an
expression is evaluated. The arithmetic operator precedence is as follows :

Precedence to higher to lower from right to left, i.e.

*/%+-

Você também pode gostar