找到关于编程的34423 篇文章

在 Java 中使用布尔值停止线程

Vikyath Ram
更新于 2019年7月30日 22:30:24

824 次浏览

可以通过实现 Runnable 接口并覆盖 run() 方法来创建线程。然后可以创建一个 Thread 对象并调用 start() 方法。可以使用 Java 中的布尔值停止线程。当布尔值 stop 为 false 时,线程运行;当布尔值 stop 变为 true 时,线程停止运行。演示此功能的程序如下所示:示例类 ThreadDemo 扩展 Thread {    public boolean stop = false;    int i = 1;    public void run() {       while (!stop) {          try {     ... 阅读更多

Java 中的最小和最大优先级线程

Arushi
更新于 2019年7月30日 22:30:24

3K+ 次浏览

线程优先级决定了何时向线程提供处理器以及其他资源。可以使用 Thread 类的 setPriority() 方法更改它。Java 中有三个线程优先级的静态变量,即 MIN_PRIORITY、MAX_PRIORITY 和 NORM_PRIORITY。这些变量的值分别为 1、10 和 5。演示此功能的程序如下所示:示例 在线演示public class ThreadDemo extends Thread {    public void run() {       System.out.println("Running...");    }    public static void main(String[] args) {       ThreadDemo thread1 = new ThreadDemo();       ThreadDemo thread2 = new ... 阅读更多

更改 Java 中的线程优先级

Fendadis John
更新于 2019年7月30日 22:30:24

610 次浏览

可以通过实现 Runnable 接口并覆盖 run() 方法来创建线程。然后可以创建一个 Thread 对象并调用 start() 方法。线程优先级决定了何时向线程提供处理器以及其他资源。可以使用 Thread 类的 setPriority() 方法更改它。演示使用 Java 中的 setPriority() 方法更改线程优先级的程序如下所示:示例 在线演示public class ThreadDemo extends Thread {    public void run() {       System.out.println("Running...");    }    public static void main(String[] args) {       ThreadDemo thread1 ... 阅读更多

Java 中 final static 字段的初始化器

Rishi Raj
更新于 2019年7月30日 22:30:24

895 次浏览

final static 字段变量是常量变量。只有一个此变量的副本可用。必须显式初始化 final static 字段变量,因为 JVM 不提供其默认值。此外,此变量不能重新初始化。演示使用静态初始化块初始化 final static 字段变量的程序如下所示:示例 在线演示public class Demo {    final static int num;    static {       System.out.println("Running static initialization block.");       num = 6;    }    public static void main(String[] args) {     ... 阅读更多

获取 Java 中 Integer 的哈希码

Arushi
更新于 2019年7月30日 22:30:24

2K+ 次浏览

可以使用 Java 中的 hashCode() 方法获取 Integer 的哈希码。此方法不接受任何参数,它返回 Integer 对象的哈希码值。演示 Java 中 hashCode() 方法的程序如下所示:示例 在线演示import java.lang.*; public class Demo {    public static void main(String args[]) {       Integer i = new Integer(60);       System.out.println("The integer value is: " + i);       System.out.println("The Hashcode for the above value is: " + i.hashCode());    } }输出The integer value is: 60 The Hashcode for the above ... 阅读更多

在 Java 中获取字符串的哈希码

Fendadis John
更新于 2019年7月30日 22:30:24

213 次浏览

hashCode() 方法用于获取字符串的哈希码。此方法不接受任何参数,因为它是一个默认方法,并且它返回一个哈希码值。演示 Java 中 hashCode() 方法的程序如下所示:示例 在线演示import java.io.*; public class Demo {    public static void main(String args[]) {       String str = new String("The sky is blue");       System.out.println("The string is: " + str);       System.out.println("The Hashcode for the string is: " + str.hashCode());    } }输出The string is: The sky is blue The ... 阅读更多

在 Java 垃圾回收中使用 finalize() 方法

Vikyath Ram
更新于 2019年7月30日 22:30:24

1K+ 次浏览

当垃圾收集器确定不再对特定对象进行引用时,垃圾收集器就会在该对象上调用 finalize() 方法。finalize() 方法不需要参数,也不返回值。演示 Java 中 finalize() 方法的程序如下所示:示例 在线演示import java.util.*; public class Demo extends GregorianCalendar {    public static void main(String[] args) {       try {          Demo obj = new Demo();          System.out.println("The current time is: " + obj.getTime());          obj.finalize();       ... 阅读更多

在 Java 类中重写 toString() 方法

Rishi Raj
更新于 2019年7月30日 22:30:24

5K+ 次浏览

可以使用 Java 中的 toString() 方法获取对象的字符串表示形式。重写此方法以便可以返回对象值。演示此功能的程序如下所示:示例 在线演示class Student {    private int rno;    private String name;    public Student(int r, String n) {       rno = r;       name = n;    }    public String toString() {       return rno + " " + name;    } } public class Demo {    public static void main(String[] args) {       Student ... 阅读更多

在 Java 中使用静态导入导入 Math 类中的 sqrt() 和 pow() 方法

Fendadis John
更新于 2019年7月30日 22:30:24

814 次浏览

静态导入意味着如果类的字段和方法定义为 public static,则可以在代码中使用它们而无需指定它们的类。java.lang 包中的 Math 类方法 sqrt() 和 pow() 是静态导入的。演示此功能的程序如下所示:示例 在线演示import static java.lang.Math.sqrt; import static java.lang.Math.pow; public class Demo {    public static void main(String args[]) {       double num = 4.0;       System.out.println("The number is: " + num);       System.out.println("The square root of the above number is: " + sqrt(num));       ... 阅读更多

在 Java 中静态导入 Math 类方法

Arushi
更新于 2019年7月30日 22:30:24

18K+ 次浏览

静态导入意味着如果类的字段和方法定义为 public static,则可以在代码中使用它们而无需指定它们的类。java.lang 包中的 Math 类方法 sqrt() 是静态导入的。演示此功能的程序如下所示:示例 在线演示import static java.lang.Math.sqrt; public class Demo {    public static void main(String[] arg) {       double num = 36.0;       System.out.println("The number is: " + num);       System.out.println("The square root of the above number is: " + sqrt(num));    } }输出The number is: 36.0 The square root ... 阅读更多

广告
© . All rights reserved.