Like many programming languages Java support many types of data. One important aspect in java is that is a Strongly Typed language. This means that when the code is compiled every expression is checked to ensure that all the data in it are compatible. If any error occouryou must correct the code before compiling again.
IntegerJava does not support unsigned integers so every one of the following integers types have both negative and positive values.
ByteThis is the smallest integer type in Java. It occupies 8-bit and has a range from -128 to 127. To create a byte variable you have to use the keyword byte
ShortNot so frequently used, shorts integer occupy 16-bid and has a range from -32,768 to 32,767. To declare a variable of this type you have to use the keyword short.
IntThis instead is the most used type. It occupies 32-bit and has range from -2,147,483,648 to 2,147,483,647.
LongThis type occupies 64-bit and has a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808. It is useful when the number you need may exeed the range of an int . To create a variable of this type, use the long keyword
Integer LiteralsAny integer values like 1, 2, 3, 4, etc is an integer literal.. These describe a base 10 integer. You can also use a base 8(octal) or base 16(Exadecimal). Octal ranges from 0 to 7 while exadecimal from 0 to 15 but letter are used from 10 to 15 so 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f. To rappresent an octal intger you need a leading 0 while for exadecimal a leading 0x or 0X. For long integer you have to append an L at the end. You can also use base 2 rappresentation with a 0b or 0B leading. Underscores can be used for make easier to read large integers. The cannot be used at the beginning or at the end.
Floatthis type can have a value from 1.4e-045 to 3.4e+038 and occupy 32-bit. This type is useful when you don’t need too much precision
Doublethis type can have a value from 4.9e-324 to 1.8e+308 and occupy 64-bit. It has an high accuracy, and on new processor is more fast than float types
LiteralsThe standard notetion for floating-point numbers is like 0.123 but you can use also the scientific notation that use the standard plus a suffix rappresenting a power of 10 like this 0.123E45. Default floating-point literals use double precision. to specify a float you must append an f or F. Like the integers literals you can use underscore to make it easier to read large numbers.
CharOther programming languages use 8-bit to rappresent a char variable. Java instead, use 16-bit for it, because it use the Unicode system that let your code to bo more international. The ASCII characters range from 0 to 127, the ISO-Latin-1 from 0 to 255. The all UNICODE ranges from 0 to 65,536.
BooleanThe boolean type is used for logical values. It can have only two possible values: true or false.
Why using int even for small numbers instead of short or Bytes?Like this article says.The JVM works with 32 bit architecture that is right an integer. Most of the times the operation will be made with int, so a byte will be casted to an int etc… So maybe using byte would be useful if you think to arrays. An array of 1000 elements of byte type will take exactly 1000 byte of memory while one of 100 int will take 4000 byte.