Java编程基础笔记

整理Android开发相关知识,从JAVA基础开始。

JAVA编程

0x001 类中的变量

一个类可以包含以下类型变量:

  • 局部变量:在方法中定义的变量,生命周期为方法执行过程。
  • 成员变量/实例变量:在方法体外,类中定义的变量,创建对象的时候实例化。成员变量可以被类中的方法调用
  • 类变量:在方法体外,类中定义的变量,声明为static 类型
public class varible{
    static int a = 0;    //类变量
    int b = 1;            //成员变量
    public static void main(String[] args){
        int c = 2;        //局部变量
        system.out.println(c);
    }
}

0x002 源文件规则

一个源文件中仅有一个public类,可有多个非public类,源文件名与public类名一致

0x003 常量

常量是指声明为 public/privatefinalstatic 类型的变量,常量一般大写

0x004 类型转换

低 -----------------------------------------> 高

byte,short,char—> int —> long—> float —> double

可向 前/高 自动转换

0x005 static

静态变量:static 关键字用来声明独立于对象的静态变量,无论一个类实例化多少对象,它的静态变量只有一份拷贝。 静态变量也被称为类变量。局部变量不能被声明为 static 变量。

静态方法:static 关键字用来声明独立于对象的静态方法。静态方法不能使用类的非静态变量。静态方法从参数列表得到数据,然后计算这些数据。

0x006 final

final变量: 被 final 修饰的实例变量必须显式指定初始值。修饰常量。

final方法:类中的 final 修饰的方法不能被子类修改

final类:不能被继承

0x007 abstract

抽象类不能实例化,类中方法是抽象方法的一定声明为抽象类,不能同时被 finalabstract 修饰

抽象方法:继承抽象类的子类必须实现抽象父类的所有抽象方法,抽象方法没有具体的实现(除非该子类也是抽象类);不能被声明为 finalstaic

0x008 synchronized

声明的方法同一时间只能被一个线程访问

0x009 运算

Java中可以++和—操作

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = 0;
        c = a + b;
        System.out.println("c = a + b = " + c );
        c += a ;
        System.out.println("c += a  = " + c );
        c -= a ;
        System.out.println("c -= a = " + c );
        c *= a ;
        System.out.println("c *= a = " + c );
        a = 10;
        c = 15;
        c /= a ;
        System.out.println("c /= a = " + c );
        a = 10;
        c = 15;
        c %= a ;
        System.out.println("c %= a  = " + c );
        c <<= 2 ;
        System.out.println("c <<= 2 = " + c );
        c >>= 2 ;
        System.out.println("c >>= 2 = " + c );
        c >>= 2 ;
        System.out.println("c >>= 2 = " + c );
        c &= a ;
        System.out.println("c &= a  = " + c );
        c ^= a ;
        System.out.println("c ^= a   = " + c );
        c |= a ;
        System.out.println("c |= a   = " + c );
    }
}

0x010 增强for语句

for(声明语句 : 表达式)
{
   //code...
}


public class Test {
   public static void main(String args[]){
      int[] numbers = {10, 20, 30, 40, 50};

      for(int x : numbers ){
         System.out.print( x );
         System.out.print(",");
      }
      System.out.print("\n");
      String[] names ={"James", "Larry", "Tom", "Lacy"};
      for( String name : names ) {
         System.out.print( name );
         System.out.print(",");
      }
   }
}

Output:
    10,20,30,40,50,
    James,Larry,Tom,Lacy,

0x011 String

StringBufferStringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。StringBuilder 有速度优势,多数使用 StringBuilder 类。如果要求线程安全,必须使用 StringBuffer 类。

public class Test{
  public static void main(String args[]){
    StringBuffer sBuffer = new StringBuffer("https:");
    sBuffer.append("//");
    sBuffer.append("blog.dyboy");
    sBuffer.append(".cn");
    System.out.println(sBuffer);
  }
}

Output:
    https://blog.dyboy.cn

0x012 文件读写

输入流对象:InputStream f = new FileInputStream("c:/1.txt");

File f = new File("c:/1.txt");        //文件对象
InputStream out = new FileInputStream(f);

// example
import java.io.*;

public class Test{
   public static void main(String args[])throws IOException{

      DataInputStream in = new DataInputStream(new FileInputStream("test.txt"));
      DataOutputStream out = new DataOutputStream(new  FileOutputStream("test1.txt"));
      BufferedReader d  = new BufferedReader(new InputStreamReader(in));

      String count;
      while((count = d.readLine()) != null){
          String u = count.toUpperCase();        // 大写
          System.out.println(u);
          out.writeBytes(u + "  ,");
      }
      d.close();
      out.close();
   }
}

0x012 异常

异常错误

import java.io.*;
public class ExcepTest{
  public static void main(String args[]){
    int a[] = new int[2];
    try{
       System.out.println("Access element three :" + a[3]);
    }catch(ArrayIndexOutOfBoundsException e){
       System.out.println("Exception thrown  :" + e);
    }
    finally{
       a[0] = 6;
       System.out.println("First element value: " +a[0]);
       System.out.println("The finally statement is executed");
    }
  }
}

0x013 super与this

super 是父类对象的引用,this 引用当前对象

class Animal {
  void eat() {
    System.out.println("animal : eat");
  }
}

class Dog extends Animal {
  void eat() {
    System.out.println("dog : eat");
  }
  void eatTest() {
    this.eat();   // this 调用自己的方法
    super.eat();  // super 调用父类方法
  }
}

public class Test {
  public static void main(String[] args) {
    Animal a = new Animal();
    a.eat();
    Dog d = new Dog();
    d.eatTest();
  }
}

0x014 继承关系

Java 中可以单继承、多重继承,不能实现多继承

implements 在类继承接口中使用

类与类之间是继承(extends),类与接口之间是实现(implements),接口与接口之间是继承

public 的类。方法、接口可以被其他类访问;protect 可以被子类访问,同包下可以被子类访问|重写,不同包下子类只能访问该保护方法;private 修饰变量和方法,私有方法可以被继承,但不能访问,可以通过父类提供的公有方法来获取私有变量,只有本类才可以直接访问私有变量和方法。

public interface A {
    public void eat();
    public void sleep();
}

public interface B {
    public void show();
}

public class C implements A,B {

}

0x015 重写规则

  • 参数列表必须完全与被重写方法的相同;
  • 返回类型必须完全与被重写方法的返回类型相同;
  • 访问权限不能比父类中被重写的方法的访问权限更低。例如:如果父类的一个方法被声明为public,那么在子类中重写该方法就不能声明为protected
  • 父类的成员方法只能被它的子类重写。
  • 声明为final的方法不能被重写。
  • 声明为static的方法不能被重写,但是能够被再次声明。
  • 子类和父类在同一个包中,那么子类可以重写父类所有方法,除了声明为privatefinal的方法。
  • 子类和父类不在同一个包中,那么子类只能够重写父类的声明为public和protected非final方法。
  • 重写的方法能够抛出任何非强制异常,无论被重写的方法是否抛出异常。但是,重写的方法不能抛出新的强制性异常,或者比被重写方法声明的更广泛的强制性异常,反之则可以。
  • 构造方法不能被重写。
  • 如果不能继承一个方法,则不能重写这个方法。

override与overload

0x016 多态

多态是同一行为有不同的表现,同一个接口根据不同的实例而执行不同的操作

public class Test {
    public static void main(String[] args) {
      show(new Cat());  // 以 Cat 对象调用 show 方法
      show(new Dog());  // 以 Dog 对象调用 show 方法

      Animal a = new Cat();  // 向上转型
      a.eat();               // 调用的是 Cat 的 eat
      Cat c = (Cat)a;        // 向下转型
      c.work();        // 调用的是 Cat 的 work
  }

    public static void show(Animal a)  {
      a.eat();
        // 类型判断
        if (a instanceof Cat)  {  // 猫做的事情
            Cat c = (Cat)a;
            c.work();
        } else if (a instanceof Dog) { // 狗做的事情
            Dog c = (Dog)a;
            c.work();
        }
    }
}

abstract class Animal {
    abstract void eat();
}

class Cat extends Animal {
    public void eat() {
        System.out.println("吃鱼");
    }
    public void work() {
        System.out.println("抓老鼠");
    }
}

class Dog extends Animal {
    public void eat() {
        System.out.println("吃骨头");
    }
    public void work() {
        System.out.println("看家");
    }
}

// Output
吃鱼
抓老鼠
吃骨头
看家
吃鱼
抓老鼠

0x017 例子(菜鸟教程)

http://www.runoob.com/java/java-examples.html

发表评论 / Comment

用心评论~


Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/blog.dyboy.cn/content/templates/dyblog/footer.php:56) in /www/wwwroot/blog.dyboy.cn/include/lib/view.php on line 23