Java rules--development article

Source: Internet
Author: User
Tags date format variables string stmt thread time and date tostring
The Java rules described in this article are divided into 3 main levels, this article abandoned the peacetime development of the situation rarely encountered, those who use less later in the high-level chapter appears. And there are six useful international software development issues that are important to note about string, following these rules can improve the efficiency of the program, make the code more readable, and so on.
(1) If a JDBC connection is not turned off, it needs to be turned off in the "finally" method
It does not seem to matter if the database connection fails or if the connection is not released. But other users will need to wait longer to connect, so the database utilization efficiency will be reduced. Make sure your code releases the database connection in any case, including an error or an unexpected termination of the program. You can ensure this by turning off the connection in the "Finally" method.
Error Example:
try {
Statement stmt = Con.createstatement ();
catch (SQLException e) {
E.printstacktrace ();
}
Correct example:
try {
Statement stmt = Con.createstatement ();
finally {
if (Con!= null &&!con.isclosed ()) {
Con.close ();
}
}

(2) Try to avoid the use of ' thread.resume () ', ' thread.stop () ', ' Thread.Suspend () ' and ' runtime.runfinalizersonexit () ' method.
These methods are also useful in the development of the usual or in textbooks, but these methods can lead to a tendency of four locks. There is sufficient information to explain why the above method is not recommended.
Reference: 1. " Java.lang.Thread "in the JDK API documentation
2.http://java.sun.com/j2se/1.3/docs/guide/misc/threadprimitivedeprecation.html
3.Paul Hyde: "Java Thread Programming"
Sams, Isbn:0-672-31585-8 pp. 270

(3) When representing a long integer constant, L is substituted for L.
Because L can easily mix with 1.
Error Example:
Long temp = 23434l;
Correct example:
Long temp = 23434L;
Reference: Ken Arnold, James Gosling: "The Java programming Language Second Edition" Addison Wesley, 1997, pp.108

(4) It is best to write a note at the beginning of the JSP
Write a comment on the header of the JSP file to help others understand your code. This rule applies not only to JSP, but to any development document.
Correct example: <%--JSP Comment--%>


(5) Explicit initialization of all fields within a constructed class
Because a field that is not initialized is a potential bug, it is best to initialize all the fields within the class. Especially static fields, it's best to assign an initial value at the beginning
Error Example:
public class CSI {
Public CSI () {
This (12);
k = 0;
}

Public CSI (int val) {
j = Val;
}

private int i = 5;
private int J;
private int k;
}

Correct example:
public class Csifixed {
Public csifixed () {
This (12);
}

Public csifixed (int val) {
j = Val;
k = 0;
}

private int i = 5;
private int J;
private int k;
}
Reference: Http://www.ambysoft.com/javaCodingStandards.pdf

(5) International development suggestion: logical operators do not precede or follow a single character
Do not use logical operators before and after a single character, if the code is to run in a country environment. We can use character comparison methods, which use the uniform character comparison standard to define the attributes of a character.
Error Example: public class CLO {
public boolean isletter (char ch) {
Boolean _isletter = (ch >= ' a ' && ch <= ' z ')//Error
|| (Ch >= ' A ' && ch <= ' Z ');
return _isletter;
}
}

Correct Example:
public class Clofixed {
public boolean isletter (char ch) {
Boolean _isletter = Character.isletter (CH);
return _isletter;
}
}
Reference: http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
More character comparison methods please refer to: http:// java.sun.com/docs/books/tutorial/i18n/text/charintro.html


(6) Internationalization Development recommendation: Do not use ' date.tostring () '
Do not use ' date.tostring () ' method for date objects, the date format is not the same for countries with different locales and languages, and must not be used.
Error Example: The ' DateFormat ' class provides a predefined format type to specify a local format.
public void Printtoday () {
Date today = new Date ();
String todaystr = today.tostring ();
System.out.println (TODAYSTR);
}
Correct example:
public void Printtoday () {
Locale Currentlocale = Locale.getdefault ();
DateFormat dateformatter = dateformat.getdateinstance (
Dateformat.default, currentlocale);
Date today = new Date ();
String todaystr = Dateformatter.format (today);
System.out.println (TODAYSTR);
}
Reference: http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
http://java.sun.com/docs/ books/tutorial/i18n/format/dateformat.html

(7) Internationalization Development recommendation: Do not use the ' ToString () ' method for numeric variables in global development, do not use the ' ToString () ' method for numeric variables and apply to any subclass of Java.lang.Number. Includes: BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and short. In this case, Java is also formatted with the "NumberFormat" method defined.
Error Example:
public class NTS {
public void method (Double amount) {
String amountstr = amount.tostring ();
System.out.println (AMOUNTSTR);
}
}
Correct example:
public class Ntsfixed {
public void method (Double amount) {
Locale Currentlocale = locale.g Etdefault ();
NumberFormat numberformatter =
Numberformat.getnumberinstance (currentlocale);
String amountstr = Numberformatter.format (amount);
System.out.println (amountstr + ' + currentlocale.tostring ());
}
}
Reference: http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
Http://java.sun.com/docs /books/tutorial/i18n/format/numberformat.html


(8) Internationalization development suggestion: Do not use ' string.equals () ' method
The ' string.equals () ' method is not recommended because it is not necessarily in the relevant order to compare in the uniform character comparison standard. The predefined collation rules provided by ' collator ' to sort the String,collator class invoke the ' getinstance () ' method, in general, to create a collator for the default local. For example: collator mycollator = Collator.getinstance (); You can also specify a special locale when creating collator. For example: collator myfrenchcollator = collator.getinstance (locale.french); then you can call ' Collator.compare () ' To perform a local character comparison mycollator.compare (S1,S2); From here you can learn more about Collator classes: http://java.sun.com/docs/books/tutorial/i18n/ Text/collationintro.html

Error Example:
public class SE {
public boolean compstr (string s1, string s2) {
Boolean B = (s1.equals (s2));
return b;
}
}
Correct example:
public class Sefixed {
public boolean compstr (string s1, string s2) {
Collator mycollator = Collator.getinstance ();
Boolean B = (Mycollator.compare (s1,s2) = = 0);
return b;
}
}

Reference: http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
Http://java.sun.com/docs/books/tutorial/i18n/text/locale.html

(9) Internationalization development suggestion: Do not use ' StringTokenizer () ' method
Example of an error: StringTokenizer st = new StringTokenizer (str);
You can get more information from here: '
Reference: http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html


(10) Internationalization development suggestion: Do not use ' time.tostring () ' method
Because of the format of time different countries are also different. If you use a date format class, your application will be able to display the time and date correctly in all parts of the world. First, create a formatter using the ' gettimeinstance () ' method. Then, call the ' format () ' method.
Error Example:
public class TTS {
public void Printtime (Time t1) {
String timestr = t1.tostring ();
System.out.println (TIMESTR);
}
}
Correct example:
Import Java.sql.Time;
Import Java.text.DateFormat;
Import Java.util.Locale;

public class Ttsfixed {
public void Printtime (Time t1) {
DateFormat Timeformatter = dateformat.gettimeinstance (
Dateformat.default, Locale.getdefault ());
String timestr = Timeformatter.format (t1);
System.out.println (TIMESTR);
}
}




Related Article

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.