Reviewing Java notes (2): understanding the data types of the Java language

Source: Internet
Author: User

This is the foundation. We will understand the following items:

1.Data in the computer is represented in binary. For a given byte of 0100011, what data does it represent? If it is ASCII encoding, this byte represents the uppercase letter C. If it is an unsigned integer, this byte represents the positive integer 67. Therefore, what data is actually represented by a binary string in the computer, it depends on how the binary string is interpreted, and the data type in the program determines how the binary string is interpreted as data.

2.The type determines the data representation in the computer, while the language compiler allocates the corresponding storage space based on the type of the data and explains the binary string, to generate more efficient target code.
For example:
Data of the signed integer type occupies 4 bytes.
Double-precision floating-point data occupies 8 bytes
 
3.After a data type is determined, the operations that can be performed on the data are also determined. For example, two integers can be used for addition, subtraction, multiplication, division, and modulo operations. The two strings can be compared, connected, and judged.

4.Java, C ++, and Pascal are all strongly typed languages. Java is divided into basic data types and composite data types. The basic data types include characters, integers, floating-point numbers, double-precision floating-point numbers, Boolean values, strings, and null values, composite data types include classes, interfaces, arrays, and so on. Note: the Java language is case sensitive.

5.Spaces, tabs, carriage returns, line breaks, and comments are collectively referred to as blank spaces. For the compiler, the only function of blank spaces is to separate words. A space is the same as three consecutive spaces or a line break.

6.Four elements of a variable: name, type, address, and value. The program uses the variable name to access the variable value. The variable type is explicitly declared by the programmer. The Compiler interprets the binary string in the bucket indicated by the variable address based on the variable type. A variable is associated with a region in the memory, the variable address refers to the location of the region. The data stored in the region is the value of the variable. The memory region may store different values at different times of running the program.

7.Type conversion rules in Java: When the Left and Right operands of the value assignment operation have different types, the right operand is first converted to the left operand type, and then the conversion result is assigned to the left operand. This rule only allows the value range of the left operand type to be greater than the value range of the right operand. For example, an int expression is assigned to a float type variable. If the value range of the left operand type is smaller than that of the right operand type, for example, if a double expression is assigned to an int or float type variable, some information may be lost during the type conversion process, therefore, the compiler reports errors in the source program and rejects the generation of the target code. If such an assignment operation is required, explicit type conversion is required.
The expression used to calculate the length of service salary is age * 1.5. The two operands of the binary operator in this expression are of different types. The value on the left is of the int type and the value on the right is of the double type. The compiler will convert age to double type, and then calculate the multiplication of two double type data. The result of the expression evaluation is also double type.

8.If explicit type conversion is controlled in a program, the conversion rules do not comply with the preceding rules. This type conversion process is called forced type conversion. A forced type conversion is a type name enclosed by parentheses before the expression.
For example, for the rectangular area, the two int type variables length and area represent the length and area respectively, and the rectangular width is obtained. The formula can be system. out. println (area/length); suppose area = 20, length = 8 because the two variables are of the int type, the division operation does not produce implicit type conversion, and the result is 2, instead of the expected 2.5. The following two methods can achieve the goal:
System. Out. println (double) area/length );
System. Out. println (area/(double) length );

9.Point out the compilation errors in the following programs and the correct methods:

Public class ex0207
...{
Public static void main (string [] ARGs)
...{
Int scaling = 0484;
Float size = scale * 1.5;
System. Out. Print ("size is" + size );
}
}
The solution can be found.

 

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.