21xrx.com
2024-11-22 13:55:56 Friday
登录
文章检索 我的文章 写文章
Java代码大全:终极编程指南
2023-06-13 21:03:11 深夜i     --     --
Java 代码 初学者

Java作为一门常用的编程语言,它的应用范围十分广泛,涉及到Java的技术岗位从未减少过。在Java的编程过程中,我们经常需要使用许多编写的Java代码。对于初学者,他们往往会遇到如何编写Java代码的问题。因此,本文为大家介绍一份Java代码大全,作为终极的Java编程指南,以便初学者快速上手。

1、Java基础

- Hello World 示例代码:


public class HelloWorld {

  public static void main(String[] args) {

    System.out.println("Hello, World");

  }

}

- Java数据类型示例代码:


public class DataType {

  public static void main(String[] args)

    byte b = 10;

    short s = 100;

    int i = 1000;

    long l = 10000L;

    float f= 1.2f;

    double d = 1.234d;

    boolean bool = true;

    char c = 'A';

  

}

- Java运算符示例代码:


public class Operator {

  public static void main(String[] args) {

    int a = 10;

    int b = 20;

    int c = 30;

    System.out.println(a + b);

    System.out.println(b - a);

    System.out.println(a * b);

    System.out.println(b / a);

    System.out.println(c % a);

  }

}

2、Java面向对象

- 类与对象示例代码:


public class Person {

  String name;

  int age;

  String gender;

  public void eat() {

    System.out.println("Person is eating.");

  }

  public void sleep() {

    System.out.println("Person is sleeping.");

  }

  public static void main(String[] args) {

    Person person = new Person();

    person.name = "张三";

    person.age = 18;

    person.gender = "男";

    person.eat();

    person.sleep();

  }

}

- 继承与多态示例代码:


public class Animal {

  public void eat() {

    System.out.println("Animal is eating.");

  }

}

public class Dog extends Animal {

  public void eat() {

    System.out.println("Dog is eating.");

  }

}

public class Cat extends Animal {

  public void eat() {

    System.out.println("Cat is eating.");

  }

}

public class Test {

  public static void main(String[] args) {

    Animal animal1 = new Dog();

    Animal animal2 = new Cat();

    animal1.eat();

    animal2.eat();

  }

}

- 抽象类与接口示例代码:


public abstract class Animal {

  public abstract void eat();

  public abstract void sleep();

}

public interface Flyable{

  void fly();

}

public class Dog extends Animal {

  public void eat() {

    System.out.println("Dog is eating.");

  }

  public void sleep() {

    System.out.println("Dog is sleeping.");

  }

}

public class Bird extends Animal implements Flyable {

  public void eat() {

    System.out.println("Bird is eating.");

  }

  public void sleep() {

    System.out.println("Bird is sleeping.");

  }

  public void fly() {

    System.out.println("Bird is flying.");

  }

}

3、Java高级

- 多线程示例代码:


public class MyThread extends Thread{

  public void run(){

    System.out.println("Thread is running.");

  }

  public static void main(String[] args) {

    MyThread myThread = new MyThread();

    myThread.start(); // 调用start方法,执行run方法

  }

}

- 异常处理示例代码:


public class ExceptionTest {

  public static void main(String[] args) {

    try

      int a = 0;

      int b = 1 / a;

     catch (ArithmeticException e) {

      System.out.println("除数不能为0!");

    }

  }

}

- IO与文件操作示例代码:


public class FileTest {

  public static void main(String[] args) {

    File file = new File("test.txt");

    try {

      BufferedWriter bw = new BufferedWriter(new FileWriter(file));

      bw.write("hello,world");

      bw.flush();

      bw.close();

      BufferedReader br = new BufferedReader(new FileReader(file));

      String str = "";

      while ((str = br.readLine()) != null) {

        System.out.println(str);

      }

      br.close();

    } catch (IOException e) {

      e.printStackTrace();

    }

  }

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复