Java basic data type

Java basic data type

Variable is to apply memory to store the value. In other words, when creating a variable, you need to apply for space in memory.
The memory management system allocates storage space for the variable according to the type of the variable, and the allocated space can only be used to store the type data.
Therefore, by defining different types of variables, you can store integers, decimals, or characters in memory.
Java’s two major data types:
  • Built-in data type
  • Reference data type

Built-in data type

The Java language provides eight basic types. Six types of numbers (four integer types, two floating point types), a character type, and a Boolean type.
Byte:
  • The byte data type is an 8-bit, signed, integers in two’s complement;
  • The minimum is -128 (-2 ^ 7);
  • The maximum is 127 (2 ^ 7-1);
  • The default value is 0;
  • Byte type used in large arrays to save space, mainly replace the integer, because the byte variable occupies only a quarter of the type of int;
  • Example: byte a = 100, byte b = -50.
Short
  • The short data type is a 16-bit, signed integer in two’s complement
  • The minimum is -32768 (-2 ^ 15);
  • The maximum is 32767 (2 ^ 15 – 1);
  • The Short data type can also save space like a byte. A short variable is a half of the space occupied by the int variable;
  • The default value is 0;
  • Example: short s = 1000, short r = -20000.
Int:
  • Int The data type is a 32-bit, signed integer in two’s complement;
  • The minimum value is -2,147,483,648 (-2 ^ 31);
  • The maximum is 2,147,483,647 (2 ^ 31 – 1);
  • In general, the integer variable defaults to the int type;
  • The default value is 0;
  • Example: int a = 100000, int b = -200000.
Long:
  • The long data type is a 64-bit, signed integer in two’s complement;
  • The minimum is -9,223,372,036,854,775,808 (-2 ^ 63);
  • The maximum is 9,223,372,036,854,775,807 (2 ^ 63 -1);
  • This type is mainly used in systems that require larger integers;
  • The default value is 0L;
  • Example: long a = 100000L, Long b = -200000L.
Float:
  • The float data type is a single-precision, 32-bit, IEEE 754 compliant floating point number;
  • Float saves memory when storing large floating-point arrays;
  • The default value is 0.0f;
  • Floating points can not be used to represent exact values, such as currency;
  • Example: float f1 = 234.5f.
Double:
  • Double data type is double, 64-bit, IEEE 754 compliant floating point number;
  • The default type of floating point is double;
  • The double type also does not represent an exact value, such as currency;
  • The default value is 0.0d;
  • Example: double d1 = 123.4.
Boolean:
  • Boolean The data type represents a bit of information;
  • There are only two values: true and false;
  • This type is only used as a flag to record true / false;
  • The default is false;
  • Example: boolean one = true.
Char:
  • Char type is a single 16-bit Unicode character;
  • The minimum value is ‘ u0000’ (that is, 0);
  • The maximum is ‘ uffff’ (ie 65,535);
  • Char data type can store any character;
  • Example: char letter = ‘A’.

Examples

For the basic type of numeric type, we do not have to force to remember, because their values ​​have been defined in the form of a constant in the corresponding packaging class. See the following example:

Examples

Public class PrimitiveTypeTest { public static void main ( String [ ] args ) { // byte The System . OUT . The println ( basic types: byte binary digit: + Byte . SIZE ) ; the System . OUT . The println ( packaging: java.lang.Byte ) ; the System . OUT . The println ( Min: = Byte.MIN_VALUE + Byte . MIN_VALUE ) ; the System . OUT . the println ( maximum: = Byte.MAX_VALUE, whichever + Byte . MAX_VALUE ) ; the System . OUT . The println ( ) ; // Short The System . OUT . The println ( basic types: short number of bits: + Short . SIZE ) ; the System . OUT . The println ( packaging: a java.lang.Short ) ; the System . OUT . The println ( Min: = Short.MIN_VALUE + Short . MIN_VALUE ) ; the System . OUT . the println ( maximum: = Short.MAX_VALUE + Short . MAX_VALUE ) ; the System . OUT . The println ( ) ; // int The System . OUT . The println ( basic types: int binary digit: + Integer . SIZE ) ; the System . OUT . The println ( packaging: java.lang.Integer ) ; the System . OUT . The println ( Min: = of Integer.MIN_VALUE + Integer . MIN_VALUE ) ; the System . OUT . the println ( maximum: Integer.= MAX_VALUE + Integer . MAX_VALUE ) ; the System . OUT . The println ( ) ; // Long The System . OUT . The println ( basic types: long binary digit: + Long . SIZE ) ; the System . OUT . The println ( packaging: a java.lang.Long ) ; the System . OUT . The println ( Min: = of Long.MIN_VALUE + Long . MIN_VALUE ) ; the System . OUT . the println ( maximum: = of Long.MAX_VALUE + Long . MAX_VALUE ) ; the System . OUT . The println ( ) ; // a float The System . OUT . The println ( basic types: float binary digit: + the Float . SIZE ) ; the System . OUT . The println ( packaging: a java.lang.Float ) ; the System . OUT . The println ( Min: = Float.MIN_VALUE + the Float . MIN_VALUE ) ; the System . OUT . the println ( maximum: = Float.MAX_VALUE + The Float . MAX_VALUE ) ; the System . OUT . The println ( ) ; // Double The System . OUT . The println ( basic types: double binary digit: + Double . SIZE ) ; the System . OUT . The println ( packaging: a java.lang.Double ) ; the System . OUT . The println ( Min: = Double.MIN_VALUE + Double . MIN_VALUE ) ; the System . OUT . the println ( maximum: Double.= MAX_VALUE + Double . MAX_VALUE ) ; the System . OUT . The println ( ) ; // char The System . OUT . The println ( basic types: char number of bits: + Character . SIZE ) ; the System . OUT . The println ( packaging: java.lang.Character ) ; // in numerical form, rather than form character Character.MIN_VALUE output to the console The System . OUT . The println ( Min: Character.MIN_VALUE = + ( int ) Character . MIN_VALUE ) ; // in numerical form, rather than in the form of the character to the console Character.MAX_VALUE The System . OUT . The println ( maximum: Character.MAX_VALUE = + ( int ) Character . MAX_VALUE ) ; } }

Running examples »

Compile the above code output as follows:
Basic type: byte Binary bits: 8 Packing class: java . Lang . Byte min: Byte . MIN_VALUE = - 128 max: Byte . MAX_VALUE = 127 




Basic Type: short Number of Bits: 16 Packaging Class: java . Lang . Short Minimum: Short . MIN_VALUE = - 32768 Maximum: Short . MAX_VALUE = 32767




Basic Type: int Binary Number of Bits: 32 Packaging Class: java . Lang . Integer Min: Integer . MIN_VALUE = - 2147483648 Max: Integer . MAX_VALUE = 2147483647




Basic types: Long number of bits: 64 packaging: Java . Lang . Long minimum: Long . MIN_VALUE = - 9223372036854775808 maximum: Long . MAX_VALUE = 9223372036854775807




Basic Type: float Number of Bits: 32 Packaging Class: java . Lang . Float Min: Float . MIN_VALUE = 1.4E-45 Maximum: Float . MAX_VALUE = 3.4028235E38




Basic Type: double Binary Number of Bits: 64 Packaging Class: java . Lang . Double Min: Double . MIN_VALUE = 4.9E-324 Maximum: Double . MAX_VALUE = 1.7976931348623157E308




Basic Type: char Binary Number of Bits: 16 Packaging Class: java . Lang . Character Min: Character . MIN_VALUE = 0 Max: Character . MAX_VALUE = 65535


The minimum and maximum values ​​of Float and Double are output in the form of scientific notation, and the ending “E + number” indicates how many times the number before E is multiplied by 10. For example, 3.14E3 is 3.14 × 10 3= 3140, 3.14E-3 is 3.14 x 10 -3 = 0.00314.
In fact, JAVA there is another basic type of void, it also has the corresponding wrapper class java.lang.Void, but we can not directly operate on them.

Reference type

  • In Java, the reference type of variable is very similar to the C / C ++ pointer. The reference type points to an object, and the variable that points to the object is a reference variable. These variables are specified as a specific type when declaring, such as Employee, Pubby, and so on. Once the variable is declared, the type can not be changed.
  • Object, array is the reference data type.
  • The default values ​​for all reference types are null.
  • A reference variable can be used to refer to any type that is compatible with it.
  • Example: Site site = new Site (“Runoob”).

Java constants

Constant will not be modified when the program is running.
Using the final keyword in Java to decorate constants, declarations are similar to variables:
Final double PI = 3.1415927 ;  
Although constant names can also be used in lower case, but in order to facilitate identification, usually using capital letters that constant.
The literal can be assigned to any built-in type of variable. E.g:
Byte a = 68 ; char a = 'A' 
Byte, int, long, and short can be expressed in decimal, hexadecimal, and octal.
When a constant is used, the prefix 0 represents octal and the prefix 0x represents hexadecimal. E.g:
Int decimal = 100 ; int octal = 0144 ; int hexa = 0x64 ;   

Like other languages, Java string constants are also contained in the two quotation marks between the character sequence. The following is an example of a string literal:
"Hello World" "two  nlines" "" This is in quotes  ""

String constants and character constants can contain any Unicode characters. E.g:
Char a = ' u0001' ; String a = " u0001" ; 
The Java language supports some special escape character sequences.
symbol Character meaning
N Line feed (0x0a)
R Enter (0x0d)
F Form feed (0x0c)
B Backspace (0x08)
S Space (0x20)
T Tabs
Double quotes
apostrophe
\ Backslash
Ddd Octal characters (ddd)
Uxxxx Hexadecimal Unicode characters (xxxx)

Automatic type conversion

Integer, real (constant), character data can be mixed operation. In the operation, different types of data are first converted to the same type, and then the operation is performed.
Convert from low to high.
Low ------------------------------------> high    

Byte , short , char -> int -> long -> float -> double
Data type conversion must meet the following rules:
  • 1. You can not type a boolean type.
  • 2. Can not convert the object type to an object of an unrelated class.
  • 3. Mandatory type conversion must be used when converting a large type to a type with a small capacity.
  • 4. The conversion process may lead to overflow or loss accuracy, for example:
    Int i = 128 ; byte b = ( byte ) i ;   
    Because the byte type when the 8-bit, the maximum value of 127, so when the mandatory conversion to int type value 128 will lead to overflow.
  • 5. Float to integer conversion is obtained by discarding the decimal, rather than rounding, for example:
    ( Int ) 23.7 == 23 ; ( int ) - 45.89f == - 45    

Automatic type conversion

The number of bits of the data type before the conversion must be lower than the converted data type. For example, if the number of bits of the short data type is 16 bits, the int type of 32 bits can be automatically converted, and the number of bits of the same float data type For 32, can be automatically converted to 64-bit double type.

Examples

Public class ZiDongLeiZhuan { public static void main ( String [ ] args ) { char c1 = a ; // define a char type Int i1 = c1 ; // char auto type is converted to int The System . OUT . The println ( char automatic conversion to a value equal to the int + I1 ) ; char C2 = A ; // define a char type Int i2 = c2 + 1 ; // char type and int type calculation System . Out . Println ( char type and int calculated value is equal to + i2 ) ; } }
The result is:
Char auto type is converted to int after the value is equal to 97 char type and int calculated value is equal to 66
Analysis: c1 value for the character ‘a’, check ascii code table shows the corresponding int value of 97, ‘A’ corresponds to 65, so i2 = 65 +1 = 66.

Forced type conversion

  • 1. The condition is that the converted data type must be compatible.
  • 2. Format: (type) value type is to force the type of conversion after the data type instance:

    Examples

    Public class QiangZhiZhuanHuan { public static void main ( String [ ] args ) { int i1 = 123 ; byte b = ( byte ) i1 ; // Forcing type to byte System . Out . Println ( int Forces the conversion of the type to byte after the value is equal to + b ) ; } }
    operation result:
    Int The value of the mandatory type converted to byte is equal to 123

Implicit mandatory type conversion

  • 1. The default type of an integer is int.
  • 2. Floating-point type does not exist because the float type must be followed by a number followed by F or f.
This section explains the basic data types of Java. The next section explores the different types of variables and their usage.

Leave a Reply

Your email address will not be published. Required fields are marked *