您的位置:首页 > 编程语言 > Java开发

Java新手问题集锦

2016-02-04 11:06 495 查看
Java是目前最流行的编程语言之一——它可以用来编写Windows程序或者是Web应用,移动应用,网络程序,消费电子产品,机顶盒设备,它无处不在。

有超过30亿的设备是运行在Java之上的。根据Oracle的统计数据,光是使用中的Java Card就有有50亿。超过900万程序员选择使用Java进行开发,它是最受开发人员欢迎的语言,同时也是最流行的开发平台。
本文为那些准Java程序员们准备了一系列广为流传的Java最佳编程实践:

1.优先返回空集合而非null
    如果程序要返回一个不包含任何值的集合,确保返回的是空集合而不是null。这能节省大量的"if else"检查。
 args){         
    for(int i=0;i<5;i++){
        try {                 
            if(i==4){                     
                System.out.println("Inside Try Block.Exiting without executing Finally block.");         System.exit(0);                 
            }             
            }finally{                 
            System.out.println("Inside Finally Block.");             
            }         
        }     
    } 
}
public boolean oddOrNot(int num) {
     return num % 2 == 1; 
}public class Haha {     
    public static void main(String args[]) {     
        System.out.print("H" + "a");     
        System.out.print('H' + 'a');     
    } 
}package com.zappinfo.test;

public class DeadlockSolutionDemo {
    public static Object addLock = new Object();
    public static Object subLock = new Object();

    public static void main(String args[]) {
        MyAdditionThread add = new MyAdditionThread();
        MySubtractionThread sub = new MySubtractionThread();
        add.start();
        sub.start();
    }

    private static class MyAdditionThread extends Thread {
        public void run() {
            synchronized (addLock) {
                int a = 10, b = 3;
                int c = a + b;
                System.out.println("Addition Thread: " + c);
                System.out.println("Holding First Lock...");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                }
                System.out.println("Addition Thread: Waiting for AddLock...");
                synchronized (subLock) {
                    System.out.println("Threads: Holding Add and Sub Locks...");
                }
            }
        }
    }

    private static class MySubtractionThread extends Thread {
        public void run() {
            synchronized (addLock) {
                int a = 10, b = 3;
                int c = a - b;
                System.out.println("Subtraction Thread: " + c);
                System.out.println("Holding Second Lock...");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                }
                System.out.println("Subtraction  Thread: Waiting for SubLock...");
                synchronized (subLock) {
                    System.out.println("Threads: Holding Add and Sub Locks...");
                }
            }
        }
    }
}double square = double a * double a;                           // Optimized 
double cube = double a * double a * double a;                   // Non-optimized 
double cube = double a * double square;                       // Optimized 
double quad = double a * double a * double a * double a;          // Non-optimized 
double quad = double square * double square;                  // Optimizedint noOfStudents = school.listStudents().count;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);
    }
}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();
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: