0% found this document useful (0 votes)
4 views4 pages

Java Class Library Overview

The document explains three methods for representing values in Java programs: initializing variables, receiving parameters, and user input during execution. It discusses the Java class library, including built-in and user-defined packages, and how to access classes from these packages using different import methods. Additionally, it provides example programs demonstrating each method of input and the use of the Scanner class for user input.

Uploaded by

chahat jha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Java Class Library Overview

The document explains three methods for representing values in Java programs: initializing variables, receiving parameters, and user input during execution. It discusses the Java class library, including built-in and user-defined packages, and how to access classes from these packages using different import methods. Additionally, it provides example programs demonstrating each method of input and the use of the Scanner class for user input.

Uploaded by

chahat jha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

4.

Class Library Of Java


There are three ways to represent values in a program

1. Initialize variables or assign values to variables within the program


2. Receive values through parameters.
3. Input values from user during execution.

Initialize variables or assign values to variables.


Example program 1
class Sum1
{
//function to assign values and display sum
void displaySum()
{
int a,b;
a=10;
b=5;
[Link]("Sum="+(a+b));
}
}

The drawback of this method is that the program will always work with the same set of values.

Receive values through parameters


Example Program 2
class Sum2
{
//function to accept values through parameters and to display sum
void displaySum(int a, int b)
{
[Link]("Sum="+(a+b));
}
}

The drawback of this method is that you need BlueJ IDE to execute this program.

Input values from user during execution (using methods of class Scanner)
Example Program 3

[Link]
import [Link].*;
class Sum3
{
//function to input two numbers and to display sum
void displaySum()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the first number");
int n1=[Link]();
[Link]("Enter the second number");
int n2=[Link]();
[Link]("sum="+(n1+n2));
}
}

import [Link].*;
import is a keyword in java.
[Link] is one of the packages in java’s class library.
The group of pre-defined classes that are part of java programming language is called the
class library of java .
class library of java is also called java API /JCL
API- Application Programming interface

package in java

A java package is a group of similar types of classes, interfaces and sub-packages.


Packages are divided into two categories:
- Built-in Packages (packages from Java API)
- User-defined Packages (created by the programmer)

Advantage of Java Package

1) Java package is used to categorize the classes and interfaces so that they can
be easily maintained.
2) Java package removes naming collision.

[Link]
Built-in Packages (packages from the Java API)

Package Name Classes belong to the package


[Link] Scanner
[Link] InputStreamReader, BufferedReader
[Link] Math, System, String

Byte
Short
Wrapper Classes
Integer
Long
Float
Double
Boolean
Character

There are three ways to access classes from packages other than [Link]

i) import package.*;
ii) import [Link];
ii) fully qualified name.
The import keyword is used to make the classes and interface of another package accessible to
the current class.
i) Accessing classes from other packages using import package.*;
If you use package.* then all the classes and interfaces of this package will be accessible.
* is a wild-card character that represents all the classes of a package.
Example of using package.*
Example Program 4
import [Link].*;
class Pgm4
{
//function to input two numbers and to display sum
void displaySum()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the first number");
int n1=[Link]();
[Link]("Enter the second number");
int n2=[Link]();
[Link]("sum="+(n1+n2));
}
}

ii) Accessing classes from other packages using import [Link];


If you import [Link] then only declared class of this package will be accessible.
Example of using import [Link]

[Link]
Example Program 5
import [Link];
class Pgm5
{
//function to input two numbers and to display sum
void displaySum()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the first number");
int n1=[Link]();
[Link]("Enter the second number");
int n2=[Link]();
[Link]("sum="+(n1+n2));
}
}
iii)Accessing classes from other packages using fully qualified name.
[Link] is called the fully qualified name or the complete name of a class.
For example the fully qualified name of class String is [Link]

If you use fully qualified name then the 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.
It is generally used when two packages have same class name e.g. [Link] and [Link] packages
contain Date class.

Example of using classes of other packages by using the fully qualified name
Example Program 6
class Pgm6
{
//function to input two numbers and to display sum
void displaySum()
{
[Link] sc=new [Link]([Link]);
[Link]("Enter the first number");
int n1=[Link]();
[Link]("Enter the second number");
int n2=[Link]();
[Link]("sum="+(n1+n2));
}

Scanner sc=new Scanner([Link]);

In this statement an object of class Scanner named sc is created and this object is linked to the
predefined input stream called [Link].

[Link]

Common questions

Powered by AI

Using a fully qualified name provides the advantage of avoiding the import statement, especially useful when two packages have classes with the same name, thus preventing naming conflicts. However, it can make the code verbose and harder to read as the full package path must be written every time the class is accessed .

The Scanner class in Java is used for parsing primitive types and strings using regular expressions. It is typically linked to the predefined System.in input stream, facilitating user input by reading data from standard input devices like keyboards. This allows programmers to handle interactive input from users during program execution .

Classes from packages other than java.lang can be accessed via: 1) import package.*; allowing access to all classes and interfaces in the package, useful when many classes are needed. 2) import package.classname; provides access only to the specified class, reducing namespace pollution. 3) fully qualified name; access without needing import, useful in cases where class name conflicts might occur between different packages .

The use of the wild-card character '*' in import statements allows all classes and interfaces within a package to be accessible, reducing manual import effort for multiple classes. However, it may decrease readability and maintainability because it isn't clear which specific classes are used in the program, leading to potential ambiguity and misunderstandings among developers .

Using parameters to receive values in a Java program is more structured as it allows clear specification of method input requirements, ensuring predictable program behavior. However, it requires the values to be known at compile time or supplied via an IDE, reducing flexibility in interactive scenarios. In contrast, using Scanner for runtime input offers greater flexibility by allowing dynamic data handling based on user input, albeit with potential risks of incorrect input formats leading to runtime errors .

Without using packages, Java programs would face significant challenges including increased risk of naming conflicts due to a lack of namespace separation, making code harder to manage. The organization would suffer from scalability issues as the codebase grows, leading to difficulties in maintenance and comprehension, as distinct modules of the program might intermix. This could further lead to technical debt, as adapting and evolving the code could become cumbersome .

Wrapper classes in Java provide a way to use primitive data types (int, char, etc.) as objects. Each primitive type has a corresponding wrapper class (e.g., int has Integer, char has Character). They are important because they enable primitives to be used in collections and have additional functionality, such as parsing and converting types .

Initializing variables directly within a program is less flexible compared to inputting them during execution. Direct initialization results in the program operating on fixed data, which doesn't adapt to different scenarios unless changes are made to the source code. In contrast, inputting variables at runtime allows the program to work under various conditions without altering its code, thus increasing flexibility and adaptability .

The three methods for representing values in a Java program are: 1) Initializing variables or assigning values to variables within the program. The main drawback is that the program will always work with the same set of values. 2) Receiving values through parameters, which requires the BlueJ IDE to execute the program. 3) Inputting values from the user during execution using classes like Scanner, which can introduce runtime errors if the user enters unexpected input .

Java packages help maintain code by categorizing classes and interfaces, making them easier to manage. They also help avoid naming collisions by creating distinct namespaces for classes and interfaces. This specialization ensures that even if two classes have the same name, they can coexist within different packages without conflict .

You might also like