In Java, as in other languages, we have many types of data that can store both fixed and floating numbers, characters, strings, and logical type. Java has strict type control, so to put it simply, each object must have a specific type.
Numeric primitives, we can distinguish 4 types, they only hold numeric data:
- byte – 1 byte – range from -128 to 127
- short – 2 bytes – range from -32 768 to 32,767
- int – 4 bytes – range from -2 147 483 648 to 2 147 483 647
- long – 8 bytes – range from -2 ^ 63 to (2 ^ 63) -1 (have suffix L, or l)
The most universal type is int, its range is big enough to perform all operations and at the same time it does not take up a lot of space (although less attention is paid to this aspect).
In addition, there are protected classes that are object-like equivalents of simple types. They provide methods that make sure that many routine activities are always at hand.
Java also does not have the Unsigned type (no characters), the consequence of which is that by crossing the scope of a given type, we will pass to the negative range.
The next two types represent floating-point numbers and, like integers, are differ in scope and use different amount of space.
- float – 4 bytes – max approx. 6-7 numbers after the decimal point (have suffix F, or f)
- double – 8 bytes – max 15 figures after the decimal point (they have suffix D, or d)
When it comes to fractions in your number (e.g. 12000,04) , be carefull not to use comma – use a dot “.”. It should also be remembered that floating-point numbers are not suitable for financial calculations in which accuracy counts. This is due to the fact that in the binary system, all numbers cannot be represented. Here BigDecimal class comes with the help. There is also the BigInteger class, which is the equivalent of integers and can represent a virtually unlimited number.
The next type is char, which is a character and is used to represent individual Unicode characters. They can be represented in single quotation marks, using hexadecimal code, or simply by entering the Unicode character number in the decimal system. In short, ‘a’ or \ u0000 is allowed.
There are also special characters that must be preceded by a backslash \:
- \t – tab
- \n – new line
- \r – carriage return
- \”- quotes
- \’- apostrophe
- \\ – backslash
The last primitive type is boolean. Its represented by two values:
- true – true
- false – false
It is usually used to falg something, or in a loop condition.