下面的3行代码编译好了.(请注意,此代码是"人工Java编码"的示例,因此在专业编写的代码中不会出现.)
int x, y;
boolean b=true;
x = b ? y=1 : 2; // Compiles OK.
Run Code Online (Sandbox Code Playgroud)
如果我现在更改上面第3行中的代码,使其看起来像下面的代码行,编译器会生成错误.
// Change the position of the "y assignment", and now the code doesn't compile.
x = b ? 1 : y=2;
Run Code Online (Sandbox Code Playgroud)
这是语法错误消息:

有人可以解释这种行为(对新手Java学习者)吗?谢谢.
我理解语句终止符号;,如果单独使用,则表示空语句.此外,"空循环体"可以是有用的编程构造,并且使用空语句来构造.
看看下面的while语句,在第2行,我决定;用一对背靠背的{}花括号替换终止符号.代码编译并运行正常.这是否意味着Java编译器使用基于null的语句替换空代码块(由"空" {}花括号表示);?
如果Java做了稍微不同的事情,那么两种情况下得到的字节码是否相同?(对不起,我无法检查这台ATM.我是Java新手,我还没有必要的知识来显示和检查字节码).
int i=0,j=100;
// Either a terminating ; symbol or {} braces work to make an "empty loop body".
while (++i < --j) {}
System.out.println("The midpoint between 0 and 100 is " +i); // Midpoint is 50.
Run Code Online (Sandbox Code Playgroud) 我想它与新println()行功能('\n')相关,但是在缩写的基于字母的形式中,这nl不是ln.谢谢你的任何评论.
如果有人能解释为什么会发生以下情况,我将不胜感激.非常感谢.
boolean b = true;
// Compiles OK.
// The LHS "assignment operand" requires no ()parentheses.
if (b=true || b==true);
// Reverse the ||'s operands, and now the code doesn't compile.
if (b==true || b=true);
// Add () around the RHS "assignment operand", and the code now compiles OK.
if (b==true || (b=true));
Run Code Online (Sandbox Code Playgroud)
编辑 -
顺便说一下,代码行#2的编译错误是:"意外类型",并且发生在短路OR运算符所在的位置:
if (b==true || b=true);
// ^ "unexpected type" compilation error occurs here.
Run Code Online (Sandbox Code Playgroud)
编辑2 -
请注意,此问题中的代码片段是"高度人工Java编码"的示例,因此在专业编写的代码中不会出现.
编辑3 -
我是这个非常有用的网站的新手,我刚学会了如何制作和上传Java编译信息的截图.下图复制了我在上面第一个"编辑"中提供的信息.它显示了示例代码行#2的编译错误.

我正在学习Java,而且我正在阅读关于hex String literals的书籍章节.它告诉我,我可以用这种格式创建一个十六进制字符串文字:"\ uxxxx".所以我尝试了这个:
char c = '\u0010';
int x = c;
System.out.println(x); // prints 16.
Run Code Online (Sandbox Code Playgroud)
首先,为什么以下的十六进制字符串文字会导致编译错误?我期待十六进制中的'a'等于十进制10.
char c = '\u000a';
Run Code Online (Sandbox Code Playgroud)
返回以下错误:
..\src\pkgs\main\Main.java:360: error: illegal line end in character literal
char c = '\u000a';
Run Code Online (Sandbox Code Playgroud)
其次,由于我的新手Java状态,我目前无法理解使用的是十六进制字符串文字.我为什么要使用一个?有人可以请我提供他们使用的"真实世界"示例吗?非常感谢.
在下面的代码中,子类"B"中名为"x"的实例变量隐藏了在父超类"A"中也称为"x"的实例变量.
public class A {
public int x;
}
public class B extends A {
public int x;
}
Run Code Online (Sandbox Code Playgroud)
在下面的代码中,为什么println(zx)显示零值?谢谢.
A a = new A();
B b = new B();
a.x = 1;
b.x = 2;
A z = b;
System.out.println(z.x); // Prints 0, but why?
Run Code Online (Sandbox Code Playgroud) 当我在以下代码上运行javac编译器时 -
void method() {
final int x;
x = 1;
x = 1; // (Intentional error)
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误 -
..\src\pkgs\main\Main.java:60: error: variable x might already have been assigned
x = 1; // (Intentional error)
^
Run Code Online (Sandbox Code Playgroud)
我想知道为什么这个错误消息使用"可能"这个词.更准确的描述是否会使用"has"这个词,如"已经被分配"一样?是否有一个特殊的原因,为什么编译器似乎对这种类型的错误使用一种"模糊的描述风格"?谢谢.
在shell命令提示符下,如果我输入javac -help,它会告诉我该-implicit选项指定"...是否为隐式引用的文件生成类文件".有两种选择-implicit: none或者class.
首先,使用-implicit:none和不使用 - implicit选项有什么区别?
其次,关于我的问题标题,我希望提供的示例可以帮助我理解隐式引用的文件是什么.非常感谢.
method(1); // This works -
void method(int... x) { }
void method(int x) { } // - this method is called
Run Code Online (Sandbox Code Playgroud)
如果我向第二个方法添加 varargs 参数,则会收到“对方法的引用不明确”编译错误:
method(1); // This now fails
void method(int... x) { }
void method(int x, String... y) { } // adding String... y causes a problem.
Run Code Online (Sandbox Code Playgroud)
由于 String...y 参数可以留为“空白”,为什么 Java 仍然不选择该方法?谢谢,如果有关于SO的紧密匹配的解释,我们深表歉意;我确实找过一个。
下面的变量(称为b)可以被称为表达式,如果它是唯一位于等号右边的东西吗?
// This code fragment will not compile.
// c is a char, and b is a byte.
c = b;
Run Code Online (Sandbox Code Playgroud)
我问这个问题的原因是因为表达式中的类型提升主题.我知道Java将所有字节提升为整数.这是该代码片段无法编译的唯一原因吗?(请注意,我知道演员阵容;这不是这个帖子的重点.非常感谢.)
编辑:非常 感谢Jon和Peter.使用第二个示例查看此主题:
byte b = 1;
short s = 2;
s = b; // OK
s = b*2; // Not OK (compilation error)
Run Code Online (Sandbox Code Playgroud)
以下是否真的发生了?
(第3行)Java将字节转换为short.(第4行)Java将表达式b*2转换为int.
如果这是正确的,那么它似乎= b; 和= b*2; 是Java处理不同的"表达式".所以,= b; "表达式"不会转换为int,而是扩展为short.但是= b*2; 表达式转换为int,而不是短,即使名为s的目标变量是短的.
编辑2: 还 -
short s1, s2 = 2, s3 = 2;
s1 = s2*s3; // Not OK (compilation error)
Run Code Online (Sandbox Code Playgroud)
即使所有三个变量都是短路,s2*s3; 表达式被提升为int,从而导致编译错误.
目前,我的"main()类"文件看起来有点像下面的代码.我没有将这个示例代码与//评论混为一谈,而是简单地用数字(1到4)标记了四个代码行,这些数字指的是代码后面出现的问题.谢谢.
// package myPackage; // **1**
import myOtherPackage.*;
class mainProject{ // **2**
// **3**
private int myVar;
mainProject(){
myVar = 0;
}
public static void main(String args[]){
// Keep main() looking fairly simple?
// Perhaps just have some "essentials" here, such as error handling?
new mainProject().start(); // **4**
}
private void start(){
// The project gets going here..
}
}
Run Code Online (Sandbox Code Playgroud)
1 与我项目中的其他类文件不同,我没有为"main()类"文件指定包名.这是一个糟糕的设计选择吗?
2 "主类"有一个很好的命名约定吗?将"main"这个词加入到这个类名中是否有帮助?粗略地像"mainProject"这样的东西会是个好主意吗?
3 各种编码结构可以出现在主类文件中.例如,局部变量,构造函数,main()方法和本地方法.他们是否有"最佳订单",他们出现在这个文件中?
4 保持main()方法看起来相当"精简和简单"是否值得?在这个例子中,我刚刚调用了一个名为start()的本地私有方法,该方法用于启动项目.
当我运行下面的源代码时,我在shell窗口中看到的运行时错误如下:
Exception in thread "main" java.lang.IllegalAccessError: tried to access method
pkgs.test.B.mStaPro()V from class pkgs.main.Main3
at pkgs.main.Main3.m6(Main3.java:919)
at pkgs.main.Main3.main(Main3.java:9)
Run Code Online (Sandbox Code Playgroud)
在上面的mStaPro()之后看到字母V是什么意思?
这是我的源代码,全部编译:
class Main3,包main:
package pkgs.main;
import pkgs.test.B;
class Main3 {
static public void main(String args[]) {
new Main3().m6();
}
void m6() {
B.mStaPro();
}
}
Run Code Online (Sandbox Code Playgroud)
A类,包主:
package pkgs.main;
public class A {
static protected void mStaPro() { System.out.println("A mStaPro()"); }
}
Run Code Online (Sandbox Code Playgroud)
B级,包装测试:
package pkgs.test;
import pkgs.main.A;
public class B extends A {
// Note: if this line below is commented out, …Run Code Online (Sandbox Code Playgroud) java ×12
final ×1
hex ×1
inheritance ×1
methodology ×1
operators ×1
out ×1
println ×1
promotions ×1
terminator ×1
types ×1
visibility ×1