Tutorial by Examples

A primitive data type such as int holds values directly into the variable that is using it, meanwhile a variable that was declared using Integer holds a reference to the value. According to java API: "The Integer class wraps a value of the primitive type int in an object. An object of type Int...
A short is a 16-bit signed integer. It has a minimum value of -215 (-32,768), and a maximum value of 215 ‑1 (32,767) short example = -48; short myShort = 987; short anotherShort = 17; short addedShorts = (short) (myShort + anotherShort); // 1,004 short subtractedShorts = (short) (myShort - an...
By default, long is a 64-bit signed integer (in Java 8, it can be either signed or unsigned). Signed, it can store a minimum value of -263, and a maximum value of 263 - 1, and unsigned it can store a minimum value of 0 and a maximum value of 264 - 1 long example = -42; long myLong = 284; long ano...
A boolean can store one of two values, either true or false boolean foo = true; System.out.println("foo = " + foo); // foo = true boolean bar = false; System.out.println("bar = " + bar); // bar = false boolean notFoo = !foo; System.out.prin...
A byte is a 8-bit signed integer. It can store a minimum value of -27 (-128), and a maximum value of 27 - 1 (127) byte example = -36; byte myByte = 96; byte anotherByte = 7; byte addedBytes = (byte) (myByte + anotherByte); // 103 byte subtractedBytes = (byte) (myBytes - anotherByte); // 89 ...
A float is a single-precision 32-bit IEEE 754 floating point number. By default, decimals are interpreted as doubles. To create a float, simply append an f to the decimal literal. double doubleExample = 0.5; // without 'f' after digits = double float floatExample = 0.5f; // with 'f' aft...
A double is a double-precision 64-bit IEEE 754 floating point number. double example = -7162.37; double myDouble = 974.21; double anotherDouble = 658.7; double addedDoubles = myDouble + anotherDouble; // 315.51 double subtractedDoubles = myDouble - anotherDouble; // 1632.91 double scientif...
A char can store a single 16-bit Unicode character. A character literal is enclosed in single quotes char myChar = 'u'; char myChar2 = '5'; char myChar3 = 65; // myChar3 == 'A' It has a minimum value of \u0000 (0 in the decimal representation, also called the null character) and a maximum valu...
Java and most other languages store negative integral numbers in a representation called 2's complement notation. For a unique binary representation of a data type using n bits, values are encoded like this: The least significant n-1 bits store a positive integral number x in integral representati...
PrimitiveBoxed TypeMemory Size of primitive / boxedbooleanBoolean1 byte / 16 bytesbyteByte1 byte / 16 bytesshortShort2 bytes / 16 bytescharChar2 bytes / 16 bytesintInteger4 bytes / 16 byteslongLong8 bytes / 16 bytesfloatFloat4 bytes / 16 bytesdoubleDouble8 bytes / 16 bytes Boxed objects always requ...
In Java, we can convert between integer values and floating-point values. Also, since every character corresponds to a number in the Unicode encoding, char types can be converted to and from the integer and floating-point types. boolean is the only primitive datatype that cannot be converted to or f...
Table showing size and values range of all primitive types: data typenumeric representationrange of valuesdefault valuebooleann/afalse and truefalsebyte8-bit signed-27 to 27 - 10-128 to +127short16-bit signed-215 to 215 - 10-32,768 to +32,767int32-bit signed-231 to 231 - 10-2,147,483,648 to +2,147,...

Page 1 of 1