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"检查。
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();
}
}
}
有超过30亿的设备是运行在Java之上的。根据Oracle的统计数据,光是使用中的Java Card就有有50亿。超过900万程序员选择使用Java进行开发,它是最受开发人员欢迎的语言,同时也是最流行的开发平台。
本文为那些准Java程序员们准备了一系列广为流传的Java最佳编程实践:
1.优先返回空集合而非null
如果程序要返回一个不包含任何值的集合,确保返回的是空集合而不是null。这能节省大量的"if else"检查。
args){public boolean oddOrNot(int num) {
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.");
}
}
}
}
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();
}
}
}
相关文章推荐
- Java实现websocket
- JAVA的内存模型及结构
- SpringMVC学习笔记-REST风格CRUD
- SpringMVC学习笔记-续
- SpringMVC学习笔记-Hello world
- sbt构建scala工程并导入scala-eclipse开发spark
- Spring4学习笔记-SpringJDBC基本操作(补充)
- Java连接Sql数据库经常用到的操作
- WebService学习笔记-添加拦截器到基于Spring的Webservice
- WebService学习笔记-使用CXF编写基于Spring的Webservice
- Mybatis学习笔记-Mybatis与Spring的整合
- java Map集合框架之HashMap
- Spring4学习笔记-Spring4整合Hibernate4
- Spring4学习笔记-Spring4整合Struts2(基于配置文件的方式)
- Spring4学习笔记-声明式事务(基于配置文件的方式)
- Spring4学习笔记-声明式事务(基于注解的方式)
- Spring4学习笔记-JDBC
- Spring4学习笔记-AOP(基于配置文件的方式)
- Spring4学习笔记-AOP(基于注解的方式)
- Spring4学习笔记-AOP前传之动态代理