This article is series of Java Lang Package tutorial. In this tutorial, we will discuss the important and commonly used Package class methods with examples.
Package objects contain version information about the implementation and specification of a Java package. This versioning information is retrieved and made available by the ClassLoader instance that loaded the class(es). Typically, it is stored in the manifest that is distributed with the classes.
The set of classes that make up the package may implement a particular specification and if so the specification title, version number, and vendor strings identify that specification. An application can ask if the package is compatible with a particular version, see the isCompatibleWith method for details.
Check out below Java Core API packages tutorials:
Java Package Class Methods
The below class diagram shows a list of Java Package class methods:
Java Package Class Methods/APIs Examples
Let's demonstrates the usage of a few important and commonly used Package class methods with examples. In below PackageClassMethods class, each method name describes the Package class method and its usage. Check out each Package class method description at https://docs.oracle.com/javase/8/docs/api/java/lang/Package.html.
package net.javaguides.lang;
/**
* Class demonstrates the usage of Package class methods with examples
*
* @author Ramesh Fadatare
*
*/
public class PackageClassMethods {
public static void main(String[] args) {
PackageClassMethods classMethods = new PackageClassMethods();
classMethods.toStringMethod();
classMethods.isSealedMethod();
classMethods.isCompatibleWithMethod();
classMethods.getSpecificationVersionMethod();
classMethods.getSpecificationVendorMethod();
classMethods.getSpecificationTitleMethod();
classMethods.getPackagesMethod();
classMethods.getPackageMethod();
classMethods.getNameMethod();
classMethods.getImplementationVersionMethod();
classMethods.getImplementationVendorMethod();
classMethods.getImplementationTitleMethod();
}
public void toStringMethod() {
// get the java lang package
Package pack = Package.getPackage("java.lang");
// print the package as a string
System.out.println("" + pack.toString());
}
public void isSealedMethod() {
// get the java lang package
Package pack = Package.getPackage("java.lang");
// check if this package is sealed
System.out.println("" + pack.isSealed());
}
public void isCompatibleWithMethod() {
// get the java lang package
Package pack = Package.getPackage("java.lang");
// check if this package is compatible with version 1.4.6
System.out.println("" + pack.isCompatibleWith("1.8"));
}
public void getSpecificationVersionMethod() {
// get the java lang package
Package pack = Package.getPackage("java.lang");
// print the specification version for this package
System.out.println("" + pack.getSpecificationVersion());
}
public void getSpecificationVendorMethod() {
// get the java lang package
Package pack = Package.getPackage("java.lang");
// print the specification vendor for this package
System.out.println("" + pack.getSpecificationVendor());
}
public void getSpecificationTitleMethod() {
// get the java lang package
Package pack = Package.getPackage("java.lang");
// print the specification title for this package
System.out.println("" + pack.getSpecificationTitle());
}
public void getPackagesMethod() {
// get all the packages
Package[] pack = Package.getPackages();
// print all packages, one by one
for (int i = 0; i < pack.length; i++) {
System.out.println("" + pack[i]);
}
}
public void getPackageMethod() {
// create a package object for java.lang package
Package pack = Package.getPackage("java.lang");
// get the fully qualified name for this package
System.out.println("" + pack.getName());
}
public void getNameMethod() {
// create a package object for java.lang package
Package pack = Package.getPackage("java.lang");
// get the fully qualified name for this package
System.out.println("" + pack.getName());
}
public void getImplementationVersionMethod() {
// create a package object for java.lang package
Package pack = Package.getPackage("java.lang");
// get the implementation version
System.out.println("" + pack.getImplementationVersion());
}
public void getImplementationVendorMethod() {
// create a package object for java.lang package
Package pack = Package.getPackage("java.lang");
// get the implementation vendor
System.out.println("" + pack.getImplementationVendor());
}
public void getImplementationTitleMethod() {
// create a package object for java.lang package
Package pack = Package.getPackage("java.lang");
// get the annotation for lang package
System.out.println("" + pack.getImplementationTitle());
}
}
Output:
package java.lang, Java Platform API Specification, version 1.8
false
true
1.8
Oracle Corporation
Java Platform API Specification
package sun.reflect, Java Platform API Specification, version 1.8
package java.util, Java Platform API Specification, version 1.8
package sun.reflect.annotation, Java Platform API Specification, version 1.8
package java.lang.annotation, Java Platform API Specification, version 1.8
package java.nio, Java Platform API Specification, version 1.8
package sun.nio, Java Platform API Specification, version 1.8
package java.security.cert, Java Platform API Specification, version 1.8
package java.util.zip, Java Platform API Specification, version 1.8
package sun.launcher, Java Platform API Specification, version 1.8
package sun.security.action, Java Platform API Specification, version 1.8
package java.nio.file, Java Platform API Specification, version 1.8
package java.nio.charset, Java Platform API Specification, version 1.8
package sun.net.www, Java Platform API Specification, version 1.8
package java.lang.ref, Java Platform API Specification, version 1.8
package java.net, Java Platform API Specification, version 1.8
package jdk.internal.util, Java Platform API Specification, version 1.8
package sun.net.www.protocol.file, Java Platform API Specification, version 1.8
package java.lang.invoke, Java Platform API Specification, version 1.8
package sun.util.locale, Java Platform API Specification, version 1.8
package sun.reflect.generics.repository, Java Platform API Specification, version 1.8
package sun.misc, Java Platform API Specification, version 1.8
package java.lang.reflect, Java Platform API Specification, version 1.8
package sun.net.util, Java Platform API Specification, version 1.8
package net.javaguides.lang
package java.security, Java Platform API Specification, version 1.8
package sun.net.www.protocol.jar, Java Platform API Specification, version 1.8
package java.util.concurrent, Java Platform API Specification, version 1.8
package sun.io, Java Platform API Specification, version 1.8
package java.util.concurrent.atomic, Java Platform API Specification, version 1.8
package java.util.concurrent.locks, Java Platform API Specification, version 1.8
package sun.util, Java Platform API Specification, version 1.8
package java.lang, Java Platform API Specification, version 1.8
package java.io, Java Platform API Specification, version 1.8
package sun.reflect.misc, Java Platform API Specification, version 1.8
package sun.nio.ch, Java Platform API Specification, version 1.8
package java.util.jar, Java Platform API Specification, version 1.8
package sun.nio.cs, Java Platform API Specification, version 1.8
package java.util.function, Java Platform API Specification, version 1.8
package java.nio.charset.spi, Java Platform API Specification, version 1.8
package sun.security.util, Java Platform API Specification, version 1.8
package sun.usagetracker, Java Platform API Specification, version 1.8
java.lang
java.lang
1.8.0_172
Oracle Corporation
Java Runtime Environment
References
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment