Java 简明教程
Java.lang package tutorial
Java - java.lang Package
Java.lang 包涵盖了 Java 编程语言设计的核心类。Integer、Boolean、Char 等包装类是 lang 包的一部分。我们不必明确导入 java.lang 包中的任何类,因为它们默认包含在内。此教程兼参考将通过简单实用的示例带你了解 java.lang 包中可用的所有方法。
Java.lang package contains the classes that are fundamental to the design of the Java programming language. Wrapper Classes like Integer, Boolean, Char etc are part of lang package. We’re not supposed to import any class of java.lang package explicitly as being included by default. This tutorial cum reference will take you through all the methods available in java.lang package using simple and practical example.
Importing java.util Package
作为 Java 的内置包,我们不需要下载任何外部库,该包及其所有类可以使用以下语法导入:
Being an inbuilt package of Java, we’re not required to download any external library for java.lang package and its all classes can be imported using following syntax:
import java.lang.*;
在这里,我们使用了 * 操作符从 java.lang 包中导入所有类,现在程序中可以使用任何类。对于特定类,例如 Thread,我们可以使用以下语法导入类:
Here we’ve used * operator to import all classes from java.lang package and now any class can be used in the program. In case of specific class, for example Thread, we can import a class using following syntax:
import java.lang.Thread;
作为 java 的默认包,不需要导入 java.lang 包,并且无需使用 import 语句就可以访问 lang 级别上的所有类。
Being a default package of java, import of java.lang package is not required and all classes at lang level are accessible without using import statements as well.
Why java.lang Package is used in Java Programs
Java.lang 包中的类包含基本类,例如包装类、线程操作类、字符串操作类、数学操作类等。以下列表显示了 java.util 包中类的一些类别。
Java.lang package classes contains fundamental classes like wrapper classes, classes for thread operations, classes for String manipulation, classes for mathematics operations etc. Following list shows some of the categories of classes of java.util package.
-
Wrapper Classes − java.lang package contains wrapper classes like Integer, Boolean, Float, Double etc for primitive values. These wrapper classes provides many useful methods and can be used as objects where primitive value is not to be used.
-
UI Components −java.lang.awt and java.lang.swing based components are core UI components of Java and provides event handling.
-
Mathematical operations − Math and StrictMath classes are rich with mathematical operations.
-
Thread operations −Thread, ThreadLocal, ThreadGroup etc. are important classes for multi-threading programs.
Important classes in java.lang Package
以下是 java.lang.package 中重要类的列表:
Following is the list of important classes in java.lang.package:
-
Boolean − The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.
-
Byte − The Byte class class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.
-
Character − The Character class offers a number of useful class (i.e., static) methods for manipulating characters.
-
Character.Subset − The Character.Subset class instances represent particular subsets of the Unicode character set. The only family of subsets defined in the Character class is UnicodeBlock.
-
Character.UnicodeBlock − The Character.UnicodeBlock class is a family of character subsets representing the character blocks in the Unicode specification. Character blocks generally define characters used for a specific script or purpose.
-
Class − The Class class instance represent classes and interfaces in a running Java application. It has no public constructor.
-
ClassLoader − The ClassLoader class is an object that is responsible for loading classes. This class is an abstract class. It may be used by security managers to indicate security domains.
-
Compiler − The Compiler class is provided to support Java-to-native-code compilers and related services. By design, it serves as a placeholder for a JIT compiler implementation.
-
Double − The Double class wraps a value of primitive type double in an object. An object of type Double contains a single field whose type is double.
-
Enum − The Enum class is the common base class of all Java language enumeration types.
-
Float − The Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose type is float.
-
InheritableThreadLocal − The InheritableThreadLocal class extends ThreadLocal to provide inheritance of values from parent thread to child thread: when a child thread is created, the child receives initial values for all inheritable thread-local variables for which the parent has values.
-
Integer − The Integer class wraps a value of primitive type int in an object. An object of type Integer contains a single field whose type is int.
-
Long − The Long class wraps a value of the primitive type long in an object. An object of type Long contains a single field whose type is long.
-
Math − The Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
-
Number − The Number class is an abstract class in java.lang package. It is the superclass of the classes that represent numeric values convertible to primitive data types such as byte, short, int, long, float, and double.
-
Object − The Object class is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
-
Package − The Package class contain version information about the implementation and specification of a Java package.
-
Process − The Process class provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.
-
ProcessBuilder − The ProcessBuilder class is used to create operating system processes.This class is not synchronized.
-
Runtime − The Runtime class allows the application to interface with the environment in which the application is running.
-
RuntimePermission − The RuntimePermission class is for runtime permissions. A RuntimePermission contains a name (also referred to as a "target name") but no actions list; you either have the named permission or you don’t.
-
SecurityManager − The SecurityManager class allows applications to implement a security policy. It allows an application to determine, before performing a possibly unsafe or sensitive operation, what the operation is and whether it is being attempted in a security context that allows the operation to be performed. The application can allow or disallow the operation.
-
Short − The Short class wraps a value of primitive type short in an object. An object of type Short contains a single field whose type is short.
-
StackTraceElement − The StackTraceElement class element represents a single stack frame. All stack frames except for the one at the top of the stack represent a method invocation.
-
StrictMath − The StrictStrictMath class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
-
String − Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects. The Java platform provides the String class to create and manipulate strings.
-
StringBuffer − The StringBuffer class is mutable sequence of characters. StringBuffer can be used to modify the content of a String with ease. It provides many utility functions to manipulate a string.
-
StringBuilder − The StringBuilder class is mutable sequence of characters. This provides an API compatible with StringBuffer, but with no guarantee of synchronization.
-
System − The System class contains several useful class fields and methods. It cannot be instantiated.
-
Thread − The Thread class is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
-
ThreadGroup − The ThreadGroup class represents a set of threads. It can also include other thread groups. The thread groups form a tree in which every thread group except the initial thread group has a parent.
-
ThreadLocal − The ThreadLocal class provides thread-local variables.
-
Throwable − The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement.
-
Void − The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.
Examples of java.lang Package
实践以下示例以了解 java.lang 包类概念和用法。
Practice the following examples to learn the concept and usage of java.lang package clasess.
Example of java.lang.Integer
以下程序阐述了 Integer 中支持的 valueOf() 方法的使用 −
The following program illustrates use of valueOf() method supported by Integer −
package com.tutorialspoint;
public class IntegerDemo {
public static void main(String[] args) {
// create a String s and assign value to it
String s = "+120";
// create a Integer object i
Integer i;
// get the value of int from string
i = Integer.valueOf(s);
// print the value
System.out.println( "Integer value of string " + s + " is " + i );
}
}
让我们编译并运行上述程序,这将生成以下结果 −
Let us compile and run the above program, this will produce the following result −
Integer value of string +120 is 120
Example of java.lang.Thread
以下程序阐述了 Thread 中支持的方法的使用 −
The following program illustrates use of methods supported by Thread −
package com.tutorialspoint;
public class ThreadDemo implements Runnable {
ThreadDemo() {
// main thread
Thread currThread = Thread.currentThread();
// thread created
Thread t = new Thread(this, "Admin Thread");
System.out.println("current thread = " + currThread);
System.out.println("thread created = " + t);
// this will call run() function
t.start();
}
public void run() {
System.out.println("This is run() method");
}
public static void main(String args[]) {
new ThreadDemo();
}
}
让我们编译并运行上述程序,这将生成以下结果 −
Let us compile and run the above program, this will produce the following result −
current thread = Thread[main,5,main]
thread created = Thread[Admin Thread,5,main]
This is run() method
Example of java.lang.Math
以下程序阐述了 Math 中支持的方法的使用 −
The following program illustrates use of methods supported by Math −
package com.tutorialspoint;
public class MathDemo {
public static void main(String[] args) {
// get two double numbers
double x = 60984.1;
double y = -497.99;
// get the natural logarithm for x
System.out.println("Math.log(" + x + ")=" + Math.log(x));
// get the natural logarithm for y
System.out.println("Math.log(" + y + ")=" + Math.log(y));
// get the max value
System.out.println("Math.max(" + x + ", y" + ")=" + Math.max(x,y));
// get the min value
System.out.println("Math.min(" + x + ", y" + ")=" + Math.min(x,y));
}
}
When java.lang Package is Used?
无论何时我们需要准备 UI、进行处理、使用包装类等,我们都可以依赖于 java.lang 包中现有的类。本参考专为初学者编写,旨在帮助他们理解与 Java.lang 包中的所有可用方法相关基本功能。
Whenever we need to prepare UI, threading, use wrapper classess etc, we can rely on classes present in java.lang package. This reference has been prepared for the beginners to help them understand the basic functionality related to all the methods available in Java.lang package.