
正文
java如何分析代码实现 java代码方法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java代码分析
亲测:结果为空
原因,System.exit(0)将当前JVM停止掉了。这个方法的作用就是将当前程序停止,参数为0表示正常退出,参数为1表示非正常退出。
JVM都停止了,后面的程序自然运行不了。所以finally里的语句根本没走到。
相关问答
Q1: Java代码如何优化?从哪些方面入手?分析?
1)尽量指定类、方法的final修饰符。带有final修饰符的类是不可派生的,Java编译器会寻找机会内联所有的final方法,内联对于提升Java运行效率作用重大,此举能够使性能平均提高50%。
2)尽量重用对象。由于Java虚拟机不仅要花时间生成对象,以后可能还需要花时间对这些对象进行垃圾回收和处理,因此生成过多的对象将会给程序的性能带来很大的影响。
3)尽可能使用局部变量。调用方法时传递的参数以及在调用中创建的临时变量都保存在栈中速度较快,其他变量,如静态变量、实例变量等,都在堆中创建速度较慢。
4)慎用异常。异常对性能不利,只要有异常被抛出,Java虚拟机就必须调整调用堆栈,因为在处理过程中创建了一个新的对象。异常只能用于错误处理,不应该用来控制程序流程。
5)乘法和除法使用移位操作。用移位操作可以极大地提高性能,因为在计算机底层,对位的操作是最方便、最快的,但是移位操作虽然快,可能会使代码不太好理解,因此最好加上相应的注释。
6)尽量使用HashMap、ArrayList、StringBuilder,除非线程安全需要,否则不推荐使用 Hashtable、Vector、StringBuffer,后三者由于使用同步机制而导致了性能开销。
尽量在合适的场合使用单例。使用单例可以减轻加载的负担、缩短加载的时间、提高加载的效率,但并不是所有地方都适用于单例。
Q2: java分析以下需求,并用代码实现:
根据需求java如何分析代码实现,代码大致如下
public class T2 {
public static String getPropertyGetMethodName(String property) {
if (property == null || property.trim().length() == 0)
return "";
return "get" + String.valueOf(property.charAt(0)).toUpperCase()
+ property.subSequence(1, property.length());
}
public static void main(String[] args) {
System.out.println(getPropertyGetMethodName("a"));
System.out.println(getPropertyGetMethodName("name"));
System.out.println(getPropertyGetMethodName("names"));
}
}
Q3: java源代码分析 实在是不太会,求高手教教我。
package test2;
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("输入的分析文件格式不对!");
}
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("//")){
countDesc++;
}else{
countCode++;
}
}
System.out.printf("代码行数:"+countCode+"占总行数的%4.2f\n",(double)countCode/count);
System.out.printf("空行数:"+countSpace+"占总行数的%4.2f\n",(double)countSpace/count);
System.out.printf("注释行数:"+countDesc+"占总行数的%4.2f\n",(double)countDesc/count);
System.out.println("总行数:"+count);
System.out.println("出现最多的5个关键字是:");
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 自动生成 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);
}
}
}
}
上班没啥时间了,还有点没写完,你在想想。
java如何分析代码实现的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java代码方法、java如何分析代码实现的信息别忘了在本站进行查找喔。







