「java是静态优化编译吗」java是动态编译还是静态编译

博主:adminadmin 2022-12-17 04:27:07 59

今天给各位分享java是静态优化编译吗的知识,其中也会对java是动态编译还是静态编译进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java如何优化编译呢?

#java编译器对`String常量表达式`的优化:

- 1.String+String 可以被编译器识别为常量表达

String a="ab" ;

String b="a"+"b";//编译后:b="ab"

System.out.println(a==b);//true

分析:

编译器将"a"+"b"当做常量表达式,在编译时期进行优化,直接取"ab". 在运行时期

并没有创建新的对象,而是从jvm字符串常量池中获取之前已经存在的"ab"对象.

- 2.String+基本类型 可以被编译器识别为常量表达式

String a="a1";

String b="a"+1; //"a1"

String c="a"+true;//"atrue"

String d="a"+3.14;//"a3.14"

#java编译器对`常量`优化:

* 它是编译时的一项优化技术,将代码的常量计算在编译期完成,节约了运行时的计算量.

1.常量替换

//编译前:

final int x=10;

int y=x;

//编译后

int x=10;

int y=10;//编译时,常量替换了

2.数学恒等式的模式匹配替换

//编译前:

int x=10+10;

//编译后

int x=20;//编译时,模式匹配替换了

3.常量折叠

//编译前:

boolean flag=true||(a || b c);

//编译后

boolean flag=true;//编译时,常量折叠了

java 编译优化问题

java编译的结果是字节码而不是二进制,所以在运行时vm的优化才是重要的,包括VM的回收策略、分配给VM内存的大小都能在一定程度上影响性能。Sun的VM支持热点编译,对高频执行的代码段翻译的2进制会进行缓存,这也是VM的一种优化。

IBM JVM处理数学运算速度最快,BEA JVM处理大量线程和网络socket性能最好,而Sun JVM处理通常的商业逻辑性能最好。不过Hotspot的Server mode被报告有稳定性的问题。

Java 的最大优势不是体现在执行速度上,所以对Compiler的要求并不如c++那样高,代码级的优化还需要程序员本身的功底。

贴个java的运行参数:

Usage: java [-options] class [args...]

(to execute a class)

or java [-options] -jar jarfile [args...]

(to execute a jar file)

where options include:

-client to select the "client" VM

-server to select the "server" VM

-hotspot is a synonym for the "client" VM [deprecated]

The default VM is client.

-cp class search path of directories and zip/jar files

-classpath class search path of directories and zip/jar files

A ; separated list of directories, JAR archives,

and ZIP archives to search for class files.

-Dname=value

set a system property

-verbose[:class|gc|jni]

enable verbose output

-version print product version and exit

-version:value

require the specified version to run

-showversion print product version and continue

-jre-restrict-search | -jre-no-restrict-search

include/exclude user private JREs in the version search

-? -help print this help message

-X print help on non-standard options

-ea[:packagename...|:classname]

-enableassertions[:packagename...|:classname]

enable assertions

-da[:packagename...|:classname]

-disableassertions[:packagename...|:classname]

disable assertions

-esa | -enablesystemassertions

enable system assertions

-dsa | -disablesystemassertions

disable system assertions

-agentlib:libname[=options]

load native agent library libname, e.g. -agentlib:hprof

see also, -agentlib:jdwp=help and -agentlib:hprof=help

-agentpath:pathname[=options]

load native agent library by full pathname

-javaagent:jarpath[=options]

load Java programming language agent, see

java.lang.instrument

-Xmixed mixed mode execution (default)

-Xint interpreted mode execution only

-Xbootclasspath:directories and zip/jar files separated by ;

set search path for bootstrap classes and resources

-Xbootclasspath/a:directories and zip/jar files separated by ;

append to end of bootstrap class path

-Xbootclasspath/p:directories and zip/jar files separated by ;

prepend in front of bootstrap class path

-Xnoclassgc disable class garbage collection

-Xincgc enable incremental garbage collection

-Xloggc:file log GC status to a file with time stamps

-Xbatch disable background compilation

-Xmssize set initial Java heap size

-Xmxsize set maximum Java heap size

-Xsssize set java thread stack size

-Xprof output cpu profiling data

-Xfuture enable strictest checks, anticipating future default

-Xrs reduce use of OS signals by Java/VM (see

documentation)

-Xcheck:jni perform additional checks for JNI functions

-Xshare:off do not attempt to use shared class data

-Xshare:auto use shared class data if possible (default)

-Xshare:on require using shared class data, otherwise fail.

Java虚拟机(JVM)参数配置说明

在Java、J2EE大型应用中,JVM非标准参数的配置直接关系到整个系统的性能。

JVM非标准参数指的是JVM底层的一些配置参数,这些参数在一般开发中默认即可,不需

要任何配置。但是在生产环境中,为了提高性能,往往需要调整这些参数,以求系统达

到最佳新能。

另外这些参数的配置也是影响系统稳定性的一个重要因素,相信大多数Java开发人员都

见过“OutOfMemory”类型的错误。呵呵,这其中很可能就是JVM参数配置不当或者就没

有配置没意识到配置引起的。

为了说明这些参数,还需要说说JDK中的命令行工具一些知识做铺垫。

首先看如何获取这些命令配置信息说明:

假设你是windows平台,你安装了J2SDK,那么现在你从cmd控制台窗口进入J2SDK安装目

录下的bin目录,然后运行java命令,出现如下结果,这些就是包括java.exe工具的和

JVM的所有命令都在里面。

-----------------------------------------------------------------------

D:\j2sdk15\binjava

Usage: java [-options] class [args...]

(to execute a class)

or java [-options] -jar jarfile [args...]

(to execute a jar file)

where options include:

-client to select the "client" VM

-server to select the "server" VM

-hotspot is a synonym for the "client" VM [deprecated]

The default VM is client.

-cp class search path of directories and zip/jar files

-classpath class search path of directories and zip/jar files

A ; separated list of directories, JAR archives,

and ZIP archives to search for class files.

-Dname=value

set a system property

-verbose[:class|gc|jni]

enable verbose output

-version print product version and exit

-version:value

require the specified version to run

-showversion print product version and continue

-jre-restrict-search | -jre-no-restrict-search

include/exclude user private JREs in the version search

-? -help print this help message

-X print help on non-standard options

-ea[:packagename...|:classname]

-enableassertions[:packagename...|:classname]

enable assertions

-da[:packagename...|:classname]

-disableassertions[:packagename...|:classname]

disable assertions

-esa | -enablesystemassertions

enable system assertions

-dsa | -disablesystemassertions

disable system assertions

-agentlib:libname[=options]

load native agent library libname, e.g. -agentlib:hprof

see also, -agentlib:jdwp=help and -agentlib:hprof=help

-agentpath:pathname[=options]

load native agent library by full pathname

-javaagent:jarpath[=options]

load Java programming language agent, see

java.lang.instrument

-----------------------------------------------------------------------

在控制台输出信息中,有个-X(注意是大写)的命令,这个正是查看JVM配置参数的命

令。

其次,用java -X 命令查看JVM的配置说明:

运行后如下结果,这些就是配置JVM参数的秘密武器,这些信息都是英文的,为了方便

阅读,我根据自己的理解翻译成中文了(不准确的地方还请各位博友斧正)

-----------------------------------------------------------------------

D:\j2sdk15\binjava -X

-Xmixed mixed mode execution (default)

-Xint interpreted mode execution only

-Xbootclasspath:directories and zip/jar files separated by ;

set search path for bootstrap classes and resources

-Xbootclasspath/a:directories and zip/jar files separated by ;

append to end of bootstrap class path

-Xbootclasspath/p:directories and zip/jar files separated by ;

prepend in front of bootstrap class path

-Xnoclassgc disable class garbage collection

-Xincgc enable incremental garbage collection

-Xloggc:file log GC status to a file with time stamps

-Xbatch disable background compilation

-Xmssize set initial Java heap size

-Xmxsize set maximum Java heap size

-Xsssize set java thread stack size

-Xprof output cpu profiling data

-Xfuture enable strictest checks, anticipating future default

-Xrs reduce use of OS signals by Java/VM (see

documentation)

-Xcheck:jni perform additional checks for JNI functions

-Xshare:off do not attempt to use shared class data

-Xshare:auto use shared class data if possible (default)

-Xshare:on require using shared class data, otherwise fail.

The -X options are non-standard and subject to change without notice.

-----------------------------------------------------------------------

JVM配置参数中文说明:

-----------------------------------------------------------------------

1、-Xmixed mixed mode execution (default)

混合模式执行

2、-Xint interpreted mode execution only

解释模式执行

3、-Xbootclasspath:directories and zip/jar files separated by ;

set search path for bootstrap classes and resources

设置zip/jar资源或者类(.class文件)存放目录路径

3、-Xbootclasspath/a:directories and zip/jar files separated by ;

append to end of bootstrap class path

追加zip/jar资源或者类(.class文件)存放目录路径

4、-Xbootclasspath/p:directories and zip/jar files separated by ;

prepend in front of bootstrap class path

预先加载zip/jar资源或者类(.class文件)存放目录路径

5、-Xnoclassgc disable class garbage collection

关闭类垃圾回收功能

6、-Xincgc enable incremental garbage collection

开启类的垃圾回收功能

7、-Xloggc:file log GC status to a file with time stamps

记录垃圾回日志到一个文件。

8、-Xbatch disable background compilation

关闭后台编译

9、-Xmssize set initial Java heap size

设置JVM初始化堆内存大小

10、-Xmxsize set maximum Java heap size

设置JVM最大的堆内存大小

11、-Xsssize set java thread stack size

设置JVM栈内存大小

12、-Xprof output cpu profiling data

输入CPU概要表数据

13、-Xfuture enable strictest checks, anticipating future default

执行严格的代码检查,预测可能出现的情况

14、-Xrs reduce use of OS signals by Java/VM (see

documentation)

通过JVM还原操作系统信号

15、-Xcheck:jni perform additional checks for JNI functions

对JNI函数执行检查

16、-Xshare:off do not attempt to use shared class data

尽可能不去使用共享类的数据

17、-Xshare:auto use shared class data if possible (default)

尽可能的使用共享类的数据

18、-Xshare:on require using shared class data, otherwise fail.

尽可能的使用共享类的数据,否则运行失败

The -X options are non-standard and subject to change without notice.

java语言在被设计的时候为什么会加上 编译 这一步?

java语言在被设计的时候为什么会加上编译这一步,其实主要的考虑因素是性能,静态语言的执行效率比动态语言要高得多。所以需要编译。

为什么有人说Java是静态语言,又说Java语言是动态的?

Java需要进行编译后运行在jvm上,所以Java是静态语言,但是Java又可以通过接口等方式,在运行时注入相关的类的实现,所以这个又是其动态性的提现

java 静态函数和非静态函数的运行性能哪个好

另外补充一下,静态函数在编译期就可以做静态优化,非静态函数只有等运行时的动态优化了,如果考虑这些的话,在某些情况下静态函数的效率要高过非静态函数。

JAVA能静态编译吗?

JAVA本身就是表态编译语言, 我想你要找的是JAVA程序的发布

首先JAVA程序运行肯定需要在目标机器上有jre(不是JDK), 如果你想不管目标机上有没有jre程序都能正常运行的话就要在你的发布包里包含jre

发布目录结构举例:

App

......bin 包含启动脚本

......lib 包含引入的第三方包

......jre JRE必要的包

启动脚本这样写:

../jre/bin/java -cp ../lib/JAR包名称.jar 启动类名称

java是静态优化编译吗的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java是动态编译还是静态编译、java是静态优化编译吗的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-17,除非注明,否则均为首码项目网原创文章,转载请注明出处。