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

Java String Operations and Packages Guide

The document outlines various string operations in Java, including concatenation, length determination, character extraction, substring extraction, and comparison, with examples provided for each. It also discusses the significance of packages in Java, detailing how to create user-defined packages and their benefits. Additionally, it explains the protected access modifier and provides an overview of InputStream and OutputStream classes for handling byte streams in Java.

Uploaded by

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

Java String Operations and Packages Guide

The document outlines various string operations in Java, including concatenation, length determination, character extraction, substring extraction, and comparison, with examples provided for each. It also discusses the significance of packages in Java, detailing how to create user-defined packages and their benefits. Additionally, it explains the protected access modifier and provides an overview of InputStream and OutputStream classes for handling byte streams in Java.

Uploaded by

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

Java Assignment Answers

Q1. Describe all operations which can be performed on strings. Give the
suitable examples (program) for any five operations.
In Java, the following operations can be performed on strings:
- Concatenation (`+`, `concat()` method)
- Length determination (`length()` method)
- Character extraction (`charAt()` method)
- Substring extraction (`substring()` method)
- Comparison (`equals()`, `compareTo()`)
- Conversion (`toUpperCase()`, `toLowerCase()`)
- Trimming (`trim()`)
- Searching (`indexOf()`, `lastIndexOf()`)

Examples for five operations:

public class StringOperations {


public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";

// 1. Concatenation
String result = str1 + " " + str2;
[Link]("Concatenated: " + result);

// 2. Length
[Link]("Length: " + [Link]());

// 3. Character extraction
[Link]("Character at index 1: " + [Link](1));

// 4. Substring
[Link]("Substring (0-5): " + [Link](0, 5));

// 5. Comparison
[Link]("Are strings equal? " + [Link](str2));
}
}
Q2. Write the significance or use of package in Java with a suitable
example. List the steps to create user-defined package.
Significance of packages in Java:
- Packages help organize classes and interfaces.
- Avoids name conflicts.
- Provides controlled access.
- Makes searching and locating classes/interfaces easier.

Steps to create user-defined package:


1. Create a package using the `package` keyword.
2. Save the class in a directory with the package name.
3. Compile the class using `javac -d . [Link]`.
4. Import and use it in another class using `import [Link]`.

Example:
1. Creating a package (in file `mypack/[Link]`):

package mypack;

public class Message {


public void display() {
[Link]("Hello from user-defined package!");
}
}

2. Using the package:

import [Link];

public class TestPackage {


public static void main(String[] args) {
Message msg = new Message();
[Link]();
}
}

Q3. Explain protected modifier in detail.


The `protected` access modifier in Java allows access to the members of a class within the
same package and by subclasses in other packages.
Key points:
- More accessible than `private`, but less than `public`.
- Accessible in the same package and in subclasses (even outside the package).

Example:
In Base class:

package demo;

public class Base {


protected void show() {
[Link]("Protected method in Base class");
}
}

In Derived class:

package test;

import [Link];

class Derived extends Base {


public void display() {
show(); // Accessible because Derived is a subclass
}
}

Q4. Write short note on Input stream and Output Stream.


InputStream and OutputStream are abstract classes in Java used for reading and writing
byte streams.

InputStream: Used for reading data (input) from a source.


- Example classes: FileInputStream, BufferedInputStream
- Common methods: read(), close()

OutputStream: Used for writing data (output) to a destination.


- Example classes: FileOutputStream, BufferedOutputStream
- Common methods: write(), flush(), close()
Example:

import [Link].*;

public class StreamExample {


public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("[Link]");
[Link]("Hello, stream!".getBytes());
[Link]();

FileInputStream in = new FileInputStream("[Link]");


int i;
while ((i = [Link]()) != -1) {
[Link]((char) i);
}
[Link]();
}
}

You might also like