Java language with the C language similar to the teaching of strong data processing capabilities, that is, its data types and different types of operations defined more complete, Java internal data types can be divided into four categories: integers, floating-point numbers, characters, Boolean type. The two classes of integers and floating-point numbers can be further divided into specific types of byte, int, short, long, float, and double according to their length and precision. Refer to the following table:
Basic data types for Java
Data type |
Key words |
Number of bits occupied |
Take value range |
Default value |
Boolean type |
Boolean |
8 |
True,false |
False |
Character type |
Byte |
8 |
-128--127 |
0 |
Integer type |
Char |
16 |
0--65535 |
' U0 ' |
Short integral type |
Short |
16 |
-32768--32767 |
0 |
Integral type |
Int |
32 |
-2147483648--2147483647 |
0 |
Long Integral type |
Long |
64 |
-9.22e18--9.22e18 |
0 |
Floating-point type |
Float |
32 |
1.4013e-45--3.4028e+38 |
0.0F |
Double-precision |
Double |
64 |
2.22551e-208--1.7977e+308 |
0.0D |
1. Boolean type
A Boolean can only have true and false two values, representing both "true" and "false" in Boolean logic.
2. Integral type and character type
An integral type is a mathematical integer, and the character type is the character, ' 0 ' and ' a ', etc. Pay attention to two points:
1 when the use of integer variables to pay attention to its maximum and minimum range of values, if the actual value exceeds the range, there will be overflow error. Especially in the factorial of the time to pay attention. If the machine is not very good, but also to save machine resources, only need int type of place, do not use long type.
2 The value of a character variable can be measured by the number of characters constants (a single character in single quotes), or by an integer constant. For example, the following two statements work exactly the same:
char char_A='A';
char char_A=65;
This is a mechanism of the computer problem, it is the character as an integer saved. Because it's more convenient.
3. Floating point Type
float and double are floating-point data types that are used in programs to represent decimals. Because the computer uses floating-point numbers to compute decimals, it is called a floating-point data type. Also because of this reason, the computer calculates the decimal point after the n bit (depending on the specific situation), and the actual value will always have a certain error. It will only go as close to it as possible, so the more the number of digits is approximately accurate. Float is 32, should be sufficient, if not enough, it can only use double, but also to save resources.
The scientific notation in the procedure is expressed. For example: 3.14E3 means 3,140, and 3.14E-2 is 0.0314. If the constant is followed by a ' d ' or ' d ', then it means double. When paying the address, you should write this:
float f1=3.14f;
double f2=3.14d;
4. Type conversion
In a Java program, a constant or variable is converted from one data type to another, called a type conversion. There are two kinds of it, one that is converted from low to high, called default type conversion. For example, converting from a char to an int, from an int to a long type, is a machine that can be executed directly. For example:
int i=5;
long j=i;
If you turn a high position into a low, there will be problems. This reason all understand, how to do? When there is a need, for example, to convert float to int, it is only through coercion type conversion. For example:
float f=3.14f;
int i=(int)f;
long j=5;
int i2=(int)j;
Note, of course, that when a high position is converted to status, the range of data is within the position, for example, a large integer 30000 cannot be converted to a char because it exceeds the range. The result will be an error!
OK, let's 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 ("布尔型:"+b1,2,20);
g.drawString ("整型:"+x,2,40);
g.drawString ("字符型"+c,2,60);
g.drawString ("浮点数据类型:"+f,2,80);
}
}