Java Simple Data Types

        Java is a strongly typed language, unlike C, Java has very strict type checking. For example, in C language, when you print a double type variable as int type, the C language compiler will only report a warning, but it can still pass. Java does not work, it will report an error directly, indicating that the compilation cannot be passed. Next, introduce the simple data types and operators in Java.

When introducing basic (simple) data types, we first introduce two quantities - constants and variables.

1.1, constants

        Constants are constants that remain constant while the program is running. For example, 12 months a year, people have 10 fingers and so on. There is also a concept called literal constants.

int a = 10;//a是常量,10是字面常量
String str = "hello world!";//str是常量,等号右边的量是字面常量。字面常量只能作为右值

Literal constants are:

(1) String constants: enclosed by "", such as "12345", "hello".

(2) Integer constant: the number written directly in the program (note that there is no decimal point), such as: 100.

(3) Floating point constants: decimal numbers directly written in the program, such as: 3.14, 0.49.

(4) Character constant: a character enclosed by single quotation marks, such as 'A', '1'.

(5) Boolean constants: There are only two kinds of true and false.

(6) Empty constant: null.

1.2, Variables

        A variable refers to an amount that can be changed while a program is running . It is equivalent to opening up a memory space to save some data. In C, a variable can be created without initializing it, but in Java it is not allowed. Once you create a new variable without initializing it, Java will report an error directly. And this also shows that Java is a language with high security.

        In Java, 1. A variable name can only contain numbers, letters, and underscores; 2. Numbers cannot start; 3. Variable names are case-sensitive. That is, num and Num are two different variables. Note: Although the Chinese/dollar sign ($) is syntactically allowed to name variables, this is strongly discouraged . The created variables should be named in the form of small camel case, such as max, maxNum, foundMaxNum... When naming, you should know the name. For example, the variable foundMaxNum means to find the largest number.

        Type divides the types of variables, and different types of variables have different characteristics. In Java, according to the number of bytes occupied, the type names from small to large are: byte[byte type](1) <= char[character type](2) <= short(2) <= int( 4) In addition to <= float(4) <= long(8) <= double(8), there are also boolean[Boolean type](size is not specified), String[String type]. In Java, these types are independent of 32-bit and 64-bit operating systems.

1.3, Introduction to basic data types

In java, data types are mainly divided into two categories: basic data types and reference data types.

Let's explain the situation of these basic data types in Java one by one.

Only the assignment in int is given, the others are of type int, and the only thing to be explained is the difference.

1.4, integer variables

1.4.1 Integer variable int

// 方式一:在定义时给出初始值
int a = 10;
System.out.println(a);//10

// 方式二:在定义时没有给初始值,但使用前必须设置初值
int b;
b = 10;
System.out.println(b);//10

It must be initialized or assigned before use, otherwise an error will be reported.

// 使用方式二定义后,在使用前如果没有赋值,则编译期间会报错
int c;
System.out.println(c);
c = 100;

 Range of int:

// int型变量所能表示的范围:
System.Out.println(Integer.MIN_VALUE);
System.Out.println(Integer.MAX_VALUE);

        The above Integer is a wrapper class for int, which is simply an enhanced version of int. Based on why you have it, you can search for java generics if you are interested. Use this method to know the range of int.

 The range is - 2^31 ~ (2^31) - 1.

Precautions:

(1) Int is 4 bytes regardless of the system.

(2) It is recommended to use the first definition. If there is no suitable initial value, it can be set to 0.

(3) When setting the initial value of the variable, the value cannot exceed the representation range of int, otherwise it will cause overflow.

(4) The variable must be assigned an initial value before use, otherwise the compilation will report an error.

(5) The packaging type of int is Integer.

For (3), we enter a number that is not in its range, and the result will be:

        report an error directly. This means that when assigning a value to a variable, Java checks the size of the value. If it exceeds the range, an error will be reported, which shows that java is a very safe language.

1.4.2 long integer variable long

int a = 10;
long b = 10; //long定义的长整型变量,虽然是long,但是编译器认为是int。需要加L。
System.out.println(a);//10
System.out.println(b);//10

long c = 10L; // 为了区分int和long类型,一般建议:long类型变量的初始值之后加L或者l
System.out.println(c);//10

long d = 10l; // 一般更加以加大写L,因为小写l与1不好区分,这里会报一个警告
System.out.println(d);//10

long range:

// long型变量所能表示的范围:这个数据范围远超过 int 的表示范围. 足够绝大部分的工程场景使用.
System.out.println(Long.MIN_VALUE);
System.out.println(Long.MAX_VALUE);

This is a very large range, -2^63 ~ (2^63)-1.

Precautions:

(1) Add L or l after the initial value of the long integer variable, and it is recommended to add L.

(2) Long integers occupy 8 bytes regardless of which system.

(3) The packaging type of long is Long.

1.4.3 short integer short

short a = 10;
System.Out.println(a);//10

The range is:

 is -2^15 ~ (2^15) - 1.

Precautions:

(1) Short occupies 2 bytes under any system.

(2) Be careful not to exceed the range when using (generally less used).

(3) The packaging type of short is Short.

1.4.4 Byte variable byte

byte b = 10;
System.Out.println(b);//10
// byte型变量所能表示的范围:
System.Out.println(Byte.MIN_VALUE);
System.Out.println(Byte.MAX_VALUE);

 This byte is a bit like char in C language, and its size is -128 ~ 127. What about char? Introduced later.

Precautions:

(1) byte occupies 1 byte under any system.

(2) The range of byte is: -128 ~ 127.

(3) The packaging type of bytes is Byte.

1.5, float

1.5.1 Double-precision floating-point double

double d = 3.14;
System.Out.println(d);

Look at code like this:

double num = 1.1;
System.out.println(num * num); // 输出1.21吗?

Precautions:

(1) double occupies 8 bytes under any system.

(2) Floating-point numbers and integers are stored in different ways in memory, and cannot be calculated simply in the form of 2^n.

(3) The packaging type of double is Double.

(4) The memory layout of the double type follows the IEEE 754 standard (same as the C language). Trying to use limited memory space to represent possibly infinite decimals will inevitably lead to certain precision errors. Therefore, floating-point numbers are approximate values, not exact values. Can only say how many digits after the decimal point are accurate.

1.5.2 Single-precision floating point float

float num = 1.0f; // 写作 1.0F 也可以
System.out.println(num);//1.0

If it is written as 1.0, the compiler will think it is a double type and will report an error because the left and right types do not match.

Precautions:

(1) The float type occupies four bytes in Java.

(2) Also comply with the IEEE 754 standard.

(3) Due to the small precision range of the data represented, double is generally preferred when using floating-point numbers in engineering, and the use of float is not recommended.

(4) The packaging type of float is Float.

1.6, character variable char

char a = 'A'; // 大写字母
char b = '1'; // 数字字符
System.out.println(a);//A
System.out.println(b);//1
        
// 注意:java中的字符可以存放整形
char c = '字';
System.out.println(c);//字

Precautions:

(1) The character in Java is essentially an integer. In the C language, the character is represented by the ACSII code, while the character in Java is represented by Unicode. Therefore, a character occupies two bytes, representing more types of characters, including Chinese.

(2) The packaging type of char is Character

(3) The range is 0 ~ 65535, which is completely different from char in C language.

1.7, Boolean variable boolean

Boolean type is used to represent true and false.

boolean b = true;
System.out.println(b);//true
b = false;
System.out.println(b);//false

Precautions:

(1) There are only two values ​​for variables of type boolean, true means true, and false means false. There is no such statement in Java that 0 is true and non-0 is false.

(2) Java's boolean type and int cannot be converted to each other.

(3) In the Java virtual machine specification, it is not clearly specified how many bytes boolean occupies, and there is no bytecode instruction specially used to process boolean. In Oracle's virtual machine implementation, boolean occupies 1 word. Festival.

(4) The packaging type of boolean is Boolean.

1.8, type conversion

        Java is a strongly typed programming language. When variables of different types are assigned to each other, there is a stricter check. In Java, if the type of the operation is inconsistent, type conversion is performed. There are implicit type conversions and casts.

Invisible type conversion generally occurs when the data range is changed from a small data range to a large data range.

System.Out.println(1024); // 整型默认情况下是int
System.Out.println(3.14); // 浮点型默认情况下是double

int a = 100;
long b = 10L;
b = a; // a和b都是整形,a的范围小,b的范围大,当将a赋值给b时,编译器会自动将a提升为long类型,然后赋值
a = b; // 编译报错,long的范围比int范围大,会有数据丢失,不安全

The same goes for other types.

Coercive type conversion occurs when the code needs to process a certain format, which is the conversion of a large data range to a small data range.

int a = 10;
long b = 100L;
b = a; // int-->long,数据范围由小到大,隐式转换
a = (int)b; // long-->int, 数据范围由大到小,需要强转,否则编译失败

Precautions:

(1) Assignment between variables of different digital types means that a type with a smaller range can be implicitly converted to a type with a larger range.

(2) If you need to assign a type with a large range to a small range, you need to cast the type, but the precision may be lost.

(3) When assigning a literal value constant, Java will automatically check the range of numbers.

(4) The forced type conversion may not be successful, and irrelevant types cannot be converted to each other, such as Boolean and other types cannot be converted to each other.

1.9, type promotion

When different data are operated on, the smaller data type will be promoted to the larger data type.

int a = 10;
long b = 10;
int c = a + b; // 编译出错: a + b ==> int + long --> long + long 赋值给int时会丢失数据
long d = a + b; // 编译成功:a + b ==> int + long --> long + long 赋值给long

Now let's take a look at the case of bytes.

byte a = 10;
byte b = 20;
byte c = a + b;
System.out.println(c);//结果是?

 We found that idea reported an error because of incompatible types. The lvalue is of type byte, and the rvalue is of type int.

        When calculating, because the computer usually reads data in units of 4 bytes, for the convenience of hardware calculation, the type with less than 4 bytes will be upgraded to int first during operation, and then calculate. The two byte types of the above equals rvalue are promoted to int, and the result is also int. To achieve such a calculation, it is necessary to perform casts.

        For the reference type string type, its situation is more complicated and needs to be explained specially. When writing questions in Java, the input of data is generally obtained by converting strings to various data types. The following articles are some summaries of Java input when I brush the questions. If you are interested, you can go and have a look.

Input and output of common data types in icon-default.png?t=M1L8Java

For the string type in Java, there are many methods. The following article is a summary of what I am doing. If you are interested, you can go and have a look.

Common Operations of Java Strings_Naion's Blog-CSDN Blog_java String icon-default.png?t=M1L8Operations 

1.10, Summary

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324125635&siteId=291194637