7.1 | Data Types
Each variable declaration in Java must define the data type of the variable. The data type defines what values a variable can store. The variable type can be as follows:
- One of the eight basic primitive data types
- An Array
- The name of a class
The eight primitive data types hold values for integers, floating-point numbers, characters and boolean values. They are called primitive because they are built into the system and are not actual objects. This makes them more efficient to use.
Data Type | Width (bits) | Minimum value/ Maximum value |
---|---|---|
boolean
|
NA
|
True, false
|
byte
|
8
|
-27 , -27-1
|
short
|
16
|
-215 , -215-1
|
char
|
16
|
0x0, 0xffff
|
int
|
32
|
-231 , -231-1
|
long
|
64
|
-263 , -263-1
|
Float
|
32
|
±1.40129846432481707e - 45f,
±3.402823476638528860e + 38f
|
double
|
64
|
±4.94065645841246544e - 324,
±1.7976931348623157e + 308
|
7.2 | Literals
A literal denotes a constant value, i.e., the value that a literal holds. A literal can represent boolean, numerical, character, string or null value
Literal
|
Literal Type
|
Example
|
---|---|---|
Integer
|
Decimal
Octal
Hexadecimal
|
8, 10L, -90
010, 012L, -0132
0 x 8, 0 x aL, -05 x a
|
Floating
|
Double
Float
|
0.41, 4100e-2 , 0.41e2
0.41F, 4100e-2F, 0.41e2F
|
Boolean
|
True, false
|
|
Character
|
'', '1', 'A', 'a'
|
|
Escape Sequences
|
\b, \t, \n, \f, \', \”, \\
|
|
String
|
“This is a String”
|
7.3 | Variable Declaration
A variable stores a value of specific type. Variable declaration is used to specify the type and name of the variable, The following code shows how to declare and initiate variables.
int i; // Declaring interger variable
i=1; // Initializing integer variable
int j=2; // Declaring and initializing integer variable
long k=2L; // Declaring and initializing integer variable
boolean status=false; // Declaring and initializing boolean variable
float f1 = i/3; // Declaring and initializing float variable
float f2 = (float) (i/3.0); // Declaring and initializing float variable
String str = "This is a string." // Declaring and initializing a String variable
7.4 | Expression & Operators
Expressions are statements in Java that return a value. Operators are special symbols that perform an operation, for example, add and subtract.
7.4.1 | Arithmetic Operators
List Java arithmetic operators
Operator
|
Description
|
Example
|
---|---|---|
+
|
Addition
|
5 + 2 = 7
|
-
|
Subtraction
|
5 – 2 = 3
|
*
|
Multiplication
|
5 * 2 = 10
|
/
|
Division
|
5/2 = 2
|
%
|
Modulus
|
5%2 = 1
|
7.4.2 | Comparison Operators
All Java comparators return a boolean value.
Operator | Description | Example |
---|---|---|
==
|
Equal
|
5 == 2 = false
|
!=
|
Not Equal
|
5 != 2 = true
|
<
|
Less Than
|
5 < 2 = false
|
>
|
Greater Than
|
5 > 2 = true
|
<=
|
Less Than Equal To
|
5 <= 2 = false
|
>=
|
Greater Than Equal To
|
5 >= 2 = true
|
7.4.3 | Logical Operators
Logical operators are used to implementing logical AND, OR, XOR and NOT condition.
Operator
|
Description
|
Example
|
---|---|---|
&&
|
AND
|
(5<2) && (2>10)
|
||
|
OR
|
(5<2) || (2>10)
|
^
|
XOR
|
(5<2) ^ (2>10)
|
!
|
NOT
|
!(5 < 2)
|
7.4.4 | Bitwise Operators
Bitwise operators perform operations on individual bits in integers.
Operator
|
Description
|
Example
|
---|---|---|
&
|
Bitwise AND
| |
|
|
Bitwise OR
| |
^
|
Bitwise XOR
| |
<<
|
Left shift
| |
>>
|
Right shift
|
>>>
|
Zero fill right shift
|
-
|
Bitwise complement
|
<<=
|
Left shift assignment
|
>>=
|
Right shift assignment
|
x&=y
|
AND assignment
|
Equivalent to x=x&y
|
x|=y
|
OR assignment
|
Equivalent to x=x|y
|
x^=y
|
NOT assignment
|
Equivalent to x=x^y
|
No comments:
Post a Comment