您的位置:

Java学习笔记

Java是一种跨平台的编程语言,由于其特有的“一次编写,到处运行”的特点,在当今软件开发领域应用十分广泛。而廖雪峰Java教程从Java的基础入手,逐步深入到高级应用,是学习Java的好帮手。

一、Java基础

1、变量、数据类型

public class HelloWorld {
    public static void main(String[] args) {
        int x = 5;
        double y = 3.14;
        String s = "Hello World!";
        System.out.println(x);
        System.out.println(y);
        System.out.println(s);
    }
}

2、数组、字符串

public class Array {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        String s = "Hello World!";
        System.out.println(arr[1]);
        System.out.println(s.substring(0, 5));
    }
}

3、流程控制语句

public class FlowControl {
    public static void main(String[] args) {
        int score = 80;
        if (score >= 90) {
            System.out.println("优秀");
        } else if (score >= 80) {
            System.out.println("良好");
        } else if (score >= 60) {
            System.out.println("及格");
        } else {
            System.out.println("不及格");
        }
    }
}

二、面向对象编程

1、类、对象

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void sayHello() {
        System.out.println("Hello, my name is " + name + ", I'm " + age + " years old.");
    }
}

public class Test {
    public static void main(String[] args) {
        Person p = new Person("Tom", 20);
        p.sayHello();
    }
}

2、继承、多态

public abstract class Animal {
    public abstract void shout();
}

public class Dog extends Animal {
    @Override
    public void shout() {
        System.out.println("wangwang");
    }
}

public class Cat extends Animal {
    @Override
    public void shout() {
        System.out.println("miaomiao");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a1 = new Dog();
        Animal a2 = new Cat();
        a1.shout();
        a2.shout();
    }
}

3、接口、内部类

public interface Shape {
    double area();
}

public class Circle implements Shape {
    private double r;

    public Circle(double r) {
        this.r = r;
    }

    @Override
    public double area() {
        return Math.PI * r * r;
    }

    public static class InnerClass {
        public void sayHi() {
            System.out.println("Hi");
        }
    }
}

public class Test {
    public static void main(String[] args) {
        System.out.println(new Circle(5).area());
        new Circle.InnerClass().sayHi();
    }
}

三、异常处理

1、异常分类

public class ExceptionTest {
    public static void main(String[] args) {
        int[] arr = new int[3];
        try {
            arr[4] = 5;
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界");
        }
    }
}

2、自定义异常

public class MyException extends Exception {
    public MyException(String message) {
        super(message);
    }
}

public class ExceptionTest {
    public static void main(String[] args) {
        try {
            throw new MyException("发生了自定义异常");
        } catch (MyException e) {
            System.out.println(e.getMessage());
        }
    }
}

3、finally语句块

public class ExceptionTest {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("test.txt");
            //...
        } catch (FileNotFoundException e) {
            System.out.println("文件不存在");
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

四、Java IO及文件操作

1、输入输出流

public class FileCopy {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("src.txt");
            fos = new FileOutputStream("dest.txt");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2、字符输入输出流

public class FileCopy {
    public static void main(String[] args) {
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
            isr = new InputStreamReader(new FileInputStream("src.txt"), "UTF-8");
            osw = new OutputStreamWriter(new FileOutputStream("dest.txt"), "UTF-8");
            char[] buffer = new char[1024];
            int len;
            while ((len = isr.read(buffer)) != -1) {
                osw.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

3、文件操作

public class FileOperation {
    public static void main(String[] args) {
        File dir = new File("/Users/liuyang/Desktop");
        if (!dir.exists() || !dir.isDirectory()) {
            System.out.println("不存在该目录");
            return;
        }
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                System.out.println(file.getName());
            }
        }
    }
}
本文从Java基础、面向对象编程、异常处理和Java IO及文件操作四个方面对于廖雪峰Java教程做了详细的阐述。Java语言以其跨平台的特点和庞大的开发社区在软件开发领域拥有广泛的应用。掌握Java编程,对于编程能力的提升和职业发展都有着重要的作用。