Java 认证学习---part 071110

 
Java 认证学习
 
What is bellow is what I don’t know before.
 
1.      访问控制问题
 
2.      Uses Hx and Oct to describe a character, such as ‘/176’, ‘/u007e’.
The style is /nnn for Oct and /unnnn for Hx.
 
 
3.      问题:
 
4.      decimal,octal:0nnnnn,hexadecimal:0xnnnnn
5.      the default floating-point literals are double precision ,which is double type. If you want to use single precision, float type, you must to define it explicitly as :
float f1 = 49837849.029847F;// or 49837849.029847f
and the following has a compiling error:
float f = 23.467890;
what’s more:
double d = 110599.995011D; // Optional, not required
double g = 987.897;        // No 'D' suffix, but OK because the
                           // literal is a double by default
 
floating-point literals has two style:
normal one: 987.897
scientific one:
6.      java 内存管理可以消除以下错误:
用new产生对象时,会返回一个对象引用,其本质上是指针,只是在java中不能对指针运算。
7.      关于垃圾收集:
不是所有不用的对象都会马上回收内存,因为回收是耗时间的,只有在需要更多内存的时候才会进行垃圾收集,并且收集是无序。
对象不再使用有几种表现:其对应的变量被赋null或其他对象;当方法返回,其方法的变量就不再有效。
没有delete来显示删除对象,但是可以显式调用垃圾收集程序:
Runtime rt = Runtime.getRuntime();
Rt.gc();
 
注意:回收为无用信息(对象不再使用?)和内存回收似乎不一样。确定为无用信息时,应该不会立即回收,见上。
 
8.      finalize ()方法
对象在被收集为无用信息时,调用finalize()。
 
调用finalize时注意使用try catch
 
9.      居然是这样的,天啊。
答案:
 
10.  构造器问题
Note:
A)      如果没有定义构造器,java会定义默认的无参构造器。
B)      如果自定义了构造器,java不会定义默认的无参构造器,需要无参构造器的话必须显式自定义之。
C)      可以在构造器第一行调用超类构造器。
D)      如果没有显式调用超类构造器,java会尝试调用超类的无参构造器;如果超类没有无参构造器(自定义构造器,而没定义无参构造器的情况也属此类),则编译器报错。
11.  数据类型
java是强类型语言,即每个变量和表达式都有一个类型,每个类型被严格定义;所有赋值都要进行类型兼容性检查。
Java不像其他语言,它不进行冲突数据类型的自动化转换。
不能将高精度数据转换为低精度数据,强制转换报编译错。
注意:char是16位unicode字符
注意:如果成员变量没有显式赋值,将赋默认值。但局部变量没有显示赋值,将报编译错。
12.  整数类型
注意:整数的存储空间主要决定于运行环境如何规定,而不是其具体类型,如下:
当执行混合整数类型操作,先会将类型位数小的数据向位数高的类型转换。
整数操作很少报 ArichmeticException 错,唯一的情况是被 0 除。
整数值上溢或下溢都不会报错,而是从该类型的另一个极限开始累积,例如byte类型数据上溢,其结果是:
13.  字符类型
java中的char应该是使用utf16。详见 http://www.ingrid.org/java/i18n/utf-16/ (谈编码在java的低层使用,如下图)
三种方式给char赋值:
Unicode码(90),转义序列(’/u0090’),字面量(‘Z’)。
Ascii字符从’/u0000’到’/u00ff’。
Char ch = ‘A’;
ch++;
注意:Char的默认值是’/u0000’是,ascii中的 ,而不是空。
 
14.  浮点数
浮点数运算出错时,不会报异常,比如被0除。
出错时使用正负无限值,NaN来表示:
0.0                   0.0                  NaN                  NaN
例子:
System.out.println(20.0 / 0.0 );
System.out.println(20.0 % 0.0 );
System.out.println(-20.0 % 0.0 );
System.out.println(-20.0 / 0.0 );
System.out.println( 0.0 / 0.0 );
System.out.println( 0.0 % 0.0 );
System.out.println(20.0 / Double.POSITIVE_INFINITY);
System.out.println(20.0 % Double.POSITIVE_INFINITY);
System.out.println( Double.POSITIVE_INFINITY / 20 );
System.out.println( Double.POSITIVE_INFINITY % 20 );
System.out.println( Double.POSITIVE_INFINITY / Double.POSITIVE_INFINITY);
System.out.println( Double.POSITIVE_INFINITY % Double.POSITIVE_INFINITY);
结果:
Infinity
NaN
NaN
-Infinity
NaN
NaN
0.0
20.0
Infinity
NaN
NaN
NaN
15.  数组
注意:数组声明与初始化是两件事。
Java数组是对象,因此可以在运行时动态分配大小,这在c是不行的。数组在类层次中的结构如下:
 
数组创建的方式:
显式使用new创建;
在声明的同时,使用{}创建,如:
    注意,在声明之后不能再使用{}对数组赋值,即 是错误的。
 
初始化的一个问题:
对于数组变量,不论是成员变量还是局部变量在没有显式初始化的情况下,系统会隐式初始化,即用默认值初始化。
这与其他类型的数据,如果是局部变量不存在隐式初始化不一样。
 
数组的数组:
对比左右两种赋值方式,右边在第三个数组之后有“,”,两种方式均可。
   
因为java的多维数组是数组的数组,所以可以单独为每列分配长度,即下面的做法合法:
int[][] ti = new int[2][];
ti[0] = new int[3];
ti[1] = new int[2];
16.  equals() ==
注意File类对equals的覆盖:
17.  运算符的优先级问题
 
关于instanceof的问题,有的编译器,比如sun的jdk在断定一个对象a不能从类B继承时 a instanceof B,是不能通过编译的。
18.  关于循环
关于for的一个注意:
    关于switch:
    关于加标号的循环:
    通过在循环中使用标号,并在break后引用,就可以跳转到 加标号的语句块末尾,进行执行。
    标号可以在嵌套循环时,用来跳出多层循环;而break只能跳出当前循环。
    标号跟continue一起使用时,描述的是从哪个循环继续执行。
19.  标号的问题
 
20.  方法
 
重载需要注意的:
对比两段代码:
   
    左边如果加写test(1,1),仍然不错,1可以转换为1.0,从而符合void test(double i, int j)。
而右边就有问题了,test(1,1)到底是直接使用void test(int i, long j),还是void test(long i, int j),这是个问题,因为从1到1L的转换是成立的,就造成两个test方法都可以的局面,所以报错。因此改写成test(1L,1),意图就明显了。
    注意:整型不会自动向下转型的,只会自动向上转。在考虑使用哪个重载的方法时注意考虑这个。
 
    关于方法的继承问题:
    abstract方法是不能有主体的。
21.  实例变量
实例变量和类变量都会被子类继承。
如果子类没有覆盖父类的实例变量/类变量,则子类对象.实例变量/类变量,在访问控制的范围内使用的是父类的实例变量/类变量。
如果覆盖的话,就使用子类的实例变量/类变量。
22.  applet paint()
23.  重没有用过的 strictfp
24.  final
final的方法必须实现,不存在abstract的final方法。
25.  内部类:
 
26.  关于 static class 的问题
27.  匿名类:
28.  使用 private 可以限制类本身访问其 static 的成员变量。
 
29.  关于 classpath
30.  关于接口
注意:不能使用在接口上的关键字:
31.  关于克隆
 
32.  关于 main 我所不知道的
用户线程和守护线程:
33.  inner class
Ø         regular inner class(not static,not method-local, not anonymous)
class MyOuter {
       class MyInner { }
}
The inner class is still, in the end, a separate class, so a separate class file is generated for it. But the inner class file isn't accessible to you in the usual way.
The only way you can access the inner class is through a live instance of the outer class!
34.  AWT 的结构
Ø         Component:定义处理事件,定位窗口、调整窗口、更新显示、设置或获取前景色和背景色和字体。
n         paint():在组件需要重画的时候负责绘制工作,包括第一次显示、组件变得模糊、大小调整、被覆盖、被覆盖部分重新要显示等。
u       应该在paint编写所有绘制代码
u       不要在paint中计算,会降低绘制速度
u       不要进行其他影响绘制速度的操作
u       paint使用一个Graphics的实例,该实例是一个平台相关的上下文。Graphics是抽象类。
n         repaint():显示重绘制、强行重绘制。
u       不带参数的repaint不会立即重绘,只是发出重绘请求。
u       repaint可能导致调用update(),也可能不调用update,而只是paint(如applet大小改变时)。这根据实际情况来决策。
l         update()做三件事
n         填充背景色,以覆盖原色和清除包含的component。这回引起屏幕的闪烁。可以直接调用父类的update,也可以根据情况override update(),以消除闪烁。
u       如,applet中交互显示相同大小的图片,很容易产生闪烁。这时,可以去除update中填充背景色的工作,而直接从设置前景色开始。这样可以消除闪烁。
n         设定当前绘制色为前景色
n         调用paint。
Ø         container:添加到container的组件被记录到列表中,如果没有指明就会添加到列表尾。布局管理器会根据列表中组件的顺序来工作。
Ø         Panel:继承了Container几乎所有功能,不含标题、边界。是Applet的超类。当界面被导向小程序,它会在panel上绘制。
Ø         Frame通常不在applet中创建,如果创建会有警告。
 
35.  布局管理器
当容器或其中的组件发生变化的时候,容器会先调用invalidate(),然后是validate()。Validate调用layout()方法,该方法调用容器的布局管理器的layoutContainer()。其他一些情况下,容器也会调用其布局管理器的方法,如下所示:
Ø         FlowLayout:
n        
n        
Ø         BorderLayout
n        
n        
n         North和South是横贯整个屏幕的
n         一个问题:一个区域不能添加多个组件,只会显示最后被添加的那个。
n         好处是:确保组件间的关系不变。
n        
u      
Ø         GridLayout
n        
n         当组件数超过定义是规定的行数*列数时,自动扩展列数。
Ø         关于默认布局管理器
36.  关于 String
Ø         经验表明,String很容易在程序影响性能。认真对待String很重要。
Ø         jvm为String保留一个特殊区域, String constant pool。当 编译器遇到一个 String literal,就会在pool中查找是否存在相同的String。有就直接引用,否则创建新String。并且,这也是 String 被对待为不可变的对象的原因。例子如下:
n         String s = new String("abc"); // creates two objects,
                               // and one reference variable
因为new,jvm在内存(非String constant pool区)创建一个String对象,s指向该引用。但是,同时根据规定literal“abc”被放到pool。
Ø         String 常用方法
public char charAt(int index)Returns the character located at the specified index
public String concat(String s)  Appends one String to the end of another ( "+" also works)
public boolean equalslgnoreCase(String s)Determines the equality of two Strings, ignoring case
public int length() Returns the number of characters in a String
public String replace(char old, char new) Replaces occurrences of a character with a new character
public String Substring(int Begin) , public String substring(int begin, int end) Returns a part of a String
public String toLowerCase() This  Returns a String with uppercase characters converted
public String toString() Returns the value of a String
public String toUpperCase() Returns a String with lowercase characters converted
public String trim()Removes whitespace from the ends of a String
Ø         StringBuffer是线程安全的
Ø         StringBuilder不是线程安全的,java5加入的,建议没有线程问题的时候使用,因为速度比StringBuffer快。
37.  Date Calendar Locale
Table 6-2: Common Use Cases When Working with Dates and Numbers
Use Case
Steps 1
Get the currect date and time
  1. Create a Date: Date d = new Date();
  2. Get its value: String s = d.toString();
Get an object that lets You perform date and time calculations in your locale.
  1. Create a Calender:
  2. Calender c = Calender.getInstance();
  3. Use c.add(...) and c.roll(...) to perform date and time manipulations.
Get an object that lets you perform date and time calculations in a different locale.
  1. Create a Locale:
  2. Locale loc = new Locale (language); or
  3. Locale loc = new Locale (language, country);
  4. Create a Calendar for that locale:
5.           Calendar c = Calendar.getInstance(loc)
  1. Use c.add(...) and c.roll(...) to perform date and time manipulations.
Get an object that lets you perform date and time calculations, and then format it for output in different locales with different date styles.
  1. Create a Calendar:
2.           Calendar c = Calendar.getInstance();
  1. Create a Locale for each location:
4.           Locale loc = new Locale(...);
  1. Convert your Calendar to a Date:
6.           Date d = c.getTime();
  1. Create a DateFormat for each Locale:
8.           DateFormat df = DateFormat.getDateInstance (style, loc);
  1. Use the format() method to create formatted dates:
10.       String s = df.format(d);
Get an object that lets you format numbers or currencies across many different locales.
  1. Create a Locale for each location:
2.           Locale loc = new Locale(...);
  1. Create a NumberFormat:
NumberFormat nf = NumberFormat.getInstance(loc); -or- NumberFormat nf = NumberFormat.getCurrencyInstance(loc) ;
3.Use the format() method to create formatted output:
String s = nf.format(someNumber);
 
Table 6-3: Instance Creation for Key java.text and java.util Classes
Class
Key Instance Creation Options
util.Date
new Date();
new Date(long millisecondsSince010170);
util.Calendar
Calendar.getInstance();
Calendar.getInstance(Locale);
util.Locale
Locale.getDefault();
new Locale(String language);
new Locale(String language,String country);
text.DateFormat
DateFormat.getInstance();
DateFormat.getDateInstance();
DateFormat.getDateInstance(style);
DateFormat.getDateInstance(style, Locale);
text.NumberFormat
NumberFormat.getInstance()
NumberFormat.getInstance(Locale)
NumberFormat.getNumberInstance()
NumberFormat.getNumberlnstance(Locale)
NumberFormat.getCurrencyInstance()
NumberFormat.getCurrencyInstance(Locale)
 
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值