事实:
javac
被编程以检测变量是否final
或如果它可以被有效地 处理final
.
证明:
此代码说明了这一点.
public static void finalCheck() {
String str1 = "hello";
Runnable r = () -> {
str1 = "hello";
};
}
Run Code Online (Sandbox Code Playgroud)
这无法编译,因为编译器能够检测到正在函数中重新分配String
引用str1
.
现在
情况1:
Javac final
String
通过避免创建StringBuilder
和相关操作来对实例进行了很好的优化.
证明
这个java方法
public static void finalCheck() {
final String str1 = "hello";
final String str2 = "world";
String str3 = str1 + " " + str2;
System.out.println(str3);
}
Run Code Online (Sandbox Code Playgroud)
编译成
public static void finalCheck();
Code:
0: ldc …
Run Code Online (Sandbox Code Playgroud) java final compiler-optimization language-lawyer javacompiler
我对此有麻烦,已经在本网站上回答过多次。
我对这个问题的看法有些不同。我有一个使用maven 3.5.x和java版本10(在maven-compiler-plugin中配置)可以很好地构建的项目。在IntelliJ的项目结构中查看时,所有模块的语言级别均为10(但项目设置为语言级别6,在项目结构中选择的SDK为1.8 ...
但是,即使我将sdk更改为java 10,并将项目语言级别更改为10,仍然会发生以下粘贴的错误... ????
我的IntelliJ版本:IntelliJ IDEA 2018.2.5
重现此错误:
GitHub:https : //github.com/jactor-rises/jactor-rises.git
尝试在IntelliJ中构建它:
信息:java:编译模块'jactor-commons'时发生错误信息:javac 10.0.1用于编译Java源信息:29/10/2018,21:31-编译在4 s 777中完成了1个错误和0个警告ms错误:java:不支持发行版本5
我用java for循环进行了一些运行时测试,并发现了一个奇怪的行为.对于我的代码,我需要原始类型的包装器对象,如int,double等,以模拟io和输出参数,但这不是重点.只需看我的代码.具有字段访问权限的对象如何比原始类型更快?
for
具有prtimitive类型的循环:
public static void main(String[] args) {
double max = 1000;
for (int j = 1; j < 8; j++) {
double i;
max = max * 10;
long start = System.nanoTime();
for (i = 0; i < max; i++) {
}
long end = System.nanoTime();
long microseconds = (end - start) / 1000;
System.out.println("MicroTime primitive(max: ="+max + "): " + microseconds);
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
MicroTime原语(max:= 10000.0):110
MicroTime原语(max:= 100000.0):1081
MicroTime原语(max:=
1000000.0 ):2450 MicroTime原语(max:= 1.0E7):28248
MicroTime原语(max:= 1.0E8) :276205 …
我用一个方法创建了以下界面:
public interface Greeting {
public void perform();
}
Run Code Online (Sandbox Code Playgroud)
我试图将接口的一个实例传递给 greet() 方法。
public class Greeter {
public void greet(Greeting greeting){
greeting.perform();
}
public static void main(String[] args) {
Greeter greeter=new Greeter();
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个问候界面的实现:
public class HelloWorldGreeting implements Greeting {
@Override
public void perform() {
System.out.println("Hello World");
}
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误(HelloWorldGreeting 类型的方法 perform() 必须覆盖或实现超类型方法),快速修复为“删除@Override 注释”
我还检查了编译器配置。只有 1.8。
请让我知道为什么会出现错误或帮助我修复它。提前致谢。
java annotations compiler-errors javacompiler java-annotations
我将Android Studio从3.0.1更新为3.1.0
但是在更新后,当我构建项目时,它显示2警告:
1.将编译替换为实现(编译支持将于2018年底终止)
2.用testImplementaion替换testCompile(并且testCompile支持将在2018年底终止)
因此,最后进行这些更改,但是之后,它显示了一些错误:
build.gradle(模块:应用)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "biz.coolpage.aashish.app"
minSdkVersion 17
targetSdkVersion 27
versionCode 4
versionName "1.2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:design:27.1.0'
implementation project(':library')
}
Run Code Online (Sandbox Code Playgroud)
build.gradle(Project:Abc)
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
}
}
allprojects {
repositories {
jcenter() …
Run Code Online (Sandbox Code Playgroud) 这是我用来编译java类的代码:
public void javaCompile(String fileName) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager
.getJavaFileObjectsFromStrings(Arrays.asList(fileName));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null,
null, compilationUnits);
boolean success = task.call();
fileManager.close();
System.out.println("Success: " + success);
}
Run Code Online (Sandbox Code Playgroud)
问题是我希望收到有关出现的错误的更多信息(超过Success:false).有人能帮我吗?
如果我做
try(Lock lock = lockProvider.lock()) {
// some code that doesn't use variable lock
}
Run Code Online (Sandbox Code Playgroud)
编译器或JITer是否存在删除锁创建的风险,因为它在块中看到它未使用?
后来编辑:
一点背景.我来自.NET背景,在C#中,它允许执行以下操作:
using(Transaction tx = BeginTransaction())
{
// code that does things without touching the tx variable, such as SQL connections and stuff
}
Run Code Online (Sandbox Code Playgroud)
事实上它甚至可以缩短为
using(BeginTransaction())
{
// code that does things without touching the tx variable, such as SQL connections and stuff
}
Run Code Online (Sandbox Code Playgroud)
静态编译器和JIT编译器将保持BeginTransaction
调用,并且在运行时它将始终发生.
然而,在Java似乎是一个很大的问题和消极周围使用try-与资源用于其他的东西,是资源.
我无法编译我的项目,我使用 Android Studio 的“将 Java 文件转换为 Kotlin 文件”将我的一项活动转换为 kotlin,并修复了未正确转换的代码的所有错误。
Java编译器日志:
引起:java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:合并dex档案时
出错: com.android.builder.dexing.DexArchiveMergerException:合并dex档案时出错:
com.android.tools.r8.CompilationFailedException :编译未能完成
com.android.tools.r8.utils.AbortException:错误:空,无法在单个 dex 文件中容纳请求的类(# 方法:110666 > 65536;# 字段:67264 > 65536)
build.gradle(应用程序)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
versionName "2.2.4"
versionCode 13
minSdkVersion 16
targetSdkVersion 27
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
debug {
applicationIdSuffix ".debug"
versionNameSuffix ".d.5"
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
flavorDimensions "server"
productFlavors …
Run Code Online (Sandbox Code Playgroud) 在intellij idea中使用maven打包我的项目时,它给出了以下错误,我的java编译器版本也设置为1.8:-source 1.5 [ERROR]不支持菱形运算符(使用-source 7或更高版本启用菱形运算符)I真的很困惑,因为java版本和编译器版本都设置为1.8.
我有一个ComPac.java
带有以下代码的java文件:
package com;
public class ComPac{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Run Code Online (Sandbox Code Playgroud)
该文件位于路径: /home/ec2-user/java_c
为了编译这个文件,我运行了javac Compac.java
,然后生成了类文件。
现在轮到运行类文件了。
所以我做了java ComPac
(下面的截图)
可以理解的是,我得到了错误Error: Could not find or load main class ComPac. Caused by: java.lang.NoClassDefFoundError: com/ComPac (wrong name: ComPac).
我假设这是因为 java 文件中com
声明了包。
所以我尝试了,java com.ComPac
并希望它能工作(下面的截图)。
但我得到了错误:Error: Could not find or load main class com.ComPac. Caused by: java.lang.ClassNotFoundException: com.ComPac
.
那么我该如何运行呢?当涉及到 java 中的包时,运行的逻辑究竟是什么?
使用的Java- openjdk version "11.0.8" 2020-07-14 LTS(AWS Corretto) …
Function<Double,Integer> f1=(d)-> {
if(d>5)
return 0;
return 1;
};
DoubleToIntFunction f2=(d)-> {
if(d>5)
return 1;
return 0;
};
double d=5.0;
f1.apply(d);
f2.applyAsInt(d);
Run Code Online (Sandbox Code Playgroud)
将f1优化为DoubleToIntFunction(有些像f2).
哪个具有更大的字节码大小或在 Java 中相同?
if (a > b) a = b;
Run Code Online (Sandbox Code Playgroud)
对比
if (a > b) {
a = b;
}
Run Code Online (Sandbox Code Playgroud) javacompiler ×12
java ×11
android ×2
java-8 ×2
jvm ×2
optimization ×2
annotations ×1
bytecode ×1
final ×1
for-loop ×1
java-10 ×1
java-11 ×1
java-package ×1
jit ×1
jvm-bytecode ×1
kotlin ×1
maven ×1
updates ×1