& Sect; 2.1 data type
& Sect; 2.2 constant volume and variable volume
& Sect; 2.3 Integer data
& Sect; 2.4 floating point data (real)
& Sect; 2.5-character data
& Sect; 2.6 Boolean data
& Sect; 2.7 Example
And integer constants:
Similar to C and C ++, Java has three forms of integers:
① A decimal integer, such as 123,-, 0
② An octal integer starting with 0. For example, 0123 indicates the decimal number 83, and-011 indicates the decimal number-9.
③ A hexadecimal integer starting with 0x or 0X. For example, 0x123 indicates the decimal number 291, and-0X12 indicates the decimal number-18.
An integer constant occupies 32 digits in the machine and has an int value. For a long value, add L or l after the number. For example, 123L indicates a long integer, it occupies 64 bits in the machine.
II. Integer variables:
The types of integer variables include byte, short, int, and long. The following table lists the memory locations of each type and their ranges.
Int type is the most commonly used integer type. It indicates the data range of a 64-bit processor. However, for large-scale computing, a large integer is often encountered, which is beyond the range indicated by the int class. In this case, the long type is used.
Because different machines store multi-byte data in different ways, it may be low-byte to high-byte storage, or from high-byte to low-byte storage. In this way, when analyzing network protocols or file cells, it is appropriate to use the byte type to represent data to solve the byte storage sequence problem on different machines. In general, because the data range is small, it is easy to cause overflow and should be avoided.
The short type is rarely used. It limits the storage of data to the first high byte, followed by the lower-byte section, so that errors may occur on some machines.
III. Definitions of integer variables, such:
Byte B; // specify the variable B as the byte type.
Short s; // specify the variable s as the short type.
Int I; // specifies that the variable I is of the int type.
Long l; // specify the variable l as the long type
& Sect; 2.4 floating point (real) data
1. Real constants
Similar to C, C ++, Java has two real constants:
① In decimal format, which consists of digits and decimal places, and must have a decimal point, such as 0. 123,. 123,123., 123.0
② Scientific notation form. For example, 123e3 or 123E3, where e or E must be known before, and the exponent after e or E must be an integer. The real constant occupies 64 bits in the machine and has a value of the double type. For a float value, f or F should be added after the number, for example, 12.3F. It occupies 32 digits in the machine and indicates that the precision is low.
II. Real variables
There are two types of real variables: float and double. The following table lists the memory occupied by these two types and their table examples.
The number of digits occupied by the data type
Float 323.4e-038 ~ 3.4e + 038
Double 641.7e-308 ~ 1.7e + 308
Double type has higher precision and greater scope than single precision float, which is often used.
III. Definitions of real variables, such
Float f; // specify the variable f as the float type.
Double d; // specify the variable d as double.
[Note] unlike C and C ++, Java does not have unsigned integers and specifies the memory bytes occupied by integer and floating point data, this ensures security, robustness, and platform independence.
& Sect; 2.5 TB data
I. Character constants
A character constant is A character enclosed in single quotes, such as 'A' and 'A '.? In addition, similar to C and C ++, Java also provides escape characters starting with a backslash () and converts the character after it into another meaning? Lists escape characters in Java.
Unlike C and C ++, the bytes data in Java is a 16-bit unsigned data, which shows the Unicode set, not just the ASCII set, for example, u0061 indicates 'a' of the ISO Latin code '.
Escape character description
Characters represented by ddd1 to three-digit octal data (ddd)
Uxxxx1 to 4 hexadecimal characters (xxxx)
'Single quote character
\ Backslash character
R press enter
N line feed
F go to the paper form
T horizontal hop
B. Return
II. Balanced variables
The character type is char, which occupies 16 digits in the machine and ranges from 0 ~ 655 35. Variable types are defined as follows:
Charc = 'a'; // specify the variable c as char type, and assign the initial value as 'A'
Unlike C and C ++, the string type data in Java cannot be used as an integer because Java does not support the unsigned integer type. But it can also be operated as an integer.
For example:
Intthree = 3;
Charone = '1 ';
Charfour = (char) (three + one); // four = '4'
In the preceding example, when the addition is calculated, the one variable is converted to an integer, added, and the result is converted to a letter.
3. String constants
Similar to C and C ++, Java's string constants are a string of characters enclosed by double quotation marks (""), such as "thisastring. n ". However, the difference is that the String constants in Java are processed as an object of the String class, rather than a data. We will describe the class String in chapter 7.
& Sect; 2.6 Boolean data
Boolean data has only two values, true and false, and they do not conform to any integer value. It is often used in Stream Control.
. Boolean variables are defined as follows:
Boolean B = true; // defines B as a boolean variable and the initial value is true.
Finally, let's look at a simple Getting started instance.
Class Demo
{
Public static void main (String [] args)
{
System. out. println ("hello world! ");
}
}
Save it to the root directory of the E drive and run it as Demo. java. cd E:
Javac Demo. java, compile; then execute java Deom; note that uppercase is required.