1、优先返回空集合而非null
如果程序要返回一个不包含任何值的集合,确保返回的是空集合而不是null。这能节省大量的”if else”检查。
2、谨慎操作字符串
如果两个字符串在for循环中使用+操作符进行拼接,那么每次循环都会产生一个新的字符串对象。这不仅浪费内存空间同时还会影响性能。类似的,如果初始化字符串对象,尽量不要使用构造方法,而应该直接初始化。比方说:
//Slower Instantiation
String bad = new String("Yet another string object");
//Faster Instantiation
String good = "Yet another string object"
3、避免无用对象
创建对象是Java中最昂贵的操作之一。因此最好在有需要的时候再进行对象的创建/初始化。如下:
import java.util.ArrayList;
import java.util.List;
public class Employees {
private List Employees;
public List getEmployees() {
//initialize only when required
if(null == Employees) {
Employees = new ArrayList();
}
return Employees;
}
}
4、数组与ArrayList之争
开发人员经常会发现很难在数组和ArrayList间做选择。它们二者互有优劣。如何选择应该视情况而定。
import java.util.ArrayList;
public class arrayVsArrayList {
public static void main(String[] args) {
int[] myArray = new int[6];
myArray[7]= 10; // ArraysOutOfBoundException
//Declaration of ArrayList. Add and Remove of elements is easy.
ArrayList<Integer> myArrayList = new ArrayList<>();
myArrayList.add(1);
myArrayList.add(2);
myArrayList.add(3);
myArrayList.add(4);
myArrayList.add(5);
myArrayList.remove(0);
for(int i = 0; i < myArrayList.size(); i++) {
System.out.println("Element: " + myArrayList.get(i));
}
//Multi-dimensional Array
int[][][] multiArray = new int [3][3][3];
}
}
数组是定长的,而ArrayList是变长的。由于数组长度是固定的,因此在声明数组时就已经分配好内存了。而数组的操作则会更快一些。另一方面,如果我们不知道数据的大小,那么过多的数据便会导致ArrayOutOfBoundException,而少了又会浪费存储空间。
ArrayList在增删元素方面要比数组简单。
数组可以是多维的,但ArrayList只能是一维的。
5、判断奇数
看下这几行代码,看看它们是否能用来准确地判断一个数是奇数?
public boolean oddOrNot(int num) {
return num % 2 == 1;
}
看似是对的,考虑到负奇数的情况,它除以2的结果就不会是1。因此,返回值是false,而这样是不对的。
代码可以修改成这样:
public boolean oddOrNot(int num) {
return (num & 1) != 0;
}
这么写不光是负奇数的问题解决了,并且还是经过充分优化过的。因为算术运算和逻辑运行要比乘除运算更高效,计算的结果也会更快。
6、单引号与双引号的区别
public class Haha {
public static void main(String args[]) {
System.out.print("H" + "a");
System.out.print('H' + 'a');
}
}
看起来这段代码会返回”Haha”,但实际返回的是Ha169。原因就是用了双引号的时候,字符会被当作字符串处理,而如果是单引号的话,字符值会通过一个叫做基础类型拓宽的操作来转换成整型值。然后再将值相加得到169。
7、一些防止内存泄露的小技巧
内存泄露会导致软件的性能降级。由于Java是自动管理内存的,因此开发人员并没有太多办法介入。不过还是有一些方法能够用来防止内存泄露的。
查询完数据后立即释放数据库连接
尽可能使用finally块
释放静态变量中的实例
8、如何计算Java中操作的耗时
在Java中进行操作计时有两个标准的方法:System.currentTimeMillis()和System.nanoTime()。问题就在于,什么情况下该用哪个。从本质上来讲,他们的作用都是一样的,但有以下几点不同:
System.currentTimeMillis()的精度在千分之一秒到千分之15秒之间(取决于系统)而System.nanoTime()则能到纳秒级。
System.currentTimeMillis读操作耗时在数个CPU时钟左右。而System.nanoTime()则需要上百个。
System.currentTimeMillis对应的是绝对时间(1970年1 月1日所经历的毫秒数),而System.nanoTime()则不与任何时间点相关。
Float还是double
-
数据类型 所用字节 有效位数 float 4 7 double 8 15 在对精度要求高的场景下,double类型相对float要更流行一些,理由如下:
大多数处理器在处理float和double上所需的时间都是差不多的。而计算时间一样的前提下,double类型却能提供更高的精度。
9、JSON编码
JSON是数据存储及传输的一种协议。与XML相比,它更易于使用。由于它非常轻量级以及自身的一些特性,现在JSON在网络上已经是越来越流行了。常见的数据结构都可以编码成JSON然后在各个网页间自由地传输。不过在开始编码前,你得先安装一个JSON解析器。在下面的例子中,我们将使用json.simple库来完成这项工作 (https://code.google.com/p/json-simple/)。
下面是编码成JSON串的一个简单的例子。
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
public class JsonEncodeDemo {
public static void main(String[] args) {
JSONObject obj = new JSONObject();
obj.put("Novel Name", "Godaan");
obj.put("Author", "Munshi Premchand");
JSONArray novelDetails = new JSONArray();
novelDetails.add("Language: Hindi");
novelDetails.add("Year of Publication: 1936");
novelDetails.add("Publisher: Lokmanya Press");
obj.put("Novel Details", novelDetails);
System.out.print(obj);
}
}
输出:
{"Novel Name":"Godaan","Novel Details":["Language: Hindi","Year of Publication: 1936","Publisher: Lokmanya Press"],"Author":"Munshi Premchand"}
10、JSON解析
开发人员要想解析JSON串,首先你得知道它的格式。下面例子有助于你来理解这一点:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonParseTest {
private static final String filePath = "//home//user//Documents//jsonDemoFile.json";
public static void main(String[] args) {
try {
// read the json file
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject)jsonParser.parse(reader);
// get a number from the JSON object
Long id = (Long) jsonObject.get("id");
System.out.println("The id is: " + id);
// get a String from the JSON object
String type = (String) jsonObject.get("type");
System.out.println("The type is: " + type);
// get a String from the JSON object
String name = (String) jsonObject.get("name");
System.out.println("The name is: " + name);
// get a number from the JSON object
Double ppu = (Double) jsonObject.get("ppu");
System.out.println("The PPU is: " + ppu);
// get an array from the JSON object
System.out.println("Batters:");
JSONArray batterArray= (JSONArray) jsonObject.get("batters");
Iterator i = batterArray.iterator();
// take each value from the json array separately
while (i.hasNext()) {
JSONObject innerObj = (JSONObject) i.next();
System.out.println("ID "+ innerObj.get("id") +
" type " + innerObj.get("type"));
}
// get an array from the JSON object
System.out.println("Topping:");
JSONArray toppingArray= (JSONArray) jsonObject.get("topping");
Iterator j = toppingArray.iterator();
// take each value from the json array separately
while (j.hasNext()) {
JSONObject innerObj = (JSONObject) j.next();
System.out.println("ID "+ innerObj.get("id") +
" type " + innerObj.get("type"));
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ParseException ex) {
ex.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
}
11、列出目录下的文件
你可以用下面的代码来列出目录下的文件。这个程序会遍历某个目录下的所有子目录及文件,并存储到一个数组里,然后通过遍历数组来列出所有文件。
import java.io.*;
public class ListContents {
public static void main(String[] args) {
File file = new File("//home//user//Documents/");
String[] files = file.list();
System.out.println("Listing contents of " + file.getPath());
for(int i=0 ; i < files.length ; i++)
{
System.out.println(files[i]);
}
}
}
import java.io.*;
public class myIODemo {
public static void main(String args[]) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("//home//user//Documents//InputFile.txt");
out = new FileOutputStream("//home//user//Documents//OutputFile.txt");
int c;
while((c = in.read()) != -1) {
out.write(c);
}
} finally {
if(in != null) {
in.close();
}
if(out != null) {
out.close();
}
}
}
}
13、在Java中执行某个shell命令
Java提供了Runtime类来执行shell命令。由于这些是外部的命令,因此异常处理就显得异常重要。在下面的例子中,我们将通过一个简单的例子来演示一下。我们会在shell命令行中打开一个pdf文件。
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ShellCommandExec {
public static void main(String[] args) {
String gnomeOpenCommand = "gnome-open //home//user//Documents//MyDoc.pdf";
try {
Runtime rt = Runtime.getRuntime();
Process processObj = rt.exec(gnomeOpenCommand);
InputStream stdin = processObj.getErrorStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String myoutput = "";
while ((myoutput=br.readLine()) != null) {
myoutput = myoutput+"\n";
}
System.out.println(myoutput);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
14、导出PDF文件
将表格导出成pdf也是一个比较常见的需求。通过itextpdf,导出pdf也不是什么难事。
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class DrawPdf {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("Employee.pdf"));
document.open();
Paragraph para = new Paragraph("Employee Table");
para.setSpacingAfter(20);
document.add(para);
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("First Name"));
table.addCell(cell);
table.addCell("Last Name");
table.addCell("Gender");
table.addCell("Ram");
table.addCell("Kumar");
table.addCell("Male");
table.addCell("Lakshmi");
table.addCell("Devi");
table.addCell("Female");
document.add(table);
document.close();
}
}
15、邮件发送
在Java中发送邮件也很简单。你只需装一下Java Mail这个jar包,放到你的类路径里即可。在下面的代码中,我们设置了几个基础属性,然后便可以发送邮件了:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail
{
public static void main(String [] args)
{
String to = "recipient@gmail.com";
String from = "sender@gmail.com";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("My Email Subject");
message.setText("My Message Body");
Transport.send(message);
System.out.println("Sent successfully!");
}
catch (MessagingException ex) {
ex.printStackTrace();
}
}
}