Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Learn the big data-Java operator from 0 (3)

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

We learn big data technology from scratch, from the foundation of java, to Linux technology, then to the Hadoop, Spark, Storm technology of big data technology, and finally to the construction of big data enterprise platform, step by step, from point to face! I hope Technology Bull can come to guide the study.

In the previous section, we learned about the basic syntax of Java. In this section, we begin to learn the basics of Java-operators, which will focus on the following knowledge points:

Arithmetic operator

Assignment operator

Comparison operator

Logical operator

Bit operator

Ternary operator

PS: before we begin, let's address a few minor issues added in the data types in the previous section.

I. several minor problems added in the data type

1. The problem of byte value

Byte b1x127

Byte b2 = (byte) 128; / /-128

Byte b3 = (byte) 129According to Melissa 127

Byte b3 = (byte) 130; / /-126

Byte ranges from-128to 127,

128: 10000000

-128VR 10000000 (where 1 is both a symbol bit and a numeric bit)

2. Default conversion of data type conversion (following the conversion from small to large)

Byte,short,char-int-long---float---double

Long:8 byte

Float:4 byte

Although it is the default conversion, long is converted to float by default for the following reasons

A, their underlying storage structure is different.

B. The data range represented by float is larger than that of long.

Long: 2 ^ 63-1

Float: 3.4 * 10 ^ 38 > 2 * 10 ^ 38 > 2 * 8 ^ 38 = 2 * 2 ^ 3 ^ 38 = 2 * 2 ^ 114 > 2 ^ 63-1

3. Can the character char in Java language store a Chinese character, and why?

Yes, because characters in the Java language take up two bytes.

The Java language uses Unicode coding, and all characters are encoded according to Unicode

II. Operator

An overview of operators

Operations are also often used in our mathematics (addition, subtraction, multiplication and division, etc.). What are some operations that often need to be done in Java programs? first, take a look at the following definitions of operations and operators:

Operation: the process of operating on constants and variables becomes an operation.

Operators: symbols that operate on constants and variables are called operators

Classification (6): arithmetic operator, assignment operator, comparison operator, logical operator, bit operator, ternary operator

So, we now understand all kinds of operators one by one.

1. Arithmetic operator

Symbol:

+, -, *, /,%, +,-

Note:

A, dividing integers by integers can only get integers. If you want to get decimals, you must transform the data into floating-point numbers.

B, / get the quotient of the division operation, and% get the remainder of the division operation

Let's first write the program code and execute it to see the effect.

Program execution result:

(1) the use of +,-operator

A: for separate use:

The effect of putting it in front of the Operand is the same as after it. (this is quite common.)

B: participate in the operation and use:

Put it in front of the Operand, first increase or subtract 1, and then participate in the operation.

Put it after the Operand, participate in the operation first, and then increase or subtract 1.

C: function: self-increasing or self-decreasing variables.

Program examples:

At the back, the result printed by the program:

On the front, the result printed by the program

It can be seen that what we summed up earlier is correct.

Put it in front of the Operand, first increase or subtract 1, and then participate in the operation.

Put it after the Operand, participate in the operation first, and then increase or subtract 1.

Next, let's take a closer look at the usage of + (there are three main types):

A: addition

B: positive sign

C: the connector of the string

Program examples:

Program execution result:

Visible:

* the operation rules are performed from left to right. "hello" +'a string 1. The system will first recognize that hello is a string, then the plus sign is used as a string concatenator. After concatenating with the character a, a new string is formed, and then concatenated with 1.

* similarly, the string 'hello' is concatenated with the string 'hello.' (the plus sign here is used for addition).

2. Assignment operator

Symbol:

=, +, -, *, /,% =, etc.

It can be divided into the following two categories:

Basic assignment operator: =

Assign the data on the right to the left.

Extended assignment operators: + =,-=, * =, / =,% =

+ = add the left and right, and then assign values to the left

In the same way, the usage of -, *, /,% = refers to the above instructions.

Program examples:

Program execution result:

Thinking questions:

Next, think about two questions.

Short scurvy 1

Short swords 1

Determine if there is a problem with the above two codes, and if so, where is the problem?

Write program code to verify:

The first kind:

Program execution result:

Note: remember, before we learned the short type to do the operation, the default is to convert to the int type, int type occupies 4 bytes, short type occupies 2 bytes, in accordance with the conversion principle from small to large, so this procedure s=s+1 to do the operation will lose progress.

The second kind:

Program execution result:

It can be seen that the extended assignment operator actually implies a cast.

S + = 1; not equivalent to s = sroom1; but equivalent to s = (data type of s) (swatches 1)

Therefore, the execution result of s + = 1; is not reported as an error.

3. Comparison operator

Symbol:

= = equals to

! = means not equal to

> indicates a greater than sign

> = means greater than or equal to

< 表示小于 右移运算 >

Unsigned right shift operation

Note:

To do a bit operation, you must first convert the data to binary

Case study:

Understand the characteristics of the following operator through the following examples:

Let's first analyze the printed results of a few bit operators:

Analysis: because it is a bit operation, we have to convert the data to binary

Binary of 3: 11

00000000 00000000 00000000 00000011

Binary of 4: 100

00000000 00000000 00000000 00000100

& bits and operations: 0 means 0

00000000 00000000 00000000 00000011

& 00000000 00000000 00000000 00000100 ‍

-

00000000 00000000 00000000

The result is: 0

| | bit or operation: if there is one, one is one |

00000000 00000000 00000000 00000011

| | 00000000 00000000 00000000 00000100 |

00000000 00000000 00000000 00000111

The result is: 7 ‍

^ bit XOR operation: same as 0, different as 1

00000000 00000000 00000000 00000011

^ 00000000 00000000 00000000 00000100

-

00000000 00000000 00000000 00000111

The result is: 7 ‍

~ Bitwise inverse operation: 0 to 1, 1 to 0

00000000 00000000 00000000 00000011

-

~ 11111111 11111111 11111111 11111100 (complement) ‍

Because it is worth coming out is the complement of the original value, so we have to further find out that its source code is the final value we want. If you don't know how to operate, you can refer to the basic syntax of Java in the previous section.

Complement: 11111111 11111111 11111100

Inverse code: 11111111 11111111 11111011

Original code: 10000000 0000000 00000100

The result is:-4 ‍

Through the analysis of the executive program code, it can be seen that our analysis is correct:

* in addition, for ^, there are the following characteristics:

One data is XOR to another data bit twice, and the data itself remains unchanged.

Program examples:

The implementation results are as follows:

It can be seen that ^ XOR operation can be used for two numerical exchange operations. It is recommended to write the following code to verify it:

A = a ^ b

B = a ^ b

A = a ^ b

This is left to everyone to do it themselves.

Next, let's take a closer look at the following operations:

Move to the right, the highest bit of positive number is 0, the highest bit of left is 0, the highest bit of negative number is 1, and the highest bit of left is 1

>

For example:

Before performing the operation, let's do the following analysis:

Movement of:

Calculate the binary of 24: 11000

Original code: 10000000 00000000 00000000 00011000

Inverse code: 11111111 11111111 11111111 11100111

Complement: 11111111 11111111 11111111 11101000

1111111111 11111111 11111111 111010 (00)

Move 2 bits to the right, and the highest bit complement 1 to generate a new complement ‍

Then find out the original code in the following way, which is the result we need.

Complement: 1111111111 11111111 11111111 111010

Inverse code: 1111111111 11111111 11111111 111001

Original code: 1000000000 00000000 00000000 000110

The result is-6 ‍

Movement of >

Calculate the binary of 24: 11000

Original code: 10000000 00000000 00000000 00011000

Inverse code: 11111111 11111111 11111111 11100111

Complement: 11111111 11111111 11111111 11101000

0011111111 11111111 11111111 111010 (00)

Move 2 bits to the right, the highest bit complement 0, and generate a new complement ‍.

New complement: 0011111111 11111111 11111111 111010

Because the highest bit of the newly generated complement is 0, it is a positive number, that is, the values of its original code, inverse code, and complement are the same.

So the decimal result for 11111111 11111111 11111111 111010 is 1073741818.

The execution result of the program is:

It can be seen that we can finally summarize the following points:

The data on the left divided by the moving power of 2

> >: move signed to the right. Positive number right shift high position complement 0, negative number right shift high position complement 1

> Whether it is a positive number or a negative number, all the high positions are filled with zero.

For positive numbers, there is no difference between > > and >.

Finally, there is a ternary operator left unmentioned, which we will explain in the next section.

Previous articles:

Start from 0 to learn the basics of big data-Java (1)

Learn big data-Java basic Grammar from 0 (2)

My ability is limited, if there is any deficiency, I hope to correct it.

Thank you for your continued support.

I hope to share it with more people.

Let's learn big data's skills together.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report