Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What is the basic data type of java? Why?
What is the basic data type of java? Why?
Java language is a strictly typed language. This means that each variable must have a declared type.

Six numeric types (four integer types and two floating-point types), one character type and one Boolean type.

I. Integer:

Definition: There is no decimal part, and negative numbers are allowed.

Byte 1 byte -2 7 ~ 2 7- 1

Short 2 bytes-215 ~ 215-1

int 4 bytes-2 3 1 ~ 2 3 1- 1

8 bytes long -2 63 ~ 2 63- 1

Second, floating point number:

Definition: including decimal part.

The four words of float are saved as +-3.4347e+38f (6 ~ 7 significant decimals).

Double 8-character is saved as+-1.7991348 6231570e+308 (15 significant digits).

Description:

1) numerical values of float type have suffix: f/F, and if there is no suffix f/F, it defaults to double.

2) When these numbers encounter errors within the range of values, they will overflow; When the image is divided by zero, underflow will occur.

Third, personality type:

Definition: Single quotation marks are used to represent char constants.

Description:

1) Double quotation marks represent a string, which is a Java object, not a data type.

2)char type represents characters in Unicode encoding scheme.

Unicode can contain 65536 characters at the same time, while ASCII/ANSI only contains 255 characters, which is actually a subset of Unicode. Unicode characters are usually represented by

Hexadecimal encoding scheme representation range is between' \u0000' and' \ uFFFF'. \u0000 to \u00FF represent ASCII/ANSI characters. \u indicates that this is a

Unicode values.

3) In Java, you can use escape sequences to represent special characters other than this \u form.

\ bBackspace \u0008

\ tTabulation \u0009

\n newline character

\r Hard Input \u000d

\ "double quotation marks \u0022

\' single quotation marks \u0027

\ backslash \u005c

4) Theoretically, Unicode characters are used in Java applications and applets, but whether they can be displayed really depends on the browser and operating system used, among which the operating system is the most fundamental.

Four, Boolean type:

Boolean types have only two values: false and true.

Data types can be divided into two categories:

1) basic type;

2) Extended types.

In this section, let's learn the basic data types of the Java language. It includes

Value range of type description

Boolean Boolean types have only two values true and false.

character type

Byte 8-bit signed integer-128 and any integer between 127.

Short 16 unsigned integer-any integer between 32768 and 32767.

Int 32-bit signed integer-Any integer from -23 1 to-1.

A 64-bit signed integer-any integer from -263 to-1

Floating-point 32-bit single-precision floating-point number is based on IEEE754- 1985 standard.

Double 64-bit double-precision floating-point number, conforming to IEEE754- 1985 standard.

Some tips:

Different from other programming languages, the integer number of Java language is fixed and will not be different according to different hardware platforms and operating systems.

1 & gt; boolean type

In the logical operators in the last chapter, we have seen variables with true values and false values, which are called Boolean variables.

illustrate

Source program: test601.java.

Public class test60 1

{

Public static void main(string args[])

{

Boolean x = true;

Boolean y = false;

system . out . println(" x & amp; & ampy = "+(x & amp; & ampy));

system . out . println(" x | | y = "+(x | | y));

system.out.println("x^y="+(x^y));

System.out.println("! x="+(! x));

}

}

According to the calculation table of logical operators, X &;; & ampy = true & amp& false = false; X ||| y = true || false = true; x^y=true^false=true; ! x=! True = false.

2> character type

Char type is used to represent letters, and can only represent a single letter. Usually, char constants must be enclosed in single quotation marks to distinguish them from numbers. This is a char.

Examples of variable types:

Char letter =' a

Char type is not commonly used in Java language, because if you want to store characters, you usually use the extended data type string.

3> integer

In the Java language, there are many integer data types: byte, short, int and long. They all define an integer, the only difference is that they can represent the norm of data.

Wai.

The larger the data range that can be represented, the larger the memory space occupied. Therefore, we should choose the most suitable type to define integers in programming.

You can remember the memory space they occupy according to the following rules:

1) int is the most basic, it takes up 32 bits (modern computers are 32-bit machines! );

2) long, which is longer than int and occupies 64 bits;

3) short, that is, shorter than short, occupying 16 bits;

4) byte, byte, 8 bits make up a byte, of course, byte only accounts for 8 bits.

According to the memory space they occupy, the range of numbers they can represent is different. The larger the memory space, the wider the range of numbers that can be represented.

illustrate

Source program: test602.java

Common class test 602

{

Public static void main(String args[])

{

int x = 20

system . out . println(x+5);

system . out . println(x * 7);

}

}

Source program: test603.java

Common class test 603

{

Public static void main(String args[])

{

Byte x =129;

system . out . println(x+5);

}

}

4>& gt floating-point type

Earlier, we learned the variable data type for storing integers. Now, let's look at the variable data type for storing decimals-floating point number. There are two types of floating-point numbers in the Java language: float.

Double.

Among them, float is a single precision type, occupying 32-bit memory space, and double is a double precision type, occupying 64-bit memory space.

Floating point number:

The name floating-point number is relative to a fixed point, which is the decimal point. Floating-point numbers mean that the decimal point can be changed as needed.

Source program: test604.java

Common class test 604

{

Public static void main(String args[])

{

float x 1 = 7;

float y 1 = 9;

Double x2 = 7.0

double y2 = 9.0

system . out . println(x 1/y 1);

system . out . println(x2/y2);

}

}