Data

Data-type Storage-size Limits
Byte 8 bits -127 <= x <= 127
Short 16 bits -32768 <= x <= 32767
Int 32 bits -2147483648 <= x <= 2147483647
Long 64 bits -9223372036854775808 <= x <= 9223372036854775808
float 32 bits 7 digits precision
double 64 bits 15 digits precision
char 16 bits 65536 Unicode characters
boolean 1 bit false (default) or true

Strings belong to the class string, it is not a data-type

Declare like:

int i = 33;
float f = 4,4545f;
double d = 643,435345123d;
char c = “A”;
boolean OK = true;

Variable-names must start with a letter and can contain digits, _ and $ characters but no spaces.

Conversion to another data-type is not always automatic
int number1 = 7;
int number2 = 2;
float result = number1 / number2; //result is an integer (3)
float result = number1 / (float) number2; // number2 is forced to float, the result will be float too.

boolean conversions:

b is the result of the comparison between i and 0

int i = 1;
boolean b = i ! = 0; // b will be true
int i = 0;
boolean b = i ! = 0; // b will be false

int is 1 if b is true, 0 if b is false

boolean b = true;
int i = (b ? 1 : 0); // int will be 1
boolean b = false;
int i = (b ? 1 : 0); //  int will be  0

Operators are like in other languages:

Operator Action
* Multiply
/ Divide
% Modus (remainder of division)
+ add
subtract
( ) Force calculation order
>= Bigger than or equal
<= Smaller than or equal
== Equal to
!= Not equal to
expr1 && expr2 True if both expressions are true, expr2 is only calculated if needed (expr1 is true)
expr1 & expr2 True if both expressions are true  (AND)
expr1 || expr2 True if expr1 or expr2 is true, expr2 is only calculated if  needed (expr1 is false)
expr1 | expr2 True if expr1 or expr2 is true (OR)
expr1 ^ expr2 True if only one of the expressions is true (eXclusive OR)
! expr True if the expression is false
~ expr Invert bits (0101 => 1010)
expr1 >> int Bitwise shift right expr1 int positions left bit is filled with sign
expr1 >>> int Bitwise shift right expr1 int positions left bit is filled with 0
expr1 << int Bitwise shift left expr1 int positions left
= Assign
*= Multiply and assign
int a = 2;
int a *=3; // a will be 6
+=, -=, /= %=, <<=, >>=, >>>=, &=, |=, ^= Like *= for resp. add, subtract, divide, modulus and the other operators.
++ Add one
int b = a++; \ Assign to b then add 1
int b = ++a; \ Add 1, then assign to b.
Subtract one (like ++)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.