服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

scjp认证考试复习笔记

     希望总结的这些知识点会对准备通过scjp考试的朋友有所帮助!难免有错误的地方欢迎大家批评指正。
系列之一

  1. thread类是在java.lang包中定义的,sleep()和yield()是thread的静态方法。但是wait()、notify()、notifyall()是object类的实例方法。要让一个线程启动要调用start()方法,但是具体什么时间线程开始运行是跟操作系统和虚拟机相关的。
  2. 任何时候都要记得string类是不可修改的,但是不要认为调用某些方法一定会创建一个新的对象。当调用string的实例的concat()、trim()、touppercase()、tolowercase()方法时,如果调用后实例没有发生变化,那么将不会创建对象。运行下面的程序得到四个true就说明了这一点。
    public class teststring
    {
     public static void main(string[] args)
     {
      teststring ts = new teststring();
      ts.test(); 
     } 
     
     public void test()
     {
      string lower = "mingjava";
      string upper = "mingjava";
      
      system.out.println(lower == lower.tolowercase());
      system.out.println(upper == upper.touppercase());
      system.out.println(lower == lower.trim());
      system.out.println(lower == lower.concat(""));
     }
    }
  3. 所有的异常都继承自throwable,它的两个分支是error和exception。error和runtimeexcepton合起来称作uncheckedexception。其他的异常称作checkedexception。error和runtimeexception是不需要进行捕捉的,error我们无能为力,但是runtimeexception的出现就是你的错误,你应该检查你的代码。如果方法中没有用throws声明checkedexception,那么你进行捕捉的话同样会出现编译错误!
  4. 记得数组的下标从开始0开始计数的,有时候会在main(string[] args)里面考察你这个问题
  5. 内部类对它的enclosing class有无限的访问权限,甚至可以访问他的private类型的变量。但是如果内部类想访问局部变量的时候,那么这个局部变量必须为final的。例如
    public class test
    {
     public void dosome()
     {
      final int j = 100;  
       
      class innerclass
      {
      
          public void print()
          {
              system.out.println(j);
          } 
       } 
     }
     
    }
  6. 方法重载的时候,返回值和抛出异常是不能区分同样名字的方法的。起作用的是方法参数的类型和个数
  7. 注意>>是带符号的移位,>>>是右边补零的移位。看下面的例子

    public class myclass
    {
     public static void main(string argv[])
     {
      int i = 0x80000000;
      
      system.out.println(i);
      system.out.println("after >>"+(i>>1));
      system.out.println("after >>>"+(i>>>1));
     }

    }
  8. arrayindexoutofboundsexception, nullpointerexception, arithmeticexception, numberformatexception.classcastexception都是常见的runtimeexception,不是complie exception
  9. string s = "xxxx" 这样的声明s是可以复用的,但是new string("xxx")是一定创建一个新的对象的。比较一下system.out.println("string" =="string")和system.out.println(new string("string")==new string("string"))的结果吧
  10. 在遇到a++和++a的时候一定要小心
  11. 短路运算符||和&&一定要弄明白,如果短路运算符前面的条件为真的话那么后面的语句就会被丢弃不会被执行
  12. transient只能修饰变量,并且它不能和final static一起使用
  13. 如果你继承thread你可以不实现public void run()方法,但是你实现runnable接口的话必须实现这个方法
  14. 如果方法前面不加限制修饰符,那么它是在包内可见的。 protected的意思是包内可见并且子类可见。
  15. string覆盖了object的equals方法,这个方法比较的是string实例的内容,==比较的是地址。
  16. byte, double, float, integer, long, short 继承自number,character、boolean继承自object。
  17. 有时间应该看看几个包装类的构造函数考试的时候会涉及到
  18. 这样的声明是正确的int[][] array = new int[3][];
  19. 你应该很清楚switch语句的结构,并且switch()你能接受int char的参数
  20. 如果你覆盖了public boolean equals(object object)方法那么你应该同时覆盖public int hashcode()
  21. instanceof 既可以用在class也可以用在interface

系列之二

  1. 调用线程实例的start()方法他的run()方法会运行,你直接调用run()它也会运行。
  2. java中方法参数是按值传递的,最简单的情况就是传递基本数据类型。他不能更改传递给他的参数的,例如下面的程序会输出0
    public class myclass
    {
     public static void main(string argv[])
     {
                    myclass mc = new myclass();
                    int i = 0;
                    mc.increment(i);
                    system.out.println(i);
            }
          void increment(int i)
     {
                    i++;
            }
    }
  3. throws用在方法的声明中表示方法可能出现异常,throw表示抛出一个异常。不要混淆
  4. finally中抛出异常可以导致try中的异常被忽略。看下面的例子
    public class test2
    {
     public static void main(string[] args) throws exception
     {
      try
      {
       f(); 
      }
     
      finally
      {
      
       m();
       
      }
      
     }
     
     public static void f() throws myexception
     {
      throw new myexception(); 
     }  
     
     public static void m() throws myexception2
     {
      throw new myexception2(); 
     } 
    }
    class myexception extends exception
    {}
    class myexception2 extends exception
    {}
  5.    assert exp1:exp2这种形势中 exp2如果是方法的话必须有返回值,可以是基本数据类型也可以是任何object但是不可以void。
  6. 如果你提供了一个类的构造器那么编译器就不会提供无参数的构造器给你了,往往在扩展类的时候会出现问题,下面的例子就不能通过编译
    class base
    {
            base(int i)
     {
      system.out.println("base");
     }
    }
    class derived extends base
    {
     public static void main(string argv[])
     {
      derived d = new derived();
     }
     
     void derived()
     {
      system.out.println("derived");
     }
    }
  7. 构造器是不能被覆盖的
  8. native的方法是不能有body的,因为他们通常用c/c++来实现
  9. public class myclass
    {
     private static int x = getvalue();
     private static int y = 5;
     
     private static int getvalue()
     { 
      return y;
     }
     
     public static void main(string args[])
     {
      system.out.println(x); 
       system.out.println(y);
     } 
    }
    这段程序的输出会是0,5。因为首先x,y都会设置为默认值0,当给他们赋值的时候y还是0,所以返回的y付给了x,所以x=0,然后给y赋值=5
  10. 引用声明为final的表示他不能再指向其他的对象,但是不说明引用的内容不能被修改
  11. 数组声明的时候,如果是基本数据类型的初始化为0或者false,如果是对象的初始化为null
  12. string = string+char是合理的 char = char+string是不合理的
  13. stringbuffer sb = new stringbuffer("haha"); string s1 = "haha";system.out.println(s1.equals(sb));输出会是false的。不要被迷惑,不同类型的比较输出是false string和stringbuffer在层次上没有关系
  14. static的方法中不能使用this
  15. &可以用在两个int之间表示bit与操作  &&不能用在两个int之间 要在布尔型之间使用

系列之三

  1. java中的boolean类型不能和int类型进行转换的,不要认为true为1、false为0
  2. 局部变量在使用前一定要先初始化
  3. class中如果含有抽象方法那么必须要声明为抽象类,如果没有抽象方法也可以声明为抽象类,接口中的方法都不能在接口中实现。
  4. length是数组的一个变量而不是方法,通常我们计算collection的时候要用size()方法
  5. boolean[] xx = new boolean[12]和boolean[] yy = new boolean[12]的区别是:前者是object的数组,并被初始化为null后者是基本数据类型数组被初始化为false
  6. java.util中的类vector和hashtable是线程安全的
  7. 编译器为你提供的默认构造器的访问控制符是和他所属类的访问控制符一致的
  8. 0。0==-0。0是返回true的
  9. 本地变量不能声明为static
  10. class base
    {
     int i = 99;
     public void amethod()
     {
             system.out.println("base.amethod()");
            }
            base()
     {
                  amethod();
            }
    }
    public class derived extends base
    {
     int i = -1;
           
     public static void main(string argv[])
     {
             base b = new derived();
             system.out.println(b.i);
             b.amethod();
            }
     
            public void amethod()
     {
                    system.out.println("derived.amethod()");
            }
    }
    上面的程序输出为derived.amethod() 99 derived.amethod(),当base b = new derived()执行的时候首先去执行base的构造器,但是由于b的实际类型是derived所以b的amethod在构造器内被调用。变量是和声明类型相关的所以输出9,方法(静态方法除外)是跟实际类型相关的输出derived.amethod()
  11. 切记数组的下标从0开始
  12.  stringbuffer sb = new stringbuffer("ha");
      system.out.println(sb+1);这样的话是不能通过编译的,因为+并不能用在stringbuffer和int之间,只能在string和原始数据类型之间
  13. 不同包装类的比较会返回false例如 new integer(4).equals(new long(4));如果是同类型那么equals比较的就是他们的值了
  14. 含有assert的程序如果执行的时候不用-ea那么相关的语句会被忽略

扫描关注微信公众号