Java.lang - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.lang Tutorial

java.lang Tutorial

shape Description

The Package can be defined as group of classes, interfaces, sub-packages. Packages are divided into two types. They are: The advantage of the package is that it provides access protection, removes naming collision, class and interfaces can be managed easily. While doing any Java program the regularly using packages are java.lang, java.io packages, where java.lang package will consist set of fundamental classes and java.io package will consist set of input, output streams. The big advantage of java.lang package is that no need to import into the code explicitly, but other packages are needed to be imported.

shape Conceptual Figure

Following are the sub packages of parent package.

java.util Package

The java.util package is the one of the important packages that is used while writing any code related to date and time. This package also consists of collection framework and miscellaneous classes. ArrayList, Map are the implementation classes of this package.

java.Awt Package

The Abstract Window Tool kit is the API which provides various components to develop the window based application. The button is one of the implementation class of this package.

Creating A Package

shape Description

Package is a keyword, by using this keyword one can create another package. Following is an example where the developer is going to create the Splesson package. Simple.java [java]package splesson; public class Simple{ public static void main(String args[]){ System.out.println("Hello, Welcome to package"); } } [/java] Without any IDE package will be compiled as follows. [java]javac -d . Simple.java //The -d is a switch that tells the compiler where to put the class file i.e. it represents destination folder.[/java] The code will be run as follows. [java]java splesson.Simple[/java] Output: Now compile the code result will be as follows. [java]Hello, Welcome to package[/java] By using three ways a developer can access the Java Packages from another package, they are as follows. Following is the structure to use packages.

By using import package.*;

Here developer is going create another package with the name pack. A.java [java]package pack; public class A{ public void msg(){ System.out.println("Welcome To Splessons");} } [/java] Now the developer is going to create main method in splesson package with different class. B.java [java]package splesson; import pack.*;//here used import keyword. public class B { public static void main(String args[]){ A obj = new A(); obj.msg(); } } [/java] Here the developer user previous package by using import keyword. [java]import pack.*;[/java] Output: When compile the code result will be as follows. [java] Welcome To Splessons [/java]

By using import package.classname;

B.java [java]package splesson; import pack.A; public class B { public static void main(String args[]){ A obj = new A(); obj.msg(); } } [/java] Where the developer used import pack.A; Output: When compile the code result will be as follows. [java] Welcome To Splessons [/java]

fully qualified name

B.java [java]package splesson; import pack.A; public class B { public static void main(String args[]){ pack.A obj = new pack.A();//fully qualified name. obj.msg(); } } [/java] Following is the fully qualified name. [java]pack.A obj = new pack.A();[/java] Output When compile the code result will be as follows. [java] Welcome To Splessons [/java]

shape Prerequisites

Before going to work on packages user need to have knowledge on OOPS concept of Java why because without knowing the syntax declarations of classes and object creation, it will be less difficult to learn and the package is nothing but a collection of classes, methods, and interface.

Summary

shape Key Points

  • Package will have set of classes and interfaces that used to develop the programming.
  • The java.io package is used to provide input, output streams.
  • An object is the important class why because it is the parent of classes.
  • Most of the methods will hire the properties from java.lang.Object class.