The data type is related to memory usage.

Source: Internet
Author: User
Tags bitwise operators

 

Not afraid of exams

Pcw-chendx@vip.sina.com

Many questions will be prepared for recruitment related to programming. If you are not careful about the questions, the candidates will follow suit. Especially for recent graduates who do not have much work experience, the answers are often too theoretical, it is difficult to satisfy the Examiner. To this end, we have specifically launched this series. By analyzing the real questions, we can make everyone more practical in answering questions, so that the examiner can be satisfied and find a job smoothly.

 

Data TypeMemory usage

Javami Studio Chen yuefeng

The data type is the basis of programming. It will affect the memory usage of the program. If the data type is not used well, it will increase the memory usage. Otherwise, it can reduce the memory usage. Therefore, there must be questions related to data types during the recruitment examination.

Recruitment questions: Write a method, askByteThe absolute value of the type.

Answer:

Public
Byte ABS (byte B ){

If (B
> = 0 ){

Return
B;

} Else {

Return
-B;

}

}

 

Answer B:

Public
Byte ABS (byte B ){

If (B
> = 0 ){

Return
B;

} Else {

Return
(Byte)-B;

}

}

 

Answer C:

Public int ABS (byte B ){

If (B
> = 0 ){

Return
B;

} Else {

Return
-B;

}

}

 

Answer:The answer is C.

 

Why is the answerC

Answer A has a syntax error. For parameter B of the byte type, expression-B is of the int type, which does not conform to the statement that the returned value is of the byte type. Therefore, answer a is wrong. In Java syntax, when arithmetic operations are performed on byte, short, and Char Types, the calculation result is automatically upgraded to int type. Do not ignore this.

Answer B has a logic error. The byte value range is [-128,127]. Because the storage formats of positive numbers and negative numbers are different (storage of the original code and supplemental code format), when determining the absolute value, -The byte value of 128 does not have an absolute value, so B is wrong.

Therefore, with such a simple question, the examiner can examine the applicant's knowledge about the changes in data type computation and gain an intuitive understanding of the applicant's basic knowledge.

Data types are used in almost all practical development. Whether it is software development or website development, using data types can not only make the code written by developers concise, it can also improve the execution efficiency of the program to a certain extent, and the concise code and execution efficiency are one of the most important content in the actual development process.

 

Practical Application

How are data types used in actual development? How can we improve execution efficiency? We use a set of code to describe. In actual data storage and network transmission, the underlying data processing is mostly based on Byte arrays. If you need to store or transmit an int type data, the easy way to think of is to use a string for back-and-forth conversion.

Although String Conversion is easy to use, it is unsatisfactory in both memory usage and execution efficiency. The Int type is composed of four bytes, using this knowledge, you can easily convert int-type values and byte arrays to improve efficiency.

Code 1:

Convert int type to byte array:

Int n = 12345;


Byte [] B = new byte [4];


B [0] = (byte) N;


B [1] = (byte) (N> 8 );


B [2] = (byte) (N> 16 );


B [3] = (byte) (N> 24 );

Comments: The Code has a clear structure and high execution efficiency. during storage, the four bytes of the int type are stored in the byte array B in ascending order, use the forced conversion of shift and byte types to easily split the four bytes of the int type.

 

Convert byte array to int type:

Int M = (B [0]
& 0xff) | (B [1] <8) & 0xff00) |

(B [2] <16) & 0xff0000) | (B [3] <24 )&
0xff000000 );

Comments: The structure and difficulty of the Code are much greater than the preceding conversion. According to the above storage order, B [0] Stores eight bits less, therefore, after the operation is performed with 0xff, only the lower eight bits are retained, and the other bits are cleared. Finally, the content of the four bytes is merged according to the position through the bitwise OR operator. This method improves the code execution efficiency by using the underlying bitwise operators.

 

Code 2:

// Convert int to byte array

Int n = 12345;

String S =
String. valueof (N );

Byte [] B =
S. getbytes ();

// Convert byte array to int

String S1 = new
String (B1 );

Int M =
Integer. parseint (S1 );

Comments:Although Code 2 has fewer lines of code than code 1, it is much worse than code 1 in both program execution efficiency and memory usage. A String object needs to be generated during each conversion, this not only wastes the memory, but also reduces the execution speed of the program.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.