Introduction
● A java package is a mechanism for organizing Java classes into groups.● A java package is a group of similar types of classes, interfaces and subpackages.
● Package in java can be Categorized in Two form,
– built-in package
– user-defined package.
● There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
● Advantage of Java Package
– Java package is used to categorize the classes and interfaces so that they can be easily maintained– Java package provides access protection.
– Java package removes naming collision.
Basic Details About Package:-
● A package declaration resides at the top of a Java source file.
● All source files to be placed in a package have common package name.
● A package provides a unique namespace for the classes it contains.
● A Package can contain:
– Classes
– Interfaces
– Enumerated types
– Annotations
● Two classes in different packages can have the same name.
● Packages provide a mechanism to hide its classes from being used by programs or packages belonging to other classes.
● How to compile java package:-
javac -d directory javafilename
● For example:-
javac -d . Simple.java
● The -d switch specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use . (dot).
● How to run java package program To Compile:
javac -d . Simple.java To Run: java mypack.Simple
Output:Welcome to package
● The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder.
How to access package from another package?
There are three ways to access the package from outside the package.
import package.*;
import package.classname;
fully qualified name.
1) Using packagename.*
● If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages.
● The import keyword is used to make the classes and interface of another package accessible to the current package.
2) Using packagename.classname
● If you import package.classname then only declared class of this package will be accessible.
3) Using fully qualified name
If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface
Subpackages in Java
● Package inside the package is called the subpackage. It should be created to categorize the package further.
● The standard of defining package is domain.company.package
● e.g. abc.java.bean
Access Modifiers in Java
● There are two types of modifiers in java: – access modifiers and non-access modifiers
● The access modifiers in java specifies accessibility (scope) of a data member, method, constructor or class.
● There are 4 types of java access modifiers:
– Private
– Default
– Protected
– Public
● There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc.
Java.lang package
● Java.lang is imported by default in all the classes that we create.
● There is no need to explicitly import the lang package.
● It contains classes that form the basic building blocks of Java.
● The Object class is the parent class of all the classes in java by default.
● In other words, it is the topmost class of java.
● The Object class is beneficial if you want to refer any object whose type you don't know.
● There is no need to explicitly inherit the Object class.
Java Wrapper Classes
● Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.
● For each primitive type, there is a corresponding wrapper class designed.
● An instance of wrapper contains or wraps a primitive value of the corresponding type.
● Wrappers allow for situations where primitives cannot be used but their corresponding objects are required.
● The eight classes of java.lang package are known as wrapper classes in java.
● The list of eight wrapper classes are given below:
● Features of Wrapper Classes: –
All the wrapper classes except Character and Float have constructors.
- one that takes the primitive value and another that takes the String representation of the value. Character has one constructor and float has three.
– Just like strings, wrapper objects are also immutable. i.e once a value is assigned it cannot be changed.
● The wrapper classes have a number of static methods for handling and manipulating primitive data types and objects.
● Constructors: converting primitive types to wrapper classes Integer i = new Integer (10);
● Methods: converting wrapper objects to primitives
all numeric classes have methods to convert a numeric wrapper class to their respective primitive type.
byteValue(), intValue(), floatValue(), doubleValue(), longValue(), shortValue() int v = a.intValue();
● Converting Primitives to String object:
the method toString() is used to convert primitive number data types to String String xyz = Integer.toString (v) //converting primitive integer to String String xyz = Float.toString (x)
● Converting Back from String Object to Primitives:
the six parser methods are parseInt, parseDouble, parseFloat, parseLong, parseByte and parseShort. int v = Integer.parseInt (xyz);
NeXt Artical Related String in Java

Don't Forget to Share your Opinion About This post in Comment Section, Your One Comment Will Not only Make Our day But will Make our Year. And Do mention Of you have any ideas for our Blog:)
0 Comments