byte a = -10; byte b = 0; byte c = 100;
short a = -19000; short b = 0; short c = 21000;
int a = -7111555; int b = 0; int c = 5200300;
long a = -9800900200l; long b = 0; long c = 5300100700l;Note that you have to add l at the end of a very large number to tell Java it's a literal long value.
byte a = 8; int b = a;The above is acceptable beause a has 8 bits (byte) while b has 32 bits (int). The variable b here will also have the value of 8.
int a = 8; byte b = a;Becase this is trying to store a variable with more bits (32 bits) to a variable with less bits(8 bits). The resulting variable will not be able to handle it.
byte a = 10; short b = 5; int c = 200; int d = a + b + c;The above code is acceptable. The result of a + b + c is automatically of int type because operand c has the highest number of bits.
byte a = 10; short b = 5; int c = 200; short d = a + b + c;Because a + b + c is int (32 bits) which can't be assigned to a short (16 bits).
float a = -10.15f;Note that you have to add f at the end of a literal number to denote that it's a literal float.
double a = -200.25d;Note that you have to add d at the end of a literal number to denote that it's a literal double.
This is valid:
float a = 10; double b = a;
This is invalid:
double b = 20; float a = b;
Both float and double can store any of the numeric data types byte, short, int, and long. Example:
byte a = 10; short b = 2000; int c = 3000000; long d = 4000000000l;When computations contains either a float or a double operand, the result will automatically be either of the two - which ever has the highest number of bits.float f1 = a; float f2 = b; float f3 = c; float f4 = d;
double d1 = a; double d2 = b; double d3 = c; double d4 = d;
This is valid because a + b will become float
int a = 5; float b = 10; float c = a + b;
This is invalid because a + b is float, which is not allowed to be assigned to an int.
int a = 5; float b = 10; int c = a + b;
This is valid because a + b will become double.
float a = 5; double b = 10; double c = a + b;
This is invalid because a + b is double (64 bits), which can't be assigned to a float (32 bits).
float a = 5; double b = 10; float c = a + b;
boolean isFullyPaid = false; boolean hasDriverLicense = true; boolean isMarried = true;
char a = 'x'; char b = '1'; char c = '#'; char d = '£'; char e = '?';Note that literal char are enclosed with single quotes.
This statement is not valid because char can't have more than 1 character.
char a = 'ab';