package?test2;

成都創(chuàng)新互聯(lián)2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元盤龍做網(wǎng)站,已為上家服務(wù),為盤龍各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.InputStreamReader;
import?java.util.HashMap;
import?java.util.Map;
import?java.util.Set;
public?class?JavaCodeAnalyzer?{
public?static?void?analyze(File?file)?throws?IOException{
//FileOutputStream?fos?=?new?FileOutputStream("F;"+File.separator+"result.txt");
if(!(file.getName().endsWith(".txt")||file.getName().endsWith(".java"))){
System.out.println("輸入的分析文件格式不對(duì)!");
}
InputStream?is=?new?FileInputStream(file);
BufferedReader?br=?new?BufferedReader(new?InputStreamReader(is));
String?temp;
int?count=0;
int?countSpace=0;
int?countCode=0;
int?countDesc=0;
MapString,?Integer?map?=?getKeyWords();
while((temp=br.readLine())!=null){
countKeys(temp,?map);
count++;
if(temp.trim().equals("")){
countSpace++;
}else?if(temp.trim().startsWith("/*")||temp.trim().startsWith("http://")){
countDesc++;
}else{
countCode++;
}
}
System.out.printf("代碼行數(shù):"+countCode+"占總行數(shù)的%4.2f\n",(double)countCode/count);
System.out.printf("空行數(shù):"+countSpace+"占總行數(shù)的%4.2f\n",(double)countSpace/count);
System.out.printf("注釋行數(shù):"+countDesc+"占總行數(shù)的%4.2f\n",(double)countDesc/count);
System.out.println("總行數(shù):"+count);
System.out.println("出現(xiàn)最多的5個(gè)關(guān)鍵字是:");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
}
public?static?void?main(String[]?args)?{
getKeyWords();
File?file?=?new?File("F://Test.java");
try?{
analyze(file);
}?catch?(IOException?e)?{
//?TODO?自動(dòng)生成?catch?塊
e.printStackTrace();
}
}
public?static?MapString,Integer?getKeyWords(){
MapString,Integer?map?=?new?HashMapString,?Integer();
String[]keywords?=?{"abstract","assert","boolean","break","byte","case","catch","char","class","continue","default","do","double","else","enum","extends","final","finally","float","for","if","implements","import","instanceof","int","interface","long","native","new","package","private","protected","public","return","????strictfp","short","static","super","????switch","synchronized","this","throw","throws","transient","try","void","volatile","while","goto","const"};
for(String?s:keywords){
map.put(s,?0);
}
return?map;
}
public?static?void?countKeys(String?s,MapString,Integer?map){
SetString?keys?=?map.keySet();
for(String?ss:keys){
if(s.indexOf(ss)!=-1){
map.put(ss,?map.get(ss)+1);
}
}
}
}
上班沒啥時(shí)間了,還有點(diǎn)沒寫完,你在想想。
1)盡量指定類、方法的final修飾符。帶有final修飾符的類是不可派生的,Java編譯器會(huì)尋找機(jī)會(huì)內(nèi)聯(lián)所有的final方法,內(nèi)聯(lián)對(duì)于提升Java運(yùn)行效率作用重大,此舉能夠使性能平均提高50%。
2)盡量重用對(duì)象。由于Java虛擬機(jī)不僅要花時(shí)間生成對(duì)象,以后可能還需要花時(shí)間對(duì)這些對(duì)象進(jìn)行垃圾回收和處理,因此生成過多的對(duì)象將會(huì)給程序的性能帶來很大的影響。
3)盡可能使用局部變量。調(diào)用方法時(shí)傳遞的參數(shù)以及在調(diào)用中創(chuàng)建的臨時(shí)變量都保存在棧中速度較快,其他變量,如靜態(tài)變量、實(shí)例變量等,都在堆中創(chuàng)建速度較慢。
4)慎用異常。異常對(duì)性能不利,只要有異常被拋出,Java虛擬機(jī)就必須調(diào)整調(diào)用堆棧,因?yàn)樵谔幚磉^程中創(chuàng)建了一個(gè)新的對(duì)象。異常只能用于錯(cuò)誤處理,不應(yīng)該用來控制程序流程。
5)乘法和除法使用移位操作。用移位操作可以極大地提高性能,因?yàn)樵谟?jì)算機(jī)底層,對(duì)位的操作是最方便、最快的,但是移位操作雖然快,可能會(huì)使代碼不太好理解,因此最好加上相應(yīng)的注釋。
6)盡量使用HashMap、ArrayList、StringBuilder,除非線程安全需要,否則不推薦使用 Hashtable、Vector、StringBuffer,后三者由于使用同步機(jī)制而導(dǎo)致了性能開銷。
盡量在合適的場(chǎng)合使用單例。使用單例可以減輕加載的負(fù)擔(dān)、縮短加載的時(shí)間、提高加載的效率,但并不是所有地方都適用于單例。
class Person {
public String name; //定義一個(gè)name屬性
public String location; //定義一個(gè)location屬性
Person(String name){ //定義一個(gè)構(gòu)造方法,用于初始化name屬性
this.name = name;
location = "beijing"; //初始化location屬性
}
Person(String name,String location) { //重載一個(gè)構(gòu)造方法,用于初始化name屬性和location屬性
this.name = name;
this.location = location;
}
public String info() { //定義一個(gè)方法,返回實(shí)例的屬性信息
return "name:"+ name +"location:"+location;
}
}
class Teacher extends Person { //定義一個(gè)Teacher類,從Person繼承
private String capital;
Teacher (String name,String capital) { //定義一個(gè)構(gòu)造方法,用于初始化name和capital屬性
this(name,"beijing",capital); //調(diào)用本類中定義的構(gòu)造方法
}
Teacher (String n,String l,String capital){ //定義一個(gè)構(gòu)造方法,用于初始Teacher類中的capital,name和location屬性
super (n,l); //調(diào)用父類中的構(gòu)造方法初始化name,location屬性
this.capital = capital; //初始化Teacher類中的capital屬性
}
public String info() { //重寫父類中的info()方法,并且附加capital屬性的信息
return super.info() + "capital" + capital; //返回Teacher的各個(gè)屬性信息
}
}
class Student extends Person { // 定義一個(gè)Student類,從Person繼承
private String school; //定義chool字段
Student(String name,String school) { //定義一個(gè)構(gòu)造方法,用于初始化name和school屬性
this(name,"beijing",school); //調(diào)用本類中定義的構(gòu)造方法
}
Student(String n,String l,String school) { //定義一個(gè)構(gòu)造方法,用于初始Student類中的school,name和location屬性
super(n,l); //調(diào)用父類中的構(gòu)造方法初始化name,location屬性
this.school = school; //初始化Student類中的school字段
}
public String info() { /重寫父類中的info()方法
return super.info() + "school:" + school; //返回Student類中各個(gè)字段或?qū)傩孕畔?/p>
}
}
public class Test { //定義Test主類,用于測(cè)試定義各個(gè)類
public static void main (String[] args) {
Person p1 = new Person("A"); //實(shí)例化Person類,調(diào)用構(gòu)造器初始化name屬性
Person p2 = new Person("B","shanghai"); //實(shí)例化Person類,調(diào)用構(gòu)造器初始化name,location屬性
Student s1 = new Student("C","s1"); //實(shí)例化Student類,調(diào)用構(gòu)造器初始化name,school屬性
Student s2 = new Student("C","shanghai","s2"); //實(shí)例化Student類,調(diào)用構(gòu)造器初始化name,location,school屬性
Teacher t1 = new Teacher("D","perfessor"); //實(shí)例化Teacher類,調(diào)用構(gòu)造器初始化name,capital屬性
System.out.println(p1.info()); //輸出實(shí)例p1的屬性信息
System.out.println(p2.info()); //輸出實(shí)例p2的屬性信息
System.out.println(s1.info()); //輸出實(shí)例s1的屬性信息
System.out.println(s2.info()); //輸出實(shí)例s2的屬性信息
System.out.println(t1.info()); //輸出實(shí)例t1的屬性信息
}
}
//求采納
//一個(gè)隨機(jī)數(shù)生成工具
Random?rand?=?new?Random();
//?rand.nextInt()?用工具生成一個(gè)隨機(jī)的整數(shù)
//rand.nextInt()?%?a.length?對(duì)上邊數(shù)組長度取余?應(yīng)該是對(duì)?52?取余
//任何整數(shù)對(duì)52取余結(jié)果只能是?-51?~?51?之間的一個(gè)整數(shù)
int?index?=?rand.nextInt()?%?a.length;?
//如果得到的這個(gè)整數(shù)小于0?
if(index??0){?
//用0?減去這個(gè)數(shù)即變成其相反數(shù)
index?=??0?-?index;
}
//從數(shù)組a中取對(duì)應(yīng)位置的字符,比如index=2????a[index]?就是?c
return?a[index];
親測(cè):結(jié)果為空
原因,System.exit(0)將當(dāng)前JVM停止掉了。這個(gè)方法的作用就是將當(dāng)前程序停止,參數(shù)為0表示正常退出,參數(shù)為1表示非正常退出。
JVM都停止了,后面的程序自然運(yùn)行不了。所以finally里的語句根本沒走到。
分享題目:java的代碼分析 java代碼分析題大學(xué)期末考試
URL網(wǎng)址:http://chinadenli.net/article8/hppgip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、品牌網(wǎng)站建設(shè)、商城網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)