1. Accueil
  2. Automotive News
  3. 8, data type in Java

8, data type in Java

Le monde automobile 13/08/2021 53

Data types in Java

Author: Han Ru

Company: Programming (Beijing) Technology Co., Ltd.

Program coffee: IT vocational skill evaluation platform

Website: https://www.chengxuka.com

8, data type in Java

task

1. Classification of data type 2. What are the basic data types 3. The memory space occupied by each of the basic data types and the value range 4. Conversion between data types

Java language is a strong type language: the requirements for data types are very strict. The declaration is what type of variable is to store what type of value.

First, the classification of data types

Java data types can be divided into two categories: basic data types and reference types

Basic Data Type: Byte, Short, Int, Long, Float, Double, Char, Boolean

Quote data type: class, interface, array, enumeration (back explanation)

This section, let's first explore the basic data type.

8, data type in Java

2. Basic data types 2.1 Basic data types

Basic data types are also called raw data types. In Java, there are eight raw data types: Boolean, character, byte, short, integer, long, floating point, and double precision. The basic data type has the following 4 classes:

Integer ---- Byte (Byte), Short (Short Integer), INT (Interest), Long (Long Integer)

Used to store an integer.

Floating-point ---- Float (single precision floating point), Double (double precision floating point)

Used to store decimals.

Characteristic ---- charr

For storage characters

Boolean ---- Boolean

Used to store True and false, indicating right and wrong.

2.2 Storage of basic data types

All basic data types in Java have a fixed storage range and the size of the memory space, instead of being affected by the specific operating system to ensure portability of the Java program.

2.2.1 Integer type

8, data type in Java

Byte: It can accommodate the integer between -128 to 127. Usually used to save memory, and you can determine that the numbers are within the limit specified by the byte data type.

public class Demo04datatype{ public static void main(String[] args) {//Define a variable of byte type byte num = 10; System.out.println(num); //If the storage size exceeds the byte Value range, it will report an error byte num2 = 129; System.out.println(num2); }}

When defining the variable, it is not possible to exceed the value range of the type of data type:

8, data type in Java

Short: Short integer, in size, it is greater than byte and is less than an integer. The range is -32768 to 32767.

INT: When Short is not enough to accommodate numbers, its range value is larger, and the int type is also the most intellectual type of us usually usage. It is also the default type in an integer type.

Long: When the size is not enough to accommodate this value, its range is wider than the INT data type, range from -9, 223, 372, 036, 854, 775, 808 to 9, 223, 372, 036 , 854, 775, 807.

To declare that the LONG type can add "L" or "L", lowercase L or uppercase L.

public class Demo04datatype{ public static void main(String[] args) {//1. Define a variable of byte type byte num = 10; System.out.println(num); //If the storage size exceeds byte The value range of, it will report an error //byte num2 = 129; //System.out.println(num2); //2. Short type variable, short s = 129;//Value range: -32768~ 32767, a total of (65536 values) System.out.println(s); //3. Variables of type int int i = 1314;//Value range: -2147483648~2147483647 System.out.println(i); / /4. Long type variable long l1 = 9223372036854775807L;//Add L to indicate that the value is long type System.out.println(l1); int age = 19; long l2 = 100000000L; ))

operation result:

8, data type in Java

At last:

The integer value of the Java language is INT type, such as int i = 3; to declare that the long type can add'L' or'L', such as: long L = 3L; Note: When the value exceeds the expression of int, 2147483647) Need to add L / L to LONG assignment

2.2.2 Floating point type

The data stored in floating-point type is what we usually say. With accuracy.

8, data type in Java

Float: Single-precision floating point (accuracy 6, 7) 4 bytes

Double: Double precision floating point (accuracy 15, 16) 8 bytes

Note: Java floating-point constants defaults to Double types, if you want to declare a FLOAT type, you need to add f or f, such as: double D = 3.14; float f = 3.14f;

Java floating-point constants have two representations, decimal numbers, and must contain a decimal point, for example: 3.14 314.0 0.314 scientific notation, such as 3.14e2 3.14E2 314E2 Note: The data stored in float double are approximate values.

Sample code:

public class Demo05FloatAndDouble {public static void main(String[] args) {//1. Single precision floating point number: float float f = 3.14f; System.out.println(f); //2. Double precision float Points: double double d = 1.23;//Decimal in java, the default type is double System.out.println(d); //Scientific notation double d2 = 2000;//int, which implies automatic conversion of data types System.out.println(d2); double d3 = 2000.0;//double System.out.println(d3); double d4 = 2E3;//equivalent to the 3rd power of 2*10 System.out.println(d4) ; //1.7E308, which is equivalent to 1.7 times 10 to the power of 308. There are 307 0s after 17 double d5 = 17000000000000000000000000.0;//double System.out.println(d5); //1. Floating point type: IEEE754 standard. }}

operation result:

8, data type in Java

Stepping on the pit: Industrial standard IEEE754 in the computer, so decimal addition or subtraction operation, is the approximate number of precision, not necessarily the same as you spendly calculated.

2.2.3 Character type

CHAR type data is used to represent the "character" in the usual sense. Represents a single character, use single quotes, but essentially a value because the corresponding character encoding value. Java characters use Unicode encoding, each character accounts for two bytes, so it can be expressed in the form of hexadecimal coding (Unicode is a global language unified).

You can directly assign a value through a single character.

CHAR C1 ='a';

It can also be assigned to a Chinese character (Chinese characters are also a character, Chinese characters)

CHAR C2 ='in';

You can also assign a value via coding

CHAR C3 = 97; // ASCII US standard information exchange code, equivalent to'a'

You can also assign values ​​through 16-based coding values.

CHAR C4 ='\ u0061'; // is equivalent to'a'

ASCII Code Table:

8, data type in Java

Sample code:

public class Demo06Char {public static void main(String[] args) {//Character type: char, enclosed in single quotes char c ='A'; System.out.println(c); //Use encoding The value is stored char c2 = 65; //65 is the coded value of character A S

operation result:

8, data type in Java

Side character: Java language also allows the use of escape characters to transition thereafter to other meaning.

Escape characters-->Use \1 in the syntax, convert the characters with special effects into ordinary characters, no longer work',",\ 2. You can also convert some ordinary characters To have a special meaning\n,\t

8, data type in Java

Sample code:

public class Demo07TranserSign {public static void main(String[] args) {/* Escape character: escape the character --> Syntactically use \ 1. Convert the character with special function to ordinary Characters no longer work \',\",\\ 2. You can also convert some ordinary characters into special meanings\n,\t */ char c ='\'';//' System. out.println(c); char c2 ='"';//" System.out.println(c2); System.out.println("Hello\nWor\tld"); System.out.println("D: \\Ruby\\Program Coffee");//Path System.out.println("\"HelloWorld\"");//Print helloworld with double quotes }}

operation result:

8, data type in Java  2.2.4 Boolean type

The Boolean type is suitable for logical operations, generally used for program flow control Boolean type data only allows TRUE or FALSE, and does not replace True and false using 0 or non-0 integers.

Sample code:

public class Demo08Boolean {public static void main(String[] args) {//Define a variable of type boolean//This type has only 2 values: true, false boolean b1 = true; boolean b2 = false; / /"Expression" is a formula, a numeric value and some operators // 3 + 5 -->8 boolean b3 = 3 > 5;//3 > The calculation result of 5: false //when When assigning a value to a boolean type variable, // can be directly assigned: true, false // can also be assigned to an expression: the calculation result of the expression is true or false System.out.println(b1); System.out. println(b2); System.out.println(b3);//false }}

operation result:

8, data type in Java  2.2.5 Summary of Basic Data Types

8, data type in Java  Three, basic data type conversion

Java language is a strong type language, defined variable data types and assignment data types need to be unified. However, the program is allowed to do a certain degree of data type conversion. Divided into the following:

Automatic type conversion

Mandatory type conversion

Note: The Boolean type cannot be converted with other types.

8, data type in Java  3.1 Automatic type conversion

Automatic type conversion: A type with a small capacity is automatically converted to a data type with a large capacity. The capacity here refers to the value range of the data type.

Automatic type conversion: the data type is converted from a small value range to a large value range 1.byte ---> short--->int--->long--->float - ->double 2.char---> int 3.byte, short, and char are converted to int type during calculation (understand) 4.int integer constants and character constants can be assigned directly within a reasonable range byte, short, int, char (understand)

Sample code:

public class Demo09Convert {public static void main(String[] args) {//1. Automatic type conversion: automatically convert a small value range to a large value range. short s1 = 123;// int i1 = s1;// implicitly convert the value of s1 from short type to int, and then assign it to the variable i1 System.out.println(s1); System.out.println(i1 ); byte b1 = 10; int i2 = b1;//byte-->int System.out.println(i2); }}

operation result:

8, data type in Java  3.2 Mandatory type conversion

Mandatory type conversion: Types with large capacity are forcibly converted into data types with small capacity. It may cause a decrease in accuracy or data overflow, so be careful when using it.

Mandatory type conversion: the data type with a large value range is converted to a small value range. long l = 100L; int i = (int)l; However, it may cause precision reduction or data overflow, so be careful when using it.

Forced converter is required: Just before the value to be converted, the data type to be converted using parentheses is used.

(Type to be converted) To convert data

Sample code:

public class Demo09Convert {public static void main(String[] args) {//1. Automatic type conversion: automatically convert a small value range to a large value range. short s1 = 123;// int i1 = s1;// implicitly convert the value of s1 from short type to int, and then assign it to the variable i1 System.out.println(s1); System.out.println(i1 ); byte b1 = 10; int i2 = b1;//byte-->int System.out.println(i2); //2.Mandatory type conversion //byte range: -128~127 byte b2 = (byte)s1;//Force s1 from short type to byte type, and then assign it to byte type variable b2 System.out.println(b2);//123 short s2 = 128; byte b3 = (byte)s2;//-128 System.out.println(b3); double d1 = 2.5; int i3 = (int)d1;//Forced conversion: floating point to integer: directly intercept the integer part, ignoring the decimal System. out.println(i3); long l1 = 100L; float f1 = l1; System.out.println(f1); }}

operation result:

8, data type in Java

At last:

Automatic conversion: A: Two types are compatible B: The value range of the target type is greater than the value range of the source type Coerced conversion: A: Both types are compatible B: The value range of the target type is smaller than the value range of the source type Need to strengthen the grammar of the conversion character: specify which type (the type to be converted) to be forced to be converted into. Value range: byte-->short-->int-->long-->float-- >double char


PREV: Leasing: which car insurance to choose?

NEXT: What are the advantages and disadvantages of the car registration document for my car? For that, you just have to do ...

Articles populaires

Articles chauds

Listes de navigation

Retour au sommet