一、Java基础语法
1、Java数据类型(byte、short、int、long、float、double、boolean、char)。
public class DataTypeDemo {
public static void main(String[] args) {
byte a = 10;
short b = 100;
int c = 1000;
long d = 10000;
float e = 1.2345f;
double f = 1.23456789;
boolean g = true;
char h = 'a';
}
}
2、Java关键字和标识符。
public class KeyWordDemo {
public static void main(String[] args) {
int class1 = 10;
int abstract = 100;
int num = class1 + abstract;
System.out.println(num);
}
}
3、Java注释(单行注释、多行注释、文档注释)。
/**
* 简单计算器类
*/
public class Calculator {
/**
* 加法运算
*
* @param a 加数1
* @param b 加数2
* @return 加法运算结果
*/
public static int add(int a, int b) {
return a + b;
}
}
二、Java面向对象编程
1、Java类与对象。
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 static void main(String[] args) {
Person person = new Person("Tom", 20);
person.sayHello();
}
}
2、继承与接口。
public class Animal {
public void eat() {
System.out.println("Animal eat.");
}
}
public interface Flyable {
void fly();
}
public class Bird extends Animal implements Flyable {
@Override
public void fly() {
System.out.println("Bird fly.");
}
}
3、多态。
public class Animal {
public void eat() {
System.out.println("Animal eat.");
}
}
public class Dog extends Animal {
@Override
public void eat() {
System.out.println("Dog eat.");
}
}
public class Cat extends Animal {
@Override
public void eat() {
System.out.println("Cat eat.");
}
}
public class Test {
public static void main(String[] args) {
Animal animal1 = new Dog();
Animal animal2 = new Cat();
animal1.eat();
animal2.eat();
}
}
三、Java IO流
1、字节流(InputStream、OutputStream)。
public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
2、字符流(Reader、Writer)。
public class CopyCharacters {
public static void main(String[] args) throws IOException {
FileReader inputStream = null;
FileWriter outputStream = null;
try {
inputStream = new FileReader("input.txt");
outputStream = new FileWriter("output.txt");
int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}
3、缓冲流(BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter)。
public class CopyBuffered {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream("input.txt"));
bos = new BufferedOutputStream(new FileOutputStream("output.txt"));
byte[] buffer = new byte[1024];
int length;
while ((length = bis.read(buffer)) != -1) {
bos.write(buffer, 0, length);
}
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}
}
}
四、Java异常处理
1、try-catch语句。
import java.io.*;
public class ReadFile {
public static void main(String[] args) {
try {
File file = new File("file.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("读取文件出错" + e);
}
}
}
2、throws关键字。
public class Test {
public static void main(String[] args) throws Exception {
throw new Exception("test");
}
}
五、Java集合框架
1、List(ArrayList、LinkedList)。
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list.get(0));
list.remove(0);
System.out.println(list.get(0));
}
}
2、Set(HashSet、LinkedHashSet)。
public class HashSetTest {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("C");
System.out.println(set.contains("A"));
set.remove("A");
System.out.println(set.contains("A"));
}
}
3、Map(HashMap、TreeMap、LinkedHashMap)。
public class HashMapTest {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
System.out.println(map.get("A"));
map.remove("A");
System.out.println(map.get("A"));
}
}
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/235892.html