J4. The data type and precision problem of Java BASIC program design structure

Source: Internet
Author: User
Tags ranges

1.1 Data Types

Data types in Java are divided into two types: basic data type and reference data type. As for the reference type, we will gradually understand it in the subsequent study, which is not covered here, and focuses on the basic data types. There are 8 basic data types in Java that store numeric values, characters, and Booleans, as shown in 4.1.

Figure 4.1 Java data type

1.3.1 Integer Type

Integer types are used to store integer values, that is, numeric values that do not have fractional parts. It can be positive, negative, or 0. Depending on the size of the memory, it can be divided into byte, short, int, and long 4 types. The memory and value ranges they occupy are shown in Table 4.2. The integer default type is int.

Figure 4.2 Integer type (1 bytes is 8 bits)

Model 1.3.1.1.byte

Use the BYTE keyword to define a byte variable, you can define multiple variables at once and assign them, or you can assign them without assigning values. Byte type is the smallest allocation of memory space in the integer, only 1 bytes allocated, the value range is also minimal, only between the -128~127, when used must be careful to avoid data overflow caused errors.

"Example 4.1" defines the byte type variable, and carries on the simple assignment operation, validates its several elements;

 Packagecn.basic.datatype.bytes; Public classbytepractice{ Public Static voidMain (string[] args) {byteA = a, b = 127, C; System.out.println (A+B);//The default type of an integer type is int, so when you perform an operation on a byte type, it is first converted to the int type, and then the Operation//System.out.println (c); We declare that the variable is not assigned a value, and the uninitialized variable is reported here .        /*b = n; byte has a memory space of 1 bytes and a value range of-128-127, so the problem of loss accuracy is reported here, but can still be resolved by forced transformation, the result is that the output does not match the actual value, which is caused by the loss of precision System.out.print ln (b);*/    }}

Note: If the member field is not initialized, it is automatically initialized to the default value at compile time. However, if the local variable is not initialized, it will be in the compile times wrong "variable uninitialized";

Model 1.3.1.2.Short

Short is a shorter integer, and you can define it by using the Quick keyword, you can define multiple variables at once, assign them to them, or do not assign values. The system allocates 2 bytes to the short type in memory, and the value range is much larger than byte, in between -32768~32767, although the range of values is larger, but still pay attention to overflow.

"Example 4.2" defines a short type variable

 short a = all;   // define short variables A, B, C, and assign values to a, b
Model 1.3.1.3.int

int is an integral type, using the INT keyword to define an int variable, you can define multiple variables at once and assign them to them, or you can not assign values. The value range of the INT type variable is very large, between -2147483648~2147483647, enough to use in general, so it is the most widely used in integer variables. The int type is the default type of the integer type.

"Example 4.3" defines the int type variable

int A = all, B = +;   //// define int variables A, b, C, and assign to a, B
Model 1.3.1.4.long

Long is a longer shape, using the long keyword to define a long variable, you can define more than one variable at a time and assign it, or you can not assign a value. When assigning a long variable, the end must be prefixed with "L" or "L" (because the uppercase of I and I are similar, so we generally use L for readability.) ), otherwise it will not be considered long and will be treated by default as int, which may cause an overflow memory error. Therefore, when the value is too large to exceed the int range, the long type is used, and the system is given a long type of 8 bytes, and the value range is greater, between -9223372036854775808~9223372036854775807.

"Example 4.4" defines a long variable and makes some analysis of it;

 Packagecn.basic.datatype.longs; Public classlongpractice{ Public Static voidMain (string[] args) {LongA = 3890, B;//when we define a variable, if his value exceeds the range of int, but does not enforce it at the end,//B = 922337203685477; Then the program will treat it as an int, then an error will occur, indicating out of range//System.out.println (b); It suggests we're too big.System.out.println (a); b= 922337203685477L;        System.out.println (b); b= 922337203685477l;        System.out.println (b); //B = 922337203685477 L; We should be aware that when defining a long variable, the initialization is immediately followed by "L" or "L", and there cannot be a space, otherwise the error will be indicated .System.out.println (b); }}

It is best to add l at the end of the definition of Long data, since L and 1 are not well differentiated;

The above four types of integers have 3 representations in Java programs, namely decimal, octal, and hexadecimal.

    • L decimal notation. The form of the decimal, is the most familiar form of expression in our real world, that is, every 10 into 1, each digit on the maximum of 9, such as 120, 99, etc. are decimal.
    • L octal notation. Octal is 8 in 1, the number on each digit is 7, and must start with 0. For example, 0123 (converted to decimal is 1*8^2+2*8^1+3*8^0=64+16+3=83), 123 (converted to decimal number-(1*8^2+2*8^1+3*8^0) =-(64+16+3) =-83) are octal.
    • L hexadecimal notation. In fact, there are 16 in ancient China, so-called Dora is so, every 16 into 1, the maximum number per digit is F (15), and must start with 0 x or 0x. such as 0x25 converted to decimal is 37.

The various types of "example 4.5" integers

 PackageCn.basic.datatype.integer; Public classintegerpractice{ Public Static voidMain (string[] args) {byteA = 127 ;  Shortb = 3276 ; intc = 214748364 ; LongD = 21474836423444L ; LongResultl = a + B + C +D; //int resultd = a + B + C + D; When a high-precision data type is converted to a low precision, the error of possible loss of precision is reported//int dd = (int) d;        intDD =NewLong (d). Intvalue (); intResultd = a + B + C +DD; System.out.println ("Result is" +RESULTL); System.out.println ("Result is" +Resultd); }}

1.3.2 Floating-point types

A floating-point type represents a number that has a decimal part. Floating-point types in the Java language are divided into single-precision floating-point types (float) and double-precision floating-point types (double), which have different ranges of values. The default type for floating-point types is double.

1.3.2.1 Float Type

Float is a single-precision floating-point type, using the FLOAT keyword to define the float variable, you can define one or more variables at a time and assign them, or you can not assign values. You must add "F" or "F" to the end of the float type, and if not, the system will treat it by default as a double type, and the value range of float is between 1.4e-45~3.4028235e38.

"Example 4.6" is defined for float type;

 Package cn.basic.datatype.floats;  Public class floatpractice{    publicstaticvoid  main (string[] args) {        = 1.4e-45f ;        System.out.print (a);    }}

1.3.2.2 Double Type

Double type is a two-precision floating-point type, using the Double keyword to define a double variable, you can define multiple variables at once and assign them, or you can assign them without assigning values. When assigning a double type, you can use the suffix "D" or "D" to explicitly indicate that this is a double type of data, but the addition does not have a hard requirement, you can add or do not add, The value range of the double type variable is between 4.9E-324 and 1.2976931348623157E-308.

The difference between single and double precision is explained in the following discussion of data accuracy.

"Example 4.6" defines a double type variable and a float type variable, and assigns, computes and outputs them;

 PackageCn.basic.datatype.doubles;classdoublepractice{ Public Static voidMain (string[] args) {DoubleA = 987.987123456789012D, B = 789.789879887918799d ; Doublec = 68.8978978977 ; floatAA = 987.987123F;//when we define a float variable and do not end with "F" or "F", the error may be lost in precision        floatD = 987.987123456789012F ; floatE = 987.987153456789012F, F = 987.987144456789012f ; System.out.println (A+ "and" +b);        System.out.println (c); SYSTEM.OUT.PRINTLN (AA+ "and" +d+ "and" +e+ "and" +f); }}

1.3.3 Character Types

Char is a character type. Declared using the Char keyword, which stores a single character, and the system allocates two bytes of memory space. When defining a character type variable, enclose it in single quotation marks. such as ' s ' represents a character, and only one character in single quotation marks, more is not a character type, but a string type, need to be declared in double quotation marks.

The Java characters are Unicode encoded and each character occupies two bytes, which can be represented by hexadecimal encoding. "Note: Unicode is a unified global language encoding, all characters are allocated two bytes of space"

Like the C, C + + language, the Java language can also treat characters as integers, because Unicode encoding uses unsigned encoding to store 65,536 characters (0X0000~0XFFFF), so the characters in Java can handle almost every language in the country. If you want the characters in the corresponding position in the Unicode table represented by a number between 0~65536, you must also use a char-type explicit conversion.

There is a special character in the character type, preceded by a backslash "\", followed by one or more characters, with a specific meaning, different from the original meaning of the character, called the escape character, and the escape character in Java is shown below.

Note: The escape character is also a character, so use the same ' single quote '.

Example 4.7 creates a class charpractice that outputs some characters from certain locations in a Unicode table and the position of some characters in a Unicode table above the console.

 Packagecn.basic.datatype.ch;classcharpractice{ Public Static voidMain (string[] args) {intA = ' d ', AA = 97 ; Charb = A, c = ' a ' ; Chard = ' \ \ ', E = ' \b ', f = ' \ n ', g = ' \u2605 ', h = ' \u0052 ';//Escape Character        Chari = ' \u4e08 '; System.out.println (A+ "and" +aa+ "and" +b+ "and" +c); System.out.println (d+ "and" +e+ "and" + "and" +g+ "and" +h);        System.out.println (f); System.out.println (i);    }}

4.3.4 Boolean type

Boolean types are also called logical types, only "true" and "false" two values, respectively, represent "true" and "false" in Boolean logic, and use the Boolean keyword to declare a boolean type variable, usually used in the process control as a criterion.

"Example 4.8" declares a Boolean variable and outputs it;

Boolean true false  ;        SYSTEM.OUT.PRINTLN (ABC+ "or" +CBA);         if (ABC) System.out.println ("True is true!") ");         Else {            System.out.println ("false is false!") ");        }         if (CBA) System.out.println ("True is true!") ");         Else {            System.out.println ("false is false!") ");        }

J4. The data type and precision problem of Java BASIC program design structure

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.