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

Java.util Tutorial

java.util Package

shape Description

The Package can be characterized as gathering of classes, interfaces, sub-bundles. Packages are isolated into two sorts. They are as follows. The upside of the package is that it gives get to assurance, evacuates naming impact, class and interfaces can be overseen effortlessly. While doing any Java program the frequently utilizing packages are java.lang, java.io, java.util package, where java.lang package will comprise set of principal classes and java.io will comprise set of information, yield streams. By utilizing java.util.package will have accumulation system classes, date and time techniques that are utilized to execute dynamic projects. Taking after is the best case for util bundle. SampleProgram.java [java] package com.SPlessons; import java.util.Scanner; public class SampleProgram { public static void main(String args[]){ int n; System.out.println("Enter the number to register"); Scanner s=new Scanner(System.in); n=s.nextInt(); if(n==2||n==3){ System.out.println("Welcome to SPlessons."); } else { System.out.println("Error: Please enter valid key"); } } } [/java] Here the developer made the code dynamically when enter the valid code as given in the code regarding message will be opened otherwise error message will be opened. Output:Now compile the code result will be as follows. [java] Enter the number to register 2 Welcome to SPlessons. [/java]

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 the 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 a 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.