Some experiences and lessons in Java coding

Source: Internet
Author: User
Tags date case statement command line continue variables variable stringbuffer netbeans
Encoding these days to gather time to restore 388 backup file format research, the use of the tools are mainly NetBeans, 010 Editor, Excel. NetBeans is used to test the rules of the analysis, implemented in the Java language; 010 editor is mainly binary or hexadecimal display of the parsed file, the function is strong, mainly in the display and combination aspects; Excel uses formatting to display hexadecimal blocks, displaying corresponding text or values , and you can add annotations, mark unknown areas, leave traces of the next analysis, and prepare. The Java language is far from ripe, and the operation from this application level shows very full, sweaty. Here's a note of some of the mistakes made in Java usage and the places that individuals think they need to focus on are small things, but I think it helps to make less of these low-level mistakes in the future ^_^


The value in byte[].

Reading 512 bytes from a file to byte[] Ba;byte should be 0 to 255, but a negative value occurs when you convert directly to the INT type. This problem is just beginning to make me a mess, later found that is the reason. The problem is solved by int i = ba[1]&0xff;.


Two, string and StringBuffer.

The former is immutable and the latter is variable. Once wanted to assign a value to a variable of type string, hehe, no mistake. Later for the possible changes in the string, the use of StringBuffer. The second point is the string passing in the function, which must be used if the result is to be stringbuffer.


Three, Swtich case statement forget the case of break termination.

This is really killing people. At that time for the format is not determined in the test, is not very sure, reported a pile of errors, had to analyze from the beginning, analysis to think logically there should be no problem, and then look back, hey, the original is to forget the break, the program down the implementation. The original branch of the results are all carried out, good is strange na.


Four, the use of global variables.

If the memory is not too good, the use of global variables must be cautious, that is only a longer function. The program accepts command-line arguments and, if no parameters are specified, gets all the files in the current directory as parameters by default. Because the default file list function is added later, debugging does not succeed me on the dead strength in the new code to find AH find, nothing found. I even found docs carefully consulted the file's description file, and wrote a lot of System.out.println (), to gradually determine the process, and then inadvertently found that the use of args[i]. Hum, there is no command line parameter, the processing time naturally has no obtained, changed to file[] fa;fa[i to calculate solves the problem. The temporary variable is used with the declaration, throw it out, don't do the global design. int FileLen, not as fast and convenient as file.length ().


Five, hashcode and equals.

When you add an object to the collection, the object invokes the method comparison already exists, and the corresponding method is called, and the hashcode and equals are called First and then the latter. Override these methods can define your own objects so that they show the effect of their own vision when compared.


Vi. the way of expression of Unicode.

Direct char c=\uf099; is not valid, the correct way is char c = ' \uf099 ';


Display the hex value of a number.

An int corresponds to a integer,long corresponding to a long. In order to look better, the idea Tohex continue touppercase.


Eight, the continue in the For statement is the meaning of continuing, and while.

Terminate this operation and proceed to the next loop.


Use calendar instead of date when adding and subtract the date, and format the display with DateFormat. The idea of locale is used when formatting.


If you do not want to easily identify E-wen, then do not miss the catch with the System.out.




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.