Java Native type (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/12/08/2809371.html

English from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

JavaProgramming LanguageIs a static type, which indicates that the variable must be declared before use. This involves clarifying the type and name of the variable, as you can see:

 
Int Gear = 1;

What I told you aboveProgramThere is a field named "gear" that stores digital data. The initial value is "1 ". The data type of the variable determines the value it can contain and the operations it can perform. For int, the Java programming language supports other native data types. Native types are predefined by programming languages, and the type names are reserved words. Native values are not shared with other native values. The Java programming language supports eight native types:

Byte: the data type of a byte is an 8-Bit Signed binary complement integer. The minimum value is 128, and the maximum value is 127 (inclusive ). Memory saving is very important, and byte data types are useful in saving memory in large arrays. If the variable value restriction is helpful for clarificationCode, You can also use byte to replace Int. In fact, the range of variables can be used as a document.

Short: the short data type is a 16-Bit Signed complement integer. The minimum value is-32,768, and the maximum value is 32,767 (inclusive ). There is also the same principle as byte: When memory saving is very important, short data types are useful in saving memory in large arrays.

INT: the int data type is a 32-Bit Signed complement integer. The minimum value is-2,147,483,648, and the maximum value is 2,147,483,647 (inclusive ). For the complete value, this data type is usually selected by default, unless there are other reasons (such as the preceding reasons ). This data type is sufficient for your program to use integers in most cases. If you need a value with a larger range, you can use long.

Long: The long data type is a 64-Bit Signed complement integer. The minimum value is-9,223,372,036,854,775,808, and the maximum value is 9,223,372,036,854,775,807 (inclusive ). This data type can be used when the required value range exceeds Int.

Float: float data type is a single-precision 32-bit IEEE 754 floating point number. Its value range is beyond the scope of this article, but you can refer to the floating point type in the Java language specification. Similar to the recommendations of byte and short, when you use a large number of floating point arrays, you need to save memory and use float. This data type should never be used for exact values, such as currencies. Therefore, you must use the java. Math. bigdecimal class instead. The Java platform provides numbers and strings including bigdecimal and other useful classes.

Double: the double data type is a single-precision 64-bit IEEE 754 floating point number. Its value range is beyond the scope of this article, but you can refer to the floating point type in the Java language specification. For small values, this data type is selected by default. As suggested above, this data type should never be used for exact values.

Boolean: boolean data type has only two possible values: true and false. Use this data as a simple tag to track true/false conditions. This data type represents the information of a bit, but its "size" is not clearly defined.

CHAR: the char data type is a single 16-bit Unicode character. The minimum value is '\ u000000' (or 0), and the maximum value is' \ uffff' (or 65,535 included ).

In addition to the eight native data types listed above, the Java programming language provides special support for strings through the java. Lang. string class. The string object is automatically created when two double quotes are used to enclose the string. For example, string S = "this is a string"; String object is immutable, which indicates that once it is created, its value is immutable. Technically speaking, the class string is not a native type, but it can be considered as a special support of the Java language. You may tend to think that it is a native type.

 

Default Value

When a field is declared, it is not necessary to assign values. If the field is declared but not initialized, the compiler will set a reasonable default value. Generally, the default value is 0 or null, depending on the data type. However, relying on this default value is generally considered a bad programming style.

The following icons summarize the default values of the data types involved above.

 

Data Type Default Value (for fields)
Byte 0
Short 0
Int 0
Long 0l
Float 0.0f
Double 0.0d
Char '\ U000000'
String (or any object) Null
Boolean False

 

Local variables are slightly different. The Compiler does not assign default values to a local variable that is not initialized. If you do not initialize a variable when declaring a local variable, you must assign a value to it before using it. When accessing a local variable without initialization, the compiler reports an error.

 

Constant

You may notice that native type variables are initialized without the use of the new keyword. The native type is a special data type built in the Java language and is not an object created by a class. Constant isSource code. Constants directly indicate that calculation is not required in the code. The following shows that native variables can assign values to constants:

 
Boolean result = true; char capit1c = 'C'; byte B = 100; short S = 10000; int I = 100000; integer literals

 

Integer constant

If an integer constant ends with the letter L or l, it is of the long type. Otherwise, it is of the int type. We recommend that you use uppercase letters (l) because it is difficult to distinguish lowercase letters (l) from numbers (1.

From an int constant, you can create integers byte, short, Int, long. Long constant. You can create a value of the long type that exceeds the int range. An integer constant can be expressed by the following digital system:

Decimal: A 10-digit system that contains numbers ranging from 0 to 9.

Hexadecimal: hexadecimal, containing 0 to 9 letters A to F.

Binary: a binary system, containing numbers ranging from 0 to 1 (Java SE 7 and binary constants can be created later ).

 

In general programming, the decimal system is the only digital system used. However, if you need to use it in other digital systems, the following example demonstrates the correct syntax. The prefix 0x indicates hexadecimal, And the 0b indicates binary:

// The number 26, in decimalint decval = 26; // The number 26, in hexadecimalint hexval = 0x1a; // The number 26, in binaryint binval = 0b11010;

 

Floating Point constant

The floating-point constant ending with the letter f or F is of the float type. Otherwise, the others are of the double type. An optional option here is to end with the letter D or D.

Floating Point (floating point and Double Precision) can also indicate the use of E or E (Scientific Notation), f or F (32-bit floating point constants) and D or D (64-bit Double Precision Floating Point constants; this is the default setting, which can be omitted by Convention ).

 
Double d1 = 123.4; // same value as D1, but in scientific notationdouble D2 = 1.234e2; float F1 = 123.4f;

 

Character and string constants

Char and string constants contain any Unicode (UTF-16) character. If the editor and file system allow, you can use strings directly in the code. If not, use "Unicode escape". For example, '\ u0108'. The Char constant uses 'single quote', And the String constant uses "Double quotation marks ". Unicode escape sequences can be used in other places of the program (such as field names), rather than Char or string constants.

The Java programming language also supports escape for char and string constants: \ B (Return key), \ t (Tab), \ n (line break), \ f (page break ), \ r (Press ENTER), \ "(double quotation marks), \ '(single quotation marks), \ (backslash ).

Null constant, used for any reference type value. Null can be copied to any variable to process the native type. The null value can be used to determine whether an object exists. Therefore, null is often used to mark in a program, indicating that some objects are unavailable.

Finally, there is a special constant that becomes a class constant. The format is the type name plus ". Class"; for example, String. Class. This indicates the type of the referenced object (or class.

 

The numeric constant is underlined.

In Java SE 7 and later versions, numeric constants can contain any underscore (_) in numbers (_). This feature allows you to group numbers in numeric constants to provide code readability.

For instances, if the value in the Code contains many numbers, you can use the underline character to separate the numbers into three groups, similar to using punctuation marks, such as commas, spaces, as separators.

The following example demonstrates the use of underscores in numeric constants:

 
Long creditcardnumber = bytes; long socialsecuritynumber = bytes; float Pi = 3.14_15f; long hexbytes = bytes; long hexwords = 0xcafe_babe; long maxlong = bytes; byte nybbles = bytes; long bytes = bytes;

You can only place underscores between numbers, not between the following:

Start or end of a value

Adjacent to the decimal point in a floating-point constant

F or l suffix before

Where the value is expected to be a string

The following example demonstrates the combination of a numeric constant and an invalid underline position:

 
// Invalid: cannot put underscores // adjacent to a decimal pointfloat PI1 = 3_.1415f; // invalid: cannot put underscores // adjacent to a decimal pointfloat Pi2 = 3. _ 1415f; // invalid: cannot put underscores // prior to an L suffixlong socialsecuritynumber1 = 999_99_9999_l; // This is an identifier, not // a numeric literalint X1 = _ 52; // OK (decimal literal) int X2 = 5_2; // invalid: cannot put underscores // at the end of a literalint X3 = 52 _; // OK (decimal literal) int X4 = 5______ _2; // invalid: cannot put underscores // In the 0x Radix prefixint X5 = 0_x52; // invalid: cannot put underscores // at the beginning of a numberint X6 = 0x_52; // OK (hexadecimal literal) int X7 = 0x5_2; // invalid: cannot put underscores // at the end of a numberint X8 = 0x52 _;

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.