"Effective Java" learning notes-accumulation and motivation

Source: Internet
Author: User

From a practical case.

A week before the National Day holiday, the eldest brother assigned me a bug, that is, the number of printed statements is an integer, and some with small digits, some without, no rules.

 

Based on a short two months of work experience and speculation, the final narrowing of the scope to the following block of code (pseudo code)

String output (double num) {//double-type parameter num is provided by the DAO layer    String result=null;     if(num equals the integer portion of num)// For example 12.0000 equals 12, 13.0001is not equal to            result=(all the decimal digits of Num are deleted, Returns the corresponding string, for example 12.0000 after processing.     Else              Result= (the decimal digit of num is reserved 4 bits, returning the corresponding string, for example 12.0010 after processing returns 12.0010);     return result; // The return value of the string type is output directly to the report }

A double type of argument reminds me that the boss has been stressing to me that you should not use a double type parameter in a stored procedure, because there may be 12.000000001 of such imprecise values, so I think the problem should be here. However, because the local data did not have such a problem, so I can not reproduce the failure. So I will be the above code to make a small change, the problem is solved, update to the customer there is no trouble after the occurrence.

String output (double num) {//double-type parameter num is provided by the DAO layer    String result=null;    num= (num takes four decimal places rounded back, for example 12.00000001 returns12.0000    )if(num equals the integer portion of num)//  For example, 12.0000 equals 12, 13.0001is not equal to            result=(all the decimal digits of NUM are deleted, the corresponding string is returned, for example, 12.0000 is processed    and returned); Else              Result= (the decimal digit of num is reserved 4 bits, returning the corresponding string, for example 12.0010 after processing returns 12.0010);     return result; // The return value of the string type is output directly to the report }

This illustrates the importance of working experience. Without the boss's previous reminders, I'm afraid it's hard to think of the reason for this (especially if the fault cannot be reproduced locally and the error data cannot be tested with breakpoints). Young man, as a novice, good accumulation of experience, later to do the work will become more and more handy. At the same time, if it takes a lot of time to solve a problem, ask your mentor or a colleague who is more experienced than you are asking for help.

After a few days the task is not so nervous idle down, opened to buy a long time of "effective Java", surprised to find that there is such content:

48th: If you need a precise answer, avoid using float and double

The book reads: "Float and doubble types are primarily designed for scientific calculations and engineering calculations." They perform binary floating-point arithmetic (binary floating-point arithmetic), which is designed to provide more accurate, fast approximation calculations over a wide range of values. However, they do not provide completely accurate results, so they should not be used in situations where precise results are required. The float and double types are especially unsuitable for currency calculations, because it is not possible to have a float or double that accurately represent 0.1 (or any other negative number of 10). ”

Suppose we have a five in our pocket and how much is left after we spend Sanmao two minutes? Write a program and try it.



  Work experience needs to accumulate day by day, anxious not to come, even if urgent also no use. However, we can through reading, so can compensate to a certain extent as the new work experience shortcomings.

It's fun to do experiments when you're reading.

(51st) The string join operator (+,string concatenation operator) is a convenient way to combine multiple strings into a single string. To produce a single row of output, or to construct a string to represent a smaller, fixed-size object, it is appropriate to use the Join operator, but it is not suitable for use in large-scale scenarios. It is necessary to use the string join operator repeatedly to concatenate n strings, requiring a square-level time of N. This is an unfortunate result of the immutable character string. When two strings are concatenated together, their contents are copied. Let's look at a piece of code first

1 Private Static String statement () {2     String result = ""; 3      for (int i = 0; i < 20000; i++) 4         Result + = i; 5     return result; 6 }

If the running scenario makes the operation larger, the execution time of this method is difficult to estimate. To ensure performance, the author of the book suggests that we use StringBuilder instead of string to store strings in construction. We can do an experiment to see

1  Packageorg;2 3  Public classTesting {4      Public Static voidMain (string[] args) {5         LongStartmili=system.currenttimemillis ();//the number of milliseconds at which the start time corresponds6 statement ();7         LongEndmili = System.currenttimemillis ();//the number of milliseconds that the end time corresponds8System.out.println ("The time spent in experiment 1 is:" + (Endmili-startmili) + "milliseconds");9         Ten         LongStartmili2=System.currenttimemillis (); One statement2 (); A         LongEndMili2 =System.currenttimemillis (); -System.out.println ("The time spent in experiment 2 is:" + (ENDMILI2-STARTMILI2) + "milliseconds"); -  the     } -  -     Private StaticString statement () { -String result = ""; +          for(inti = 0; I < 20000; i++) -Result + =i; +         returnresult; A     } at      -     Private StaticString Statement2 () { -StringBuilder result =NewStringBuilder (); -          for(inti = 0; I < 20000; i++) - result.append (i); -         returnresult.tostring ();  in     } -  to}

  The result of the operation is as follows (quite astonishing contrast):

The time spent in experiment 1 is: 1627 milliseconds the time spent in Experiment 2 is: 2 milliseconds

Imagine what a pleasure it would be if you had such a problem at work and it happened to be solved by the knowledge you had learned. not only does it save you a lot of time, but it also motivates you to devote more energy to your self-study.

(If you like what I write, then follow my personal public account "little Y".) Have fun and interesting content to share with you every day)

"Effective Java" learning notes-accumulation and motivation

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.