字符串tostring
线程类字符串toString() (Thread Class String toString())
This method is available in java.lang.Thread.toString().
此方法在java.lang.Thread.toString()中可用。
This method is used to return the string representation of the thread including the thread name, thread priority, thread group.
此方法用于返回线程的字符串表示形式,包括线程名称,线程优先级,线程组。
This method returns a string representation of the thread that is easy to understand for end-user.
此方法返回线程的字符串表示形式,对于最终用户来说很容易理解。
This method does not return an exception.
此方法不返回异常。
Syntax:
句法:
String toString(){
}
Parameter(s):
参数:
We don't pass any object as a parameter in the method of the Thread.
我们不会在Thread方法中将任何对象作为参数传递。
Return value:
返回值:
The return type of this method is String, this method returns String representation of the thread.
该方法的返回类型为String ,此方法返回线程的String表示形式。
Java程序演示toString()方法的示例 (Java program to demonstrate example of toString() method)
/* We will use Thread class methods so we are importing
the package but it is not mandate because
it is imported by default
*/
import java.lang.Thread;
class MyThread extends Thread {
// Override run() method of Thread class
public void run() {
System.out.println("Thread Priority:" + Thread.currentThread().getPriority());
System.out.println("Thread Name :" + Thread.currentThread().getName());
System.out.println("Thread Group :" + Thread.currentThread().getThreadGroup());
}
}
class Main {
public static void main(String[] args) {
// Creating an object of MyThread and calling start() of Thread class
MyThread mt = new MyThread();
mt.start();
System.out.println("String representation of MyThread" + mt.toString());
}
}
Output
输出量
E:\Programs>javac Main.java
E:\Programs>java Main
String representation of MyThreadThread[Thread-0,5,main]
Thread Priority:5
Thread Name :Thread-0
Thread Group :java.lang.ThreadGroup[name=main,maxpri=10]
翻译自: https://www.includehelp.com/java/thread-class-string-tostring-method-with-example.aspx
字符串tostring