Unit 3-1
Unit 3-1
2) Using packagename.classname
If you import package.classname then only declared class of this package will be
accessible.
Example of package by import package.classname
1. //save by A.java
2.
3. package pack;
4. public class A{
5. public void msg(){System.out.println("Hello");}
6. }
1. //save by B.java
2. package mypack;
3. import pack.A;
4.
5. class B{
6. public static void main(String args[]){
7. A obj = new A();
8. obj.msg();
9. }
10. }
Output:Hello
Note: Sequence of the program must be package then import then class.
Subpackage in java
Package inside the package is called the subpackage. It should be created to
categorize the package further.
ADVERTISEMENT
Let's take an example, Sun Microsystem has definded a package named java that
contains many classes like System, String, Reader, Writer, Socket etc. These classes
represent a particular group e.g. Reader and Writer classes are for Input/Output
operation, Socket and ServerSocket classes are for networking etc and so on. So, Sun
has subcategorized the java package into subpackages such as lang, net, io etc. and
put the Input/Output related classes in io package, Server and ServerSocket classes in
net packages and so on.
The standard of defining package is domain.company.package e.g. com.javatpoint.bean
or org.sssit.dao.
Example of Subpackage
1. package com.javatpoint.core;
2. class Simple{
3. public static void main(String args[]){
4. System.out.println("Hello subpackage");
5. }
6. }
To Compile: javac -d . Simple.java
To Run: java com.javatpoint.core.Simple
Output:Hello subpackage
1. //save as Simple.java
2. package mypack;
3. public class Simple{
4. public static void main(String args[]){
5. System.out.println("Welcome to package");
6. }
7. }
To Compile:
e:\sources> javac -d c:\classes Simple.java
To Run:
To run this program from e:\source directory, you need to set classpath of the directory where the
class file resides.
e:\sources> set classpath=c:\classes;.;
e:\sources> java mypack.Simple
Another way to run this program by -classpath switch of java:
The -classpath switch can be used with javac and java tool.
To run this program from e:\source directory, you can use -classpath switch of java
that tells where to look for class file. For example:
e:\sources> java -classpath c:\classes mypack.Simple
Output:Welcome to package
Rule: There can be only one public class in a java source file and it must be saved by the
public class name.
1. //save as C.java otherwise Compilte Time Error
2. class A{}
3. class B{}
4. public class C{}
o You need to load a class that is not present in the current directory or any sub-
directories.
o You need to load a class that is not in a location specified by the extensions
mechanism.
The CLASSPATH depends on what you are setting the CLASSPATH. The CLASSPATH
has a directory name or file name at the end. The following points describe what should
be the end of the CLASSPATH.
o If a JAR or zip, the file contains class files, the CLASSPATH end with the name of
the zip or JAR file.
o If class files placed in an unnamed package, the CLASSPATH ends with the
directory that contains the class files.
o If class files placed in a named package, the CLASSPATH ends with the directory
that contains the root package in the full package name, that is the first package
in the full package name.
The default value of CLASSPATH is a dot (.). It means the only current directory
searched. The default value of CLASSPATH overrides when you set the CLASSPATH
variable or using the -classpath command (for short -cp). Put a dot (.) in the new setting
if you want to include the current directory in the search path.
If CLASSPATH finds a class file which is present in the current directory, then it will load
the class and use it, irrespective of the same name class presents in another directory
which is also included in the CLASSPATH.
If you want to set multiple classpaths, then you need to separate each CLASSPATH by
a semicolon (;).
The third-party applications (MySQL and Oracle) that use the JVM can modify the
CLASSPATH environment variable to include the libraries they use. The classes can be
stored in directories or archives files. The classes of the Java platform are stored in
rt.jar.
There are two ways to ways to set CLASSPATH: through Command Prompt or by
setting Environment Variable.
Step 1: Click on the Windows button and choose Control Panel. Select System.
ADVERTISEMENT
If the CLASSPATH doesn't exist in System Variables, then click on the New button and
type Variable name as CLASSPATH and Variable value as C:\Program
Files\Java\jre1.8\MySQL-Connector Java.jar;.;
PATH CLASSPATH
You are required to include You are required to include all the directories which
the directory which contains contain .class and JAR files.
.exe files.
In the above command, The set is an internal DOS command that allows the user to
change the variable value. CLASSPATH is a variable name. The variable enclosed in
percentage sign (%) is an existing environment variable. The semicolon is a separator,
and after the (;) there is the PATH of rt.jar file.
The following table demonstrates the key difference between the CLASSPATH and
Extension Mechanism:
Class loading CLASSPATH loads after bootstrap ext loads after bootstrap
order and extension loading. loading but before CLASSPATH
loading.
Scope It is an application specific. All JREs All JVMs are running in specific
on the host is the CLASSPATH JRE java.ext.dirs.
environment variable.
The mechanism will pick up all .jar files from the extension directory even if the file
does not have the .jar extension. The implementation of this is that if one can change
the name of a jar placed in a classpath directory to have an extension other than .jar.
The wildcard (*) does not pick it up. This technique will not work with the extension
directory.
A.java
1. public class A {
2. public String toString()
3. {
4. return "hello";
5. } }
B.java
1. public class B {
2. public static void main(final String[] args)
3. {
4. System.out.println(new A());
5. } }
Compile the A.java file. we will archive the compiled A.class file into A.jar. Place this
JAR file into another directory than the compiled B.class file.
To demonstrate the use of the classpath, we place the A.jar file in a directory
C:\JavaPrograms and will access that JAR through wildcard (*) for B to use.
We found that B can still load the A.class while we had deleted it from the current
directory. The Java launcher was explicitly looked for C:\JavaProgram. It is also possible
to have the class loaded without its presence in the same directory and explicit
classpath specification.
What happens if we change the name of A.jar into A.backup in the same CLASSPATH-
referenced directory. NoClassDefFoundError is encountered when we do the same
because the CLASSPATH-reference does not have the .jar extension.
what is utility classes
utility class is a class that defines a set of methods that perform common, often re-used
functions. Most utility classes define these common methods under static (see Static
variable) scope. Examples of utility classes include java.util.Collections which provides
several utility methods (such as sorting) on objects that implement a Collection
(java.util.collection).
StringTokenizer in Java
The java.util.StringTokenizer class allows you to break a String into tokens. It is
simple way to break a String. It is a legacy class of Java.
It doesn't provide the facility to differentiate numbers, quoted strings, identifiers etc.
like StreamTokenizer class. We will discuss about the StreamTokenizer class in I/O
chapter.
In the StringTokenizer class, the delimiters can be provided at the time of creation or
one by one to the tokens.
Constructors of the StringTokenizer Class
There are 3 constructors defined in the StringTokenizer class.
Constructor Description
Methods Description
String nextToken() It returns the next token from the StringTokenizer object.
String nextToken(String delim) It returns the next token based on the delimiter.
Simple.java
1. import java.util.StringTokenizer;
2. public class Simple{
3. public static void main(String args[]){
4. StringTokenizer st = new StringTokenizer("my name is khan"," ");
5. while (st.hasMoreTokens()) {
6. System.out.println(st.nextToken());
7. }
8. }
9. }
Output:
my
name
is
khan
The above Java code, demonstrates the use of StringTokenizer class and its methods
hasMoreTokens() and nextToken().
1. import java.util.*;
2. public class Test {
3. public static void main(String[] args) {
4. StringTokenizer st = new StringTokenizer("my,name,is,khan");
5. // printing next token
6. System.out.println("Next token is : " + st.nextToken(","));
7. }
8. }
Output:
Next token is : my
Note: The StringTokenizer class is deprecated now. It is recommended to use the split()
method of the String class or the Pattern class that belongs to the java.util.regex package.
StringTokenizer1.java
1. import java.util.StringTokenizer;
2. public class StringTokenizer1
3. {
4. /* Driver Code */
5. public static void main(String args[])
6. {
7. /* StringTokenizer object */
8. StringTokenizer st = new StringTokenizer("Demonstrating methods from StringTok
enizer class"," ");
9. /* Checks if the String has any more tokens */
10. while (st.hasMoreTokens())
11. {
12. System.out.println(st.nextToken());
13. }
14. }
15. }
Output:
Demonstrating
methods
from
StringTokenizer
class
The above Java program shows the use of two methods hasMoreTokens() and
nextToken() of StringTokenizer class.
StringTokenizer2.java
1. import java.util.StringTokenizer;
2. public class StringTokenizer2
3. {
4. public static void main(String args[])
5. {
6. StringTokenizer st = new StringTokenizer("Hello everyone I am a Java developer","
");
7. while (st.hasMoreElements())
8. {
9. System.out.println(st.nextToken());
10. }
11. }
12. }
Output:
Hello
everyone
I
am
a
Java
developer
StringTokenizer3.java
1. import java.util.StringTokenizer;
2. public class StringTokenizer3
3. {
4. /* Driver Code */
5. public static void main(String args[])
6. {
7. /* StringTokenizer object */
8. StringTokenizer st = new StringTokenizer("Hello Everyone Have a nice day"," ");
9. /* Checks if the String has any more tokens */
10. while (st.hasMoreTokens())
11. {
12. /* Prints the elements from the String */
13. System.out.println(st.nextElement());
14. }
15. }
16. }
Output:
Hello
Everyone
Have
a
nice
day
StringTokenizer4.java
1. import java.util.StringTokenizer;
2. public class StringTokenizer3
3. {
4. /* Driver Code */
5. public static void main(String args[])
6. {
7. /* StringTokenizer object */
8. StringTokenizer st = new StringTokenizer("Hello Everyone Have a nice day"," ");
9. /* Prints the number of tokens present in the String */
10. System.out.println("Total number of Tokens: "+st.countTokens());
11. }
12. }
Output: