1、字符定义:
char c=65; System.out.println(c);
输出结果:
A
分析:java中定义字符方式:
2、程序输出结果
int a = 5; System.out.println("vlaue is " + ((a < 5) ? 10.9 : 9));
输出结果:
vlaue is 9.0
条件表达式执行说明:
分析:因为前面有一个10.9,所以9会自动提升为9.0,所以最终输出结果是9.0。
3、程序输出结果
char x = ‘x‘; int i = 10; System.out.println(false ? i : x); System.out.println(false ? 10 : x);
输出结果:
120
x
分析:int i=10;中的i是个变量,所以第一个输出的x被提升为int型,‘x’对应的值是120,所以输出120;当三目运算中后两个表达式,有一个是常量表达式(本题中是10),另一个是T类型(本题中是char),而常量表达式可以被T类型表示,则输出结果为T类型。因为10为常量,且可以表示为char类型,所以输出结果为char。
4、下列输出结果:
public class test1 { private int count; public static void main(String[] args) { // TODO Auto-generated method stub test1 t1 = new test1(99); System.out.println(t1.count); } test1(int ballcount) { count = ballcount; } }
输出结果:
99
分析:count定义为私有属性,并不会阻止构造方法对其进行初始化,当然也可在该类进行调用私有属性,但是如果不在同一个类,则不行。
public class test1 { public static void main(String[] args) { test2 t2 = new test2(88); System.out.println(t2.count); } } class test2 { private int count2; test2(int ballcount) { count2 = ballcount; } }
上述代码会编译出错:The field test2.count2 is not visible
原文:http://www.cnblogs.com/SaraMoring/p/5748367.html