⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scjp 的一些笔记.htm

📁 想考SCJP国际认证的必看这文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
                  &nbsp; System.out.print("exception ");<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; 
                  }<BR>4<BR>Catch处理最接近的异常,若在函数中有throw关键字抛出异常,则在本函数中有catch捕获到,则处理,不然就调用的时候被处理。如:<BR>public 
                  class TestEx1<BR>{<BR>&nbsp; &nbsp; &nbsp; &nbsp; static void 
                  test() throws RuntimeException<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  try<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("test 
                  ");<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; throw new 
                  RuntimeException();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; } catch (Exception ex)<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; System.out.print("exception ");<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; }<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; public static void 
                  main(String[] args)<BR>&nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; test();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; } catch (RuntimeException ex)<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; System.out.print("runtime ");<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  System.out.print("end ");<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  }<BR>}<BR>Print result: test exception 
                  end<BR>5<BR>注意空指针异常,代码如:<BR>package exception;<BR><BR>public 
                  class TestEx2<BR>{<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  /**<BR>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;* @param 
                  args<BR>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;*/<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; public static void main(String[] 
                  args)<BR>&nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ClassA a = new 
                  ClassA();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; a.methodA();<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  }<BR><BR>}<BR><BR>class ClassA<BR>{<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; public void methodA()<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  ClassB classB = new ClassB();<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; classB.getValue();<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; }<BR>}<BR><BR>class ClassB<BR>{<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; // public ClassC classC = new 
                  ClassC();<BR>&nbsp; &nbsp; &nbsp; &nbsp; // classC = new 
                  ClassC();<BR>&nbsp; &nbsp; &nbsp; &nbsp; public ClassC 
                  classC;<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; public String 
                  getValue()<BR>&nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 
                  classC.getValue();<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  }<BR>}<BR><BR>class ClassC<BR>{<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  public String value;<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; public 
                  String getValue()<BR>&nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = 
                  "ClassB";<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; return value;<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  }<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>&nbsp; &nbsp; &nbsp; &nbsp; public void 
                  count(int i)<BR>&nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  count(++i);<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR><BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; public static void main(String[] 
                  args)<BR>&nbsp; &nbsp; &nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ClassC c = new 
                  ClassC();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; c.count(3);<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  }<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&amp;page=1&amp;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&amp;page=1#pid71960', '已经复制到剪贴板')" 
                  href="http://www.spoto.net/bbs/viewthread.php?tid=12957&amp;page=1&amp;extra=page%3D1###">#4</A></DIV>
                  <DIV class=right style="PADDING-TOP: 5px"><A 
                  href="http://www.spoto.net/bbs/magic.php?action=user&amp;pid=71960" 
                  target=_blank>使用道具</A>&nbsp;&nbsp;</DIV>
                  <DIV style="PADDING-TOP: 5px">发表于 2007-5-15 20:32&nbsp; <A 
                  href="http://www.spoto.net/bbs/profile-uid-4334.html" 
                  target=_blank>资料</A>&nbsp; <A title=allen_su的个人空间 
                  href="http://www.spoto.net/bbs/space-uid-4334.html" 
                  target=_blank>个人空间</A>&nbsp; <A 
                  href="http://www.spoto.net/bbs/pm.php?action=send&amp;uid=4334" 
                  target=_blank>短消息</A>&nbsp; <A id=ajax_buddy_3 
                  onclick="ajaxmenu(event, this.id)" 
                  href="http://www.spoto.net/bbs/memcp.php?action=buddylist&amp;newbuddyid=4334&amp;buddysubmit=yes" 
                  target=_blank>加为好友</A>&nbsp; </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&amp;tid=12957&amp;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>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  String s = "Fred";<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; s = s + "47";<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = s.substring(2, 
                  5);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  s = s.toUpperCase();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; return s.toString();<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; }<BR>答案是:3个<BR><BR>StringBuffer s = new 
                  StringBuffer("123456789"); <BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; s.delete(0,3).replace( 1,3, 
                  "24").delete(4,6); <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; 
                  System.out.println(s);<BR><BR>输出s为:4247。<BR><BR>10<BR>基本类型转换的优先级大于自动装箱<BR>public 
                  class Yikes {<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; public static 
                  void go(Long n) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; System.out.println("Long ");<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; }<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; public 
                  static void go(Short n) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Short 
                  ");<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR><BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; public static void go(int n) {<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  System.out.println("int ");<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  }<BR>public static void main(String[] args) {<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; short y = 
                  6;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  long z = 7;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; go(y);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; go(z);<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  }<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>&nbsp; &nbsp; &nbsp; &nbsp; private Tree tree = new 
                  Tree();<BR>&nbsp; &nbsp; &nbsp; &nbsp; public static void 
                  main(String[] args) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; Forest f = new Forest();<BR>&nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; FileOutputStream fs = new 
                  FileOutputStream("Forest.ser");<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  ObjectOutputStream os = new ObjectOutputStream(fs);<BR>&nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; os.writeObject(f);<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  os.close();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; } catch (Exception ex) {<BR>&nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  ex.printStackTrace();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                  &nbsp; &nbsp; &nbsp; }<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
                  }<BR>}<BR>class Tree 
                  {<BR>}<BR>输入结果为:java.io.NotSerializableException: 
                  serializable.Tree<BR>13&nbsp; &nbsp;这个题目怎么做??<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 + -