📄 scjp 的一些笔记.htm
字号:
System.out.print("exception ");<BR>
}<BR>
}<BR>4<BR>Catch处理最接近的异常,若在函数中有throw关键字抛出异常,则在本函数中有catch捕获到,则处理,不然就调用的时候被处理。如:<BR>public
class TestEx1<BR>{<BR> static void
test() throws RuntimeException<BR>
{<BR>
try<BR>
{<BR>
System.out.print("test
");<BR>
throw new
RuntimeException();<BR>
} catch (Exception ex)<BR>
{<BR>
System.out.print("exception ");<BR>
}<BR>
}<BR><BR> public static void
main(String[] args)<BR> {<BR>
try<BR>
{<BR>
test();<BR>
} catch (RuntimeException ex)<BR>
{<BR>
System.out.print("runtime ");<BR>
}<BR>
System.out.print("end ");<BR>
}<BR>}<BR>Print result: test exception
end<BR>5<BR>注意空指针异常,代码如:<BR>package exception;<BR><BR>public
class TestEx2<BR>{<BR><BR>
/**<BR> * @param
args<BR> */<BR>
public static void main(String[]
args)<BR> {<BR>
ClassA a = new
ClassA();<BR>
a.methodA();<BR><BR>
}<BR><BR>}<BR><BR>class ClassA<BR>{<BR>
public void methodA()<BR>
{<BR>
ClassB classB = new ClassB();<BR>
classB.getValue();<BR>
}<BR>}<BR><BR>class ClassB<BR>{<BR>
// public ClassC classC = new
ClassC();<BR> // classC = new
ClassC();<BR> public ClassC
classC;<BR><BR> public String
getValue()<BR> {<BR>
return
classC.getValue();<BR>
}<BR>}<BR><BR>class ClassC<BR>{<BR>
public String value;<BR><BR> public
String getValue()<BR> {<BR>
value =
"ClassB";<BR>
return value;<BR>
}<BR>}<BR><BR><BR><BR><BR>6<BR>数组要注意初始化,然后才可以付值
如下代码是错误的:<BR>注意抛:ExceptionlnlnitializerError<BR>public class
Foo {<BR>static int[] a;<BR>static { a[0]=2; }<BR>public
static void main( String[] args)
{}<BR>}<BR>7<BR>注意益出错误:StackOverflowError<BR>public class
ClassC<BR>{<BR><BR> public void
count(int i)<BR> {<BR>
count(++i);<BR> }<BR><BR>
public static void main(String[]
args)<BR> {<BR>
ClassC c = new
ClassC();<BR>
c.count(3);<BR><BR>
}<BR><BR>}<BR>8 <BR>StringBuff
类是线程安全的<BR>可以专换成StringBuild、用StingBuild执行更快。</DIV><BR
style="CLEAR: both"><FONT
style="FONT: 9pt Tahoma, Verdana; COLOR: #313031"></FONT></TD></TR>
<TR>
<TD align=right><A onclick=scroll(0,0)
href="http://www.spoto.net/bbs/viewthread.php?tid=12957&page=1&extra=page%3D1###"><IMG
alt=顶部 src="scjp 的一些笔记.files/top.gif" border=0></A>
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV>
<DIV class=spaceborder style="MARGIN-BOTTOM: 4px; WIDTH: 100%">
<TABLE class=t_row cellSpacing=0 cellPadding=5 width="100%"
align=center><TBODY>
<TR style="HEIGHT: 100%">
<TD class=t_user vAlign=top width="18%"><A name=pid71960></A><A
class=bold href="http://www.spoto.net/bbs/space-uid-4334.html"
target=_blank>allen_su</A> <BR>
<DIV class=smalltxt>中级会员<BR><IMG alt="Rank: 4"
src="scjp 的一些笔记.files/star_level2.gif"><IMG alt="Rank: 4"
src="scjp 的一些笔记.files/star_level1.gif"><BR><BR>
<TABLE style="TABLE-LAYOUT: fixed; OVERFLOW: hidden" cellSpacing=0
cellPadding=0 width="95%" border=0>
<TBODY>
<TR>
<TD align=middle>
<DIV class=avatar style="WIDTH: 120px"><IMG height=120 alt=""
src="scjp 的一些笔记.files/4334_2007441013970215.jpg" width=120
border=0></DIV></TD></TR></TBODY></TABLE><BR>UID 4334<BR>精华 0<BR>积分
888<BR>帖子 81<BR>阅读权限 30<BR>注册 2006-10-22<BR>状态 离线 </DIV></TD>
<TD
style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px"
vAlign=top width="82%">
<TABLE class=t_msg cellSpacing=0 cellPadding=5 border=0>
<TBODY>
<TR>
<TD>
<DIV>
<DIV class="right t_number"><A class=bold title=复制帖子链接到剪贴板
onclick="setcopy('http://www.spoto.net/bbs/viewthread.php?tid=12957&page=1#pid71960', '已经复制到剪贴板')"
href="http://www.spoto.net/bbs/viewthread.php?tid=12957&page=1&extra=page%3D1###">#4</A></DIV>
<DIV class=right style="PADDING-TOP: 5px"><A
href="http://www.spoto.net/bbs/magic.php?action=user&pid=71960"
target=_blank>使用道具</A> </DIV>
<DIV style="PADDING-TOP: 5px">发表于 2007-5-15 20:32 <A
href="http://www.spoto.net/bbs/profile-uid-4334.html"
target=_blank>资料</A> <A title=allen_su的个人空间
href="http://www.spoto.net/bbs/space-uid-4334.html"
target=_blank>个人空间</A> <A
href="http://www.spoto.net/bbs/pm.php?action=send&uid=4334"
target=_blank>短消息</A> <A id=ajax_buddy_3
onclick="ajaxmenu(event, this.id)"
href="http://www.spoto.net/bbs/memcp.php?action=buddylist&newbuddyid=4334&buddysubmit=yes"
target=_blank>加为好友</A> </DIV></DIV></TD></TR>
<TR>
<TD class=line style="PADDING-TOP: 10px" vAlign=top
height="100%">
<DIV style="FLOAT: right" align=right><A title="评分 0"
href="http://www.spoto.net/bbs/misc.php?action=viewratings&tid=12957&pid=71960"></A></DIV>
<DIV class=t_msgfont id=message71960>9<BR>String
的方法应用:<BR>Substring(int start,int
end)用于取得一个字符传的子串,<BR>toUpperCase() 把字符串变成大写<BR>replace(int
start, int end, String str)<BR>delete(int start, int
end)<BR>多少个对象生产,但这个方法被调用:<BR>public String makinStrings()
{<BR>
String s = "Fred";<BR>
s = s + "47";<BR>
s = s.substring(2,
5);<BR>
s = s.toUpperCase();<BR>
return s.toString();<BR>
}<BR>答案是:3个<BR><BR>StringBuffer s = new
StringBuffer("123456789"); <BR>
s.delete(0,3).replace( 1,3,
"24").delete(4,6); <BR>
System.out.println(s);<BR><BR>输出s为:4247。<BR><BR>10<BR>基本类型转换的优先级大于自动装箱<BR>public
class Yikes {<BR><BR> public static
void go(Long n) {<BR>
System.out.println("Long ");<BR>
}<BR><BR> public
static void go(Short n) {<BR>
System.out.println("Short
");<BR> }<BR><BR>
public static void go(int n) {<BR>
System.out.println("int ");<BR>
}<BR>public static void main(String[] args) {<BR>
short y =
6;<BR>
long z = 7;<BR>
go(y);<BR>
go(z);<BR>
}<BR>}<BR>输出为:int
Long<BR><BR><BR><BR>11<BR>比较java.io.BufferedWriter与
java.io.FileWriter ,BufferedWriter里的方法多了个readline(),<BR>12
Serializable<BR>注意,使用的类没有序列化的话,进行序列化,若没有序列化该类会在运行时抛出异常/代码如:<BR>import
java.io.*;<BR>public class Forest implements Serializable
{<BR> private Tree tree = new
Tree();<BR> public static void
main(String[] args) {<BR>
Forest f = new Forest();<BR>
try {<BR>
FileOutputStream fs = new
FileOutputStream("Forest.ser");<BR>
ObjectOutputStream os = new ObjectOutputStream(fs);<BR>
os.writeObject(f);<BR>
os.close();<BR>
} catch (Exception ex) {<BR>
ex.printStackTrace();<BR>
}<BR>
}<BR>}<BR>class Tree
{<BR>}<BR>输入结果为:java.io.NotSerializableException:
serializable.Tree<BR>13 这个题目怎么做??<BR>Question
101<BR>Assuming that the serializeBanana2() and the
deserializeBanana2() <BR>methods will correctly use Java
serialization and given:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -