Module 3 Java
Module 3 Java
Output Classes
( By Santosh )
● Packages and Access Modifiers
● Access Protection
● Handling Strings
○ Create Strings
○ Operations on Strings
○ Wrapper Classes
● Input and Output Classes
○ File Classes
■ FilterInputStream
■ FilterOutputStream
● Basics of Networking
What is a Package?
In Java, a package is like a folder or directory that groups related classes and interfaces
together.
● Packages help avoid name conflicts. For example, you can have
com.companyA.project.Employee and com.companyB.project.Employee
without collision.
● They provide access protection (access modifiers interact with package boundaries).
● Domains are unique worldwide, so reversing them makes the package names globally
unique.
Example:
If your organization’s domain is example.com, the package name starts as:
package com.example.myproject;
package com.example.myproject.utils;
package com.example.myproject.models;
package com.example.myproject.services;
Example:
package com.example.shapes;
If omitted, the class goes into the default package, which is discouraged for larger projects.
Example:
export CLASSPATH=/home/user/classes:.
Used to access classes from other packages without typing their full package names every time.
Syntax:
import package_name.class_name;
Example:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.*;
● java.util: Utility classes like collections (ArrayList, HashMap), dates, random number
generation.
● java.nio: New I/O classes with buffer-oriented I/O and non-blocking I/O.
private Yes No No No No
Examples:
package com.example;
If another class in the same package accesses MyClass, it can see pubVar, protVar, and
defVar, but not privVar.
If another class is in a different package and is not a subclass, it can only access pubVar.
2. Handling Strings
Creating Strings
Operations on Strings
● Concatenation:
● Length:
● Check if empty:
● Trim whitespace:
● charAt(int index)
● equals(String another)
if(s1.equals(s2)) {
System.out.println("Equal");
}
● equalsIgnoreCase(String another)
● indexOf(String substr)
● lastIndexOf(String substr)
Extracts a substring.
Common methods:
Wrapper Classes
int Integer
char Character
boolean Boolean
double Double
Supports autoboxing/unboxing.
Example:
File Class
int data;
while((data = bis.read()) != -1) {
System.out.print((char)data);
}
bis.close();
fis.close();
4. Basics of Networking
TCP sockets:
Example server:
Example client:
out.println("Hello Server");
String response = in.readLine();
System.out.println("Server says: " + response);