博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转载]细细讲述Java技术开发的规则之开发篇
阅读量:2447 次
发布时间:2019-05-10

本文共 4961 字,大约阅读时间需要 16 分钟。

细细讲述Java技术开发的规则之开发篇
本文介绍的Java规则的说明分为3个主要级别,本篇抛弃了平时开发中很少遇到的情况,那些用得比较少的以后再高级篇里面出现。并有六个有用的国际软件开发重要注意的有关String的问题,遵守了这些规则可以提高程序的效率、
使代码又更好的可读性等。
(1)如果有JDBC连接没有关掉的话,需要在"finally"方法中关掉
如果数据库连接失败或者是没有释放连接,看上去无关紧要。但是其他的用户就需要用更长的时间等待连接,这样数据库利用效率就会下降。确保你的代码在任何情况下,包括出错或者程序异常终止的情况下都释放数据库连接。在"finally"方法中关掉连接,就可以确保这一点。
错误示例:
try {         Statement stmt = con.createStatement();        } catch(SQLException e) 		{            e.printStackTrace();        }
正确示例:
try {         Statement stmt = con.createStatement();        } finally 		{              if (con != null && !con.isClosed())			{                con.close();            }        }
(2)尽量避免使用'Thread.resume ()', 'Thread.stop ()', 'Thread.suspend ()'和 'Runtime.runFinalizersOnExit ()' 方法。
这些方法在平时的开发或者是教科书里面也有用到过,但是这些方法会导致四锁的倾向。一下有充足的资料来说明为什么不建议用上述方法。
(3)在表示长整常量的时候,用L来代替l,因为l很容易和1混一起。
错误示例:
long temp = 23434l;
正确示例:
long temp = 23434L;
(4)最好在jsp开头写一条注释
在jsp文件头上面写一条注释,这样可以帮助别人来理解你的代码。这条规则不仅适用于jsp,更是用于任何开发的文档。
(5)明确的初始化一个构造类里面的所有的字段
因为没有初始化的字段会是一个潜在的bug,所以最好初始化类里面的所有的字段。特别是静态的字段,最好在一开始就分配一个初始值
错误示例:
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;}
正确示例:
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;}
(5)国际化开发建议:逻辑操作符不要再一个单个的字符的前面或者后面
一个单个字符的前后不要用逻辑操作符,如果代码要在一个国家环境中运行的话。我们可以使用字符比较方法,这些方法使用统一字符比较标准来定义字符的属性的。
错误示例:
public class CLO{    public boolean isLetter (char ch) 	{        boolean _isLetter = 		( ch >= 'a' && ch <= 'z')  		//错误            || (ch >= 'A' && ch <= 'Z');        return _isLetter;    }}
正确示例:
public class CLOFixed{    public boolean isLetter (char ch)	{        boolean _isLetter = 		Character.isLetter(ch);          return _isLetter;    }}
(6)国际化开发建议:不要对日期对象使用'Date.toString ()'
不要使用'Date.toString()'方法,日期格式对于地区和语言不同的国家来说是不一样的,务必不要使用。
错误示例:'DateFormat'类提供了一个预定义的格式类型来指定本地的格式。
public void printToday (){        Date today = new Date ();        String todayStr = today.toString ();          System.out.println (todayStr);    }
正确示例:
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);    }
(7)国际化开发建议:不要对数字变量使用'toString ()'方法
在全球化的开发中,不要对数字变量使用'toString()'方法,对于java.lang.Number的任何子类都适用。包括:BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short.对于这样的情况,java里也与定义了"NumberFormat"方法来格式化。
错误示例:
public class NTS {    public void method (Double amount) 	{        String amountStr = amount.toString ();          System.out.println (amountStr);    }}
正确示例:
public class NTSFixed{    public void method (Double amount)	{        Locale currentLocale = 		Locale.getDefault ();        NumberFormat numberFormatter =             NumberFormat.getNumberInstance			(currentLocale);        String amountStr = 		numberFormatter.format (amount);		//         System.out.println (amountStr + '		' + currentLocale.toString ());    }}
(8)国际化开发建议:不要使用'String.equals ()'方法
建议不要使用'String.equals()'方法,因为在统一字符比较标准中不一定按照相关的顺序来比较。'Collator'提供的预定义整理规则来排序string,Collator类调用'getInstance()'方法,一般来说,可以为默认的本地创建一个Collator。
例如:Collator myCollator = Collator.getInstance();创建Collator的时候你也可以指定一个特殊的locale。例如:Collator myFrenchCollator = Collator.getInstance(Locale.FRENCH);然后就可以调用'Collator.compare ()'来执行一个本地的字符比较myCollator.compare(s1,s2);从这里可以了解更多的有关Collator类的信息:http://java.sun.com/docs/books/tutorial/i18n/text/collationintro.html
错误示例:
public class SE {    public boolean compstr 	(String s1, String s2) {        boolean b = (s1.equals (s2));          return b;    }}
正确示例:
public class SEFixed {    public boolean compstr (String s1, String s2) 	{        Collator myCollator =		Collator.getInstance ();        boolean b = 		(myCollator.compare(s1,s2) == 0);          return b;    }}
(9)国际化开发建议:不要使用'StringTokenizer()'方法
错误示例:
StringTokenizer st = new StringTokenizer(str);
(10)国际化开发建议:不要使用'Time.toString ()'方法
因为时间的格式各个国家也不一样。如果你使用日期格式类,你的应用就能够在世界上各个地方正确的显示时间和日期了。首先,用'getTimeInstance()'方法创建一个formatter。然后,调用'format ()'方法。
错误示例:
public class TTS {    public void printTime (Time t1) {        String timeStr = t1.toString ();          System.out.println (timeStr);    }}
正确示例:
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);    }}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-130939/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/374079/viewspace-130939/

你可能感兴趣的文章
xcloud下载_Project xCloud是Microsoft在流Xbox游戏上的赌博
查看>>
gpu驱动程序_如何从错误的GPU驱动程序更新中恢复
查看>>
esp now_Google带回Google Now(内部)排序助手
查看>>
如何防止视频在Chrome中自动播放
查看>>
如何使用Synology NAS下载文件(并避免在夜间开启计算机)
查看>>
使用批处理脚本上传ftp_通过批处理脚本将文件上传到FTP站点
查看>>
pxe网络启动引导程序_如何使用PXE设置网络可引导实用程序光盘
查看>>
凌乱的yyy_如何清理凌乱的Internet Explorer上下文菜单
查看>>
Laravel Eloquent:API资源
查看>>
在React中使用Font Awesome 5
查看>>
React Hooks入门
查看>>
盖茨比乔布斯_用盖茨比快速浏览WordPress站点
查看>>
vue.js表单验证_Vue.js中的模板驱动表单验证
查看>>
软件测试结束标志_使用功能标志进行生产中的测试
查看>>
css网格_在CSS网格中放置,跨度和密度
查看>>
火狐动态调试css_使用Firefox开发工具调试CSS网格
查看>>
服务周期性工作内容_使服务工作者生命周期神秘化
查看>>
nuxt.js 全局 js_在Nuxt.js应用中实现身份验证
查看>>
具有NgClass和NgStyle的Angular 2+类
查看>>
网络抓取_使用ScrapeStack轻松进行网络抓取
查看>>