Niuke.com Java Wrong Question Summary (8)

table of Contents

One, byte data storage

Second, the yield() method

Three, ternary operation operator

Four, processing flow


 

One, byte data storage

Analysis:

This question examines the conversion of data types:

129 is represented by int type as 4 bytes, and its binary system is: 00000000 00000000 00000000 10000001

In a computer system, with all values complement expressed (stored), the same number of positive and complement the original code .

  1. Int is converted to byte (1 byte), interception:  10000001
  2. Complement code to one's complement code, complement -1 is one's complement code:  10000000
  3. The inverse code changes to the original code, the sign bit remains unchanged, and the other bits are inverted: 11111111
  4. The decimal number corresponding to the original code of 11111111 is -127

 

Second, the yield() method

Analysis:

The function of the Thread.yield() method is to suspend the currently executing thread object and execute other threads.

What yield() should do is to return the currently running thread to a runnable state to allow other threads with the same priority to get a chance to run.

Therefore, the purpose of using yield() is to allow threads of the same priority to rotate appropriately. However, in practice, there is no guarantee that yield() will achieve the concession purpose, because the concession thread may be selected again by the thread scheduler.

Conclusion: yield() never causes the thread to go to the waiting/sleeping/blocking state. In most cases, yield() will cause the thread to transition from the running state to the runnable state, but it may have no effect.

Because there are no other threads, the following statement is executed after yield

 

Three, ternary operation operator

Analysis:

The ternary operator performs automatic type promotion on the data types of the two results.

Therefore, you can put

Object o1 = true ? new Integer(1) : new Double(2.0);

See

Object o1 = true ? new Double(1.0) : new Double(2.0);

 

The conversion rules for ternary operator types are as follows:

  1. If the two operands are not convertible, no conversion is performed, and the return value is Object type.
  2. If the two operands are expressions of clear types (such as variables), they are converted according to normal binary numbers, int type is converted to long type, and long type is converted to float type, etc.
  3. If one of the two operands is a number S, the other is an expression, and its type is marked as T, then if the number S is in the range of T, it is converted to type T; if S exceeds the range of type T , Then T is converted to S type.
  4. If both operands are Literal, the return value type is the larger one.

 

Four, processing flow

Analysis:

According to whether the stream is directly connected to a specific place (such as disk, memory, equipment, etc.), it is divided into two types: node stream and processing stream.

  • Node flow : You can read and write data from or to a specific place (node). Such as FileReader
  • Processing flow : It is the connection and encapsulation of an existing flow, and data reading and writing are realized through the function call of the encapsulated flow. Such as BufferedReader
    • The construction method of processing the stream always takes an other stream object as a parameter. A stream object is packaged multiple times by other streams, which is called a stream link.

JAVA commonly used node flow:

  • Files: FileInputStream, FileOutputStream, FileReader, FileWriter, node streams for file processing.
  • String: StringReader, StringWriter, a node stream for processing strings.
  • Array: ByteArrayInputStream, ByteArrayOutputStream, CharArrayReader, CharArrayWriter, the node stream that processes the array (the corresponding is no longer a file, but an array in memory).
  • Pipeline: PipedInputStream, PipedOutputStream, PipedReaderPipedWriter, the node stream that processes the pipeline.

Common processing flow (use closed node flow when closing processing flow)

  • Buffered stream: BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter, increase the buffering function to avoid frequent reading and writing of the hard disk.
  • Conversion stream: InputStreamReader, OutputStreamReader, realize the conversion between byte stream and character stream.
  • Data stream: DataInputStream, DataOutputStream, etc., provide basic data types to be written to a file, or read out.

Close sequence of the stream

  1. In general, it is: open first and then close, and open second and close first
  2. Another case: looking at the dependency relationship, if stream a depends on stream b, you should close stream a first, and then close stream b. For example, if processing flow a depends on node flow b, processing flow a should be closed first, and then node flow b should be closed.
  3. You can only close the processing flow without closing the node flow. When the processing flow is closed, the closing method of the node flow it is processing is called.

Guess you like

Origin blog.csdn.net/weixin_39478524/article/details/115103782