YouTip LogoYouTip

Java Basic Datatypes

Variables are used to store values in memory. This means that when you create a variable, you need to allocate space in memory for it. The memory management system allocates storage space for variables based on their type, and the allocated space can only store data of that type. !(#) Therefore, by defining variables of different types, you can store integers, decimals, or characters in memory. Java has two major data types: * Primitive Data Types * Reference Data Types * * * ## Primitive Data Types The Java language provides eight primitive types. Six are numeric types (four integer types, two floating-point types), one is a character type, and one is a boolean type. **byte:** * The byte data type is an 8-bit signed two's complement integer; * Minimum value is -128 (-2^7); * Maximum value is 127 (2^7 - 1); * Default value is 0; * The byte type is used to save space in large arrays, mainly as a substitute for integers, because a byte variable occupies only a quarter of the space of an int type; * Example: byte a = 100, byte b = -50. **short:** * The short data type is a 16-bit signed two's complement integer; * Minimum value is -32768 (-2^15); * Maximum value is 32767 (2^15 - 1); * The short data type can also save space like byte. A short variable occupies half the space of an int variable; * Default value is 0; * Example: short s = 1000, short r = -20000. **int:** * The int data type is a 32-bit signed two's complement integer; * Minimum value is -2,147,483,648 (-2^31); * Maximum value is 2,147,483,647 (2^31 - 1); * Generally, integer variables default to the int type; * Default value is 0; * Example: int a = 100000, int b = -200000. **long:** * The long data type is a 64-bit signed two's complement integer; * Minimum value is -9,223,372,036,854,775,808 (-2^63); * Maximum value is 9,223,372,036,854,775,807 (2^63 - 1); * This type is mainly used in systems requiring large integers; * Default value is 0L; * Example: long a = 100000L, long b = -200000L. "L" is theoretically case-insensitive, but writing it as "l" can be easily confused with the number "1", making it hard to distinguish. Therefore, it's best to use uppercase. **float:** * The float data type is a single-precision 32-bit IEEE 754 floating-point number; * Using float can save memory space when storing large floating-point arrays; * Default value is 0.0f; * Floating-point numbers cannot represent exact values, such as currency; * Example: float f1 = 234.5f. **double:** * The double data type is a double-precision 64-bit IEEE 754 floating-point number; * The default type for floating-point numbers is double; * The double type also cannot represent exact values, such as currency; * Default value is 0.0d; * Example: double d1 = 7D ;double d2 = 7.; double d3 = 8.0; double d4 = 8.D; double d5 = 12.9867; 7 is an int literal, while 7D, 7., and 8.0 are double literals. **boolean:** * The boolean data type represents one bit of information; * It has only two possible values: true and false; * This type is used as a flag to record true/false situations; * Default value is false; * Example: boolean one = true. **char:** * The char type is a single 16-bit Unicode character; * Minimum value is u0000 (decimal equivalent is 0); * Maximum value is uffff (which is 65535); * The char data type can store any character; * Example: char letter = 'A';. ### Example We don't need to memorize the value ranges of the primitive numeric types, because their values are already defined as constants in the corresponding wrapper classes. See the example below: ## Example public class PrimitiveTypeTest{public static void main(String[]args){System.out.println("Primitive type: byte binary bits: " + Byte.SIZE); System.out.println("Wrapper class: java.lang.Byte"); System.out.println("Minimum value: Byte.MIN_VALUE=" + Byte.MIN_VALUE); System.out.println("Maximum value: Byte.MAX_VALUE=" + Byte.MAX_VALUE); System.out.println(); System.out.println("Primitive type: short binary bits: " + Short.SIZE); System.out.println("Wrapper class: java.lang.Short"); System.out.println("Minimum value: Short.MIN_VALUE=" + Short.MIN_VALUE); System.out.println("Maximum value: Short.MAX_VALUE=" + Short.MAX_VALUE); System.out.println(); System.out.println("Primitive type: int binary bits: " + Integer.SIZE); System.out.println("Wrapper class: java.lang.Integer"); System.out.println("Minimum value: Integer.MIN_VALUE=" + Integer.MIN_VALUE); System.out.println("Maximum value: Integer.MAX_VALUE=" + Integer.MAX_VALUE); System.out.println(); System.out.println("Primitive type: long binary bits: " + Long.SIZE); System.out.println("Wrapper class: java.lang.Long"); System.out.println("Minimum value: Long.MIN_VALUE=" + Long.MIN_VALUE); System.out.println("Maximum value: Long.MAX_VALUE=" + Long.MAX_VALUE); System.out.println(); System.out.println("Primitive type: float binary bits: " + Float.SIZE); System.out.println("Wrapper class: java.lang.Float"); System.out.println("Minimum value: Float.MIN_VALUE=" + Float.MIN_VALUE); System.out.println("Maximum value: Float.MAX_VALUE=" + Float.MAX_VALUE); System.out.println(); System.out.println("Primitive type: double binary bits: " + Double.SIZE); System.out.println("Wrapper class: java.lang.Double"); System.out.println("Minimum value: Double.MIN_VALUE=" + Double.MIN_VALUE); System.out.println("Maximum value: Double.MAX_VALUE=" + Double.MAX_VALUE); System.out.println(); System.out.println("Primitive type: char binary bits: " + Character.SIZE); System.out.println("Wrapper class: java.lang.Character"); System.out.println("Minimum value: Character.MIN_VALUE=" + (int)Character.MIN_VALUE); System.out.println("Maximum value: Character.MAX_VALUE=" + (int)Character.MAX_VALUE); }} [Run Example Β»](#) Compiling the above code produces the following output: Primitive type: byte binary bits: 8Wrapper class: java.lang.ByteMinimum value: Byte.MIN_VALUE=-128Maximum value: Byte.MAX_VALUE=127Primitive type: short binary bits: 16Wrapper class: java.lang.ShortMinimum value: Short.MIN_VALUE=-32768Maximum value: Short.MAX_VALUE=32767Primitive type: int binary bits: 32Wrapper class: java.lang.IntegerMinimum value: Integer.MIN_VALUE=-2147483648Maximum value: Integer.MAX_VALUE=2147483647Primitive type: long binary bits: 64Wrapper class: java.lang.LongMinimum value: Long.MIN_VALUE=-9223372036854775808Maximum value: Long.MAX_VALUE=9223372036854775807Primitive type: float binary bits: 32Wrapper class: java.lang.FloatMinimum value: Float.MIN_VALUE=1.4E-45Maximum value: Float.MAX_VALUE=3.4028235E38Primitive type: double binary bits: 64Wrapper class: java.lang.DoubleMinimum value: Double.MIN_VALUE=4.9E-324Maximum value: Double.MAX_VALUE=1.7976931348623157E308Primitive type: char binary bits: 16Wrapper class: java.lang.CharacterMinimum value: Character.MIN_VALUE=0Maximum value: Character.MAX_VALUE=65535 The minimum and maximum values for Float and Double are output in scientific notation. The "E+number" at the end indicates that the number before E should be multiplied by 10 to the power of that number. For example, 3.14E3 is 3.14 Γ— 10^3 = 3140, and 3.14E-3 is 3.14 Γ— 10^-3 = 0.00314. In fact, there is another primitive type in JAVA called void, which also has a corresponding wrapper class java.lang.Void, but we cannot directly operate on them. ### Type Default Values The following table lists the default values for Java's various types: | Data Type | Default Value | | --- | --- | | int | 0 | | long | 0L | | short | 0 | | char | 'u0000' | | byte | 0 | | float | 0.0f | | double | 0.0d | | boolean | false | | Reference Types (Class, Interface, Array) | null | Explanation: * The default value for `int`, `short`, `long`, `byte` is 0. * The default value for `char` is `u0000` (null character). * The default value for `float` is `0.0f`. * The default value for `double` is `0.0d`. * The default value for `boolean` is `false`. * The default value for reference types (class, interface, array) is `null`. ## Example public class Test{static boolean bool; static byte by; static char ch; static double d; static float f; static int i; static long l; static short sh; static String str; public static void main(String[]args){System.out.println("Bool :" + bool); System.out.println("Byte :" + by); System.out.println("Character:" + ch); System.out.println("Double :" + d); System.out.println("Float :" + f); System.out.println("Integer :" + i); System.out.println("Long :" + l); System.out.println("Short :" + sh); System.out.println("String :" + str); }} The output of the example is: Bool :falseByte :0Character:Double :0.0Float :0.0Integer :0Long :0Short :0String :null * * * ## Reference Types * In Java, reference type variables are very similar to pointers in C/C++. A reference type points to an object, and the variable that points to the object is a reference variable. These variables are declared as a specific type, such as Employee, Puppy, etc., when declared. Once a variable is declared, its type cannot be changed. * Objects and arrays are reference data types. * The default value for all reference types is null. * A reference variable can be used to refer to any compatible type. * Example: Site site = new Site("Tutorial"). * * * ## Java Constants Constants cannot be modified during program execution. In Java, the `final` keyword is used to modify constants. The declaration method is similar to variables: final double PI = 3.1415927; Although constant names can also be lowercase, uppercase letters are usually used to represent constants for easy identification. Literals can be assigned to variables of any primitive type. For example: byte a = 68;char a = 'A' byte, int, long, and short can be represented in decimal, hexadecimal, and octal. When using literals, the prefix `0` indicates octal, and the prefix `0x` indicates hexadecimal. For example: int decimal = 100;int octal = 0144;int hexa = 0x64; Like other languages, Java string literals are character sequences enclosed in two double quotes. Here are examples of string literals: "Hello World""twonlines"""This is in quotes"" String literals and character variables can contain any Unicode character. For example: char a = 'u0001';String a = "u0001"; The Java language supports some special escape character sequences. | Symbol | Character Meaning | | --- | --- | | n | Newline (0x0a) | | r | Carriage Return (0x0d) | | f | Form Feed (0x0c) | | b | Backspace (0x08) | | | Null Character (0x0) | | s | Space (0x20) | | t | Tab | | " | Double Quote | | ' | Single Quote | | | Backslash | | ddd | Octal Character (ddd) | | uxxxx | Hexadecimal Unicode Character (xxxx) | * * * ## Automatic Type Conversion **Integer, real (constant), and character type data can be mixed in operations. In operations, data of different types are first converted to the same type, then the operation is performed.** Conversion proceeds from lower to higher levels. Low ------------------------------------> Highbyte,short,charβ€”> int β€”> longβ€”> float β€”> double Data type conversion must satisfy the following rules: * 1. Cannot perform type conversion on the boolean type. * 2. Cannot convert an object type to an unrelated class object. * 3. When converting a larger capacity type to a smaller capacity type, a forced type conversion must be used. * 4. The conversion process may cause overflow or loss of precision, for example: int i =128; byte b = (byte)i; Because the byte type is 8 bits, with a maximum value of 127, when int is forcibly converted to byte type, the value 128 will cause overflow. * 5. Conversion from floating-point to integer is done by discarding the decimal part, not by rounding, for example: (int)23.7 == 23;(int)-45.89f == -45 ### Automatic Type Conversion The bit length of the data type before conversion must be lower than that after conversion. For example: the short data type has a bit length of 16, so it can be automatically converted to the int type with 32 bits. Similarly, the float data type has a bit length of 32 and can be automatically converted to the double type with 64 bits. ## Example public class ZiDongLeiZhuan{public static void main(String[]args){char c1='a';int i1 = c1;System.out.println("char automatically converted to int value equals "+i1); char c2 = 'A';int i2 = c2+1;System.out.println("char type and int calculation result equals "+i2); }} The result is: char automatically converted to int value equals 97char type and int calculation result equals 66 **Analysis:** The value of c1 is the character **a**. Looking up the ASCII code table, the corresponding int type value is 97. A corresponds to 65, so i2=65+1=66. ### Forced Type Conversion * 1. The condition is that the converted data types must be compatible. * 2. Format: (type)value where type is the data type after forced conversion. Example: ## Example public class ForceTransform{public static void main(String[]args){int i1 = 123; byte b = (byte)i1;System.out.println("int forcibly converted to byte value equals "+b); }} The result is: int forcibly converted to byte value equals 123 ### Implicit Forced Type Conversion * 1. The default type for integers is int. * 2. Decimals default to the double type. When defining a float type, you must append F or f after the number. This section explained Java's basic data types. The next section will explore different variable types and their usage.
← Plugins Layout AccordionJava Object Classes β†’