编译和运行下面这段Java代码后,控制台输出的结果是?
publicclassTest{static{intx=5;}staticintx,y;publicstaticvoidmain(Stringargs[]){x--;my...
public class Test {
static {
int x = 5;
}
static int x, y;
public static void main(String args[]) {
x--;
myMethod();
System.out.println(x + y + ++x);
}
public static void myMethod() {
y = x++ + ++x;
}
}
A. compiletime error
B. prints: 1
C. prints: 2
D. prints: 3
E. prints: 7 展开
static {
int x = 5;
}
static int x, y;
public static void main(String args[]) {
x--;
myMethod();
System.out.println(x + y + ++x);
}
public static void myMethod() {
y = x++ + ++x;
}
}
A. compiletime error
B. prints: 1
C. prints: 2
D. prints: 3
E. prints: 7 展开
3个回答
展开全部
选D ,打印结果为3
public class Test {
static {
int x = 5;//形参x 赋值为5,没有调用
}
static int x, y;//本类属性,x,y默认为0
public static void main(String args[]) {
x--;// 本类属性x-- this.x=-1
myMethod();
System.out.println(x + y + ++x);//x = this.x = 1 ,y = 0, ++x = 2
//打印结果为 3 x + y + ++x = 1+0+2 = 3
}
public static void myMethod() {
y = x++ + ++x;
// x++ = -1 , this.x = 0
// ++x = 1 , this.x = 1
//y = -1 + 1 = 0
}
}
public class Test {
static {
int x = 5;//形参x 赋值为5,没有调用
}
static int x, y;//本类属性,x,y默认为0
public static void main(String args[]) {
x--;// 本类属性x-- this.x=-1
myMethod();
System.out.println(x + y + ++x);//x = this.x = 1 ,y = 0, ++x = 2
//打印结果为 3 x + y + ++x = 1+0+2 = 3
}
public static void myMethod() {
y = x++ + ++x;
// x++ = -1 , this.x = 0
// ++x = 1 , this.x = 1
//y = -1 + 1 = 0
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询