Primitive Types, Operators and Flow Control Statements

Java language provides 8 primitive types to represent simple numeric, character and boolean values.

Declare and Initialize Primitive Variables:

Primitive declaration and initialization rules:
<type> <variable name> = <value>;
-
Numeric values can be expressed as binary, octal, decimal and hex.
- Smaller types are automatically casted (promoted) to bigger types.
byte -> short -> char -> int ->long -> float -> double

int a= 0b101010; // binary         float e = 1.99E2F;
short b = 052; // octal double f = 1.99;
byte c = 42; // decimal long g = 5, h = c;
long d = 0x2A // hex float i = g;
char j = 'A';
char k = '\u0041', l = '\101';
int s; s = 77;

Restrictions on Primitive Declarations and Initialization:

- Variables must be initialized before use.
- A bigger type value cannot be assigned to a smaller type without explicit type casting. Type can be explicitly casted using the following syntax.
(new type)<variable or expression>
- Character values must be enclosed in single quotation marks.
- Boolean values can be expressed only as true or false.

byte a;                             boolean h = "true";
byte b = a; boolean i = 'false';
byte c = 128; boolean j = 0;
int d = 42L; boolean k = False;
float e = 1.2;
char f = "a";
char g = 'AB'; // these are wrong examples

Java Operators:

byte a = 127, b = 5;
byte c = a + b; //compilation fails
int d = a + b; //d is 132
byte e = (byte)(a + b); //e is -124 (type overflow 127 max)
int f = a / b; //f is 25
float g = a / b; //g is 25.0F implicitly casted to float
float h = (float)(a/b); //h is 25.0F explicitly casted to float
float i = (float)a/b; //i is 25.4F when either a or b is float
float j = a/(float)b; //j is 25.4F float the a/b becomes float
b = (byte)(b+1); //explicit casting is required,b+1 is an int
b++; //no casting is required for ++ and --
char x = 'x'; //arithmetic operations work with character char y = ++x; //codes

Binary Number Representation:

All Java numeric primitives are signed.
- For example a byte zero value is represented as the 00000000 sequence of bits.
- Changing a sign(negative or positive) is done by inverting all the bits and then adding one to the result.
- For example a byte value of one is represented as 00000001 and minus one is 11111111.
- Bitwise Compliment operator inverts all bits of a number. The result of the bitwise compliment operator ~a would be its “mirrored” binary value: -(a+1)

int a = 2;
int b = ~a; //b is -3

Equality, Relational and Conditional Operators:

Returns true or false values as a result.
==, !=, >, >=, <, <=, !, &&, ||, &, |, ^
Note: Compilation fails because a block containing more than one statement must be enclosed with { }.

int a = 2, b = 3;
if(a > b)
a--; //it fails
else
a++;

Flow Control Using switch Construct:

Switch expression must be of one of the following types: byte, short, int, char, String, enum.
- Execution flow continuous until it reach the end of switch or encounters an optional break statement.

char status = 'N';
double price = 10;
switch (status) {
case 'S':
price += 1;
case 'N':
price += 2; //price is 12
case 'D':
price -= 4; //price is 8
break; //flow is completed , break statement
case 'E':
price = 0;
break;
default:
price = 3;
}

See the next episode:
Text, Date, Time and Numeric Objects

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store