有兩種類型的開發(fā)人員,優(yōu)秀的開發(fā)人員和聰明的開發(fā)人員,你是哪一種?掌握一些技巧能讓我們變成一個聰明的開發(fā)人員。

1.main()函數的不同寫法
從使用 Java 的第一天開始,我們應該就會這么寫:
public class Example {
public static void main(String [ ] args){
System.out.println(“Hello World”);
}
}
那么你知道 main() 方法的不同寫法嗎?
方式一
public class Example {
public static void main(String . . . args){
System.out.println(“Hello World”);
}
}
方式二
public class Example {
static public void main(String . . . args){
System.out.println(“Hello World”);
}
}
看出什么差別了嗎?
2.數值交換
數值交換的典型寫法:
class Example {
public static void main(String a[]) {
int x = 10;
int y = 5;
x = x + y;
y = x - y;
x = x - y;
System.out.println("交換后的值為:"+ " x = " + x + ", y = " + y);
}
}
聰明的寫法:
class Example {
public static void main(String a[]) {
int x = 10;
int y = 5;
y= x+y - (x=y);
System.out.println("After swaping:"+ " x = " + x + ", y = " + y)
}
}
3.沒有main()函數的Java代碼
{
static
{
System.out.println("Hello World!");
System.exit(0); // 防止“main method not found” 錯誤
}
}
4.測量程序運行時間
currentTimeMillis():返回自大紀元以來以毫秒為單位的當前時間,以長為單位。
long startTime = System.currentTimeMillis();
long estimatedTime = System.currentTimeMillis() - startTime;
nanoTime():返回最精確的可用系統定時器的當前值,以納秒為單位,以 long 為單位。nanoTime() 用于測量相對時間間隔而不是提供絕對時間。
long startTime = System.nanoTime();
long estimatedTime = System.nanoTime() - startTime;
5.檢查奇數
常規(guī)寫法:
public boolean oddOrNot(int num) {
return num % 2 == 1;
}
考慮到負奇數,除以 2 的余數不會是 1。因此,返回的結果將是 false,這是不正確的!
聰明的寫法:
public boolean oddOrNot(int num) {
return (num & 1) != 0;
}
使用這段代碼,不僅解決了負奇數的問題,而且這段代碼也進行了高度優(yōu)化。由于與除法和乘法相比,算術和邏輯運算要快得多,因此在第二個片段中可以更快地獲得結果。
6.在 Java 中搜索字符串
這可能不是所謂的技巧,但它是 Java 開發(fā)人員應該知道的東西。Java 提供了一個 indexOf() 的庫方法。此方法與 String 對象一起使用,返回所需字符串的索引位置。如果未找到該字符串,則返回 -1。
public class Example {
public static void main(String[] args) {
String myString = "Hello String!";
if(myString.indexOf("String") == -1) {
System.out.println("String not Found!");
} else {
System.out.println("String found at: " + myString.indexOf("String"));
}
}
}
7.明智的判斷
當我們做判斷時,首先想到的是 If-else。這是在 Java 中做判斷的最簡單方法。如下:
class Example{
public static void main(String [ ] main){
int n1 = 5;
int n2 = 10;
if(n1>n2){
System.out.println(“n1 is greater”);
} else{
System.out.println(“n2 is greater”);
}
}
}
使用三木運算更加簡潔:
class Ternary {
public static void main(String[] args)
{
int n1 = 5;
int n2 = 10;
int max = (n1 > n2) ? n1 : n2;
System.out.println("Maximum is = " + max);
}
}
8.使用堆棧跟蹤
捕捉錯誤可能是 Java 開發(fā)過程中最耗時的部分。堆棧跟蹤能準確跟蹤項目拋出異常的位置。
import java.io.*;
Exception e = …;
java.io.StringWriter sw = new java.io.StringWriter();
e.printStackTrace(new java.io.PrintWriter(sw));
String trace = sw.getBuffer().toString();
9.使用 System.in.read() 進行用戶輸入
用戶輸入是任何應用程序的重要方面之一。Java 常見的方式為:
- 使用 Scanner 類
- 使用 BufferedReader 類
更簡潔的方法:
import java.io.IOException;
class ExamTest{
public static void main(String args[]) throws IOException{
int sn=System.in.read();
System.out.println(sn);
}
}
10.小心使用字符串
如果在“for”循環(huán)中使用“+”運算符連接兩個字符串,那么它每次都會創(chuàng)建一個新的字符串對象。這會導致內存浪費并增加執(zhí)行時間。此外,在實例化字符串對象時,應避免使用構造函數,而應直接進行實例化。
//實例化慢,創(chuàng)建新對象
String bad = new String("bad");
//實例化快
String good = "good"