Getting started with Java Applet
YY435
Pacific Network Academy
the next day
2. Data type
The Java language has similar data processing capabilities similar to the C language, that is, its data type and different types of operations define more complete. The data type inside Java can be divided into four categories: integer, floating point, character, Boolean. Two major categories of integers and floating point are different from the length and precision, and several specific types of Byte, int, short, long, float, and double can be further divided. Specifically, see the following table:
Java basic data type
Data type keyword
Occupy number of bits
Value range default value Boolerance Boolean
8
True, Falsefalse character type byte
8
-128-270 integer char
16
0--65535 '/ u0' Short Singer SHORT
16
-32768-327670 integer int
32
--2147483648 --21474836470 Long integer Long
64
-9.22e18--9.22E180 floating-point Float
32
1.4013E-45-3.4028E 380.0F double precision Double Double
64
2.22551E-208--1.7977E 3080.0D
1. Boolean Boolean types can only have two values of True and False, represent "true" and "false" in Boolean logic, respectively.
2. Integer and characters integers are integers in mathematics, and the character type is character, '0' and 'a', and the characters are all characters. Pay attention to two points: 1) Pay attention to its maximum and minimum value range when using an integer type variable, and an overflow error occurs if the actual value exceeds the range. Especially when you are doing step by multiplication. If the machine is not very good, save the machine resources, just an int type place, do not use a long type. 2) The value of the character type variable can use the character constant (single character with single quotes) or an integer constant. For example, the following two statements have the same effect: char char_a = 'a'; char char_a = 65;
This is a mechanism problem of the computer, which is saved as an integer type. Because it is more convenient.
3. Floating-Point Float and Double are all floating point data types, and they use them to represent decimals. Since the computer uses floating point to calculate the decimal, it is called a floating point data type. Also because this reason, the N bit after the computer calculated, and the specific case may always have a certain error with the actual value. It will only be close to it, so the more the number of bits is more accurate. Float is 32-bit, should be enough, if not enough, then you can only use Double, but also save resources.
The scientific notes in the program are represented. For example: 3.14e3 representation 3, 140, and 3.14e-2 represents 0.0314. If there is a 'd' or 'd' behind the constant, then it is Double type. When you pay, you should write: float f1 = 3.14f; double f2 = 3.14d;
4. In the Type Conversion Java program, constant or variables are converted from one data type to another data type called a type conversion. It has two kinds, one is called the default type conversion from the low level to the high position. For example, from the CHAR type to the INT type, switch from the INT type to the long type, both the machine can be executed directly. For example: INT i = 5; long j = i;
If the high level is converted to a low position, there will be problems. This truth understands, what should I do? When there is a need, for example, put the FLOAT to the INT type, then only transform the mandatory type. For example: float f = 3.14F; INT i = (int) f; long j = 5; INT i2 = (int) j; For example, a large integer 30000 cannot be converted to a CHAR type because it exceeds the scope. The result will have an error!
Ok, look at an example:
Import java.awt. *;
IMPORT JAVA.Applet.Applet;
Public class leiXing extends applet
{
Boolean B1 = True;
INT x = 10;
CHAR C = 65;
Float f = 3.14f;
Public void Paint (Graphics G)
{
g.drawstring ("Boolean:" B1, 2, 20);
g.drawstring ("Integer:" X, 2, 40);
g.drawstring ("character type" C, 2, 60);
g.drawstring ("Floating Point Data Type:" F, 2, 80);
}
}