Java unit 2
Java unit 2
Unit
Topics
Class
Implement the `Comparable` interface in the `Video` class so that videos can be
compared based on their number of views. The natural ordering should be from the
highest number of views to the lowest.
Comparator Interface:
Comparable interface is mainly used to sort the arrays (or lists) of custom objects. Lists
(and arrays) of objects that implement Comparable interface can be sorted automatically
by Collections.sort (and Arrays.sort).
Purpose: The Comparator interface is used to define multiple different orders for
objects of a class. It is useful when you want to sort objects in various ways.
Method: It has a single method: compare(T o1, T o2)
Signature: int compare(T o1, T o2);This method compares two specified objects and
returns
A negative integer if the first object is less than the second object.
Zero if the first object is equal to the second object.
A positive integer if the first object is greater than the second object.
Implementation:
Implement this interface in a separate class or use lambda expressions or method references if
you’re using Java 8 or later.
Comparator Interface:
Student Marks Sorter
Comparator Interface:
Observer Interface
Types of Streams:
There are two main categories of streams in Java for I/O
operations
Byte Stream
Character Stream
I/O OPERATIONS
Byte streams in Java are designed to provide a convenient way for handling the input
and output of bytes (i.e., units of 8-bits data). We use them for reading or writing to
binary data I/O.
A byte stream is suitable for any kind of file, however not quite appropriate for text files
Byte streams in Java operate on binary data, allowing you to read and write data as
individual bytes. They are commonly used for working with non-textual data such as
images, audio files, or serialized objects.
Types of byte streams in Java include
FileInputStream and FileOutputStream for reading and writing bytes to files,
BufferedInputStream and BufferedOutputStream for efficient buffered operations,
and
DataInputStream and DataOutputStream for handling different data types.
There are many classes related to byte streams but the most frequently used classes
are ,
BYTE STREAM
INPUT/OUTOUT STREAMS:
INPUT/OUTOUT STREAMS:
CHARACTER STREAM
BufferedWriter maintains an internal buffer to store characters before writing them to the actual
output stream. This minimizes the number of I/O operations, making the writing process faster.
Performance: By reducing the number of I/O operations, BufferedWriter can significantly improve
performance, especially when writing large amounts of data.
Convenience: Provides methods like newLine() to easily write platform-specific line separators.
Count the number of characters
in the file
RANDOM ACCESS FILE CLASS
This class allows the random movement of the file pointers. The file
pointer can
be moved back and forth.
FUNCTIONALITIES OF RANDOM ACCESS FILE CLASS
Reading and Writing: You can read bytes of data from any position
in the file or write data at any position, overwriting existing data if
necessary.
File Pointer: A file pointer acts like a cursor within the file, keeping
track of the current position for read/write operations. You can
manipulate the file pointer to move around the file.
Since you now know what serialization in Java is, and all the relevant
points, let’s dwelve deep into how to serialize an object. You must use the
writeObject() method of the ObjectOutputStream class for serialization and
readObject() method of the InputObjectStream class for deserialization
purpose.
DATES ,CALENDAR, NUMBERS AND CURRENCIES IN JAVA
Key Classes:
java.util.Date: Older class representing a specific point in time
(deprecated in Java 9 and later).
java.time.LocalDate: Newer class representing a date
without time (recommended).
java.time.LocalDateTime: Class representing a date and
time with
optional nanoseconds.
java.time.ZonedDateTime: Class representing a date and time
with time zone information.
DATES ,CALENDAR, NUMBERS AND CURRENCIES
IN JAVA
Numbers:
Java SimpleDateFormat
• The java.Text.Simpledateformat class provides methods to format and parse date and time in java.
The simpledateformat is a concrete class for formatting and parsing date which inherits
java.Text.Dateformat class.
• Notice that formatting means converting date to string and parsing means converting string to
date.
import java.text.SimpleDateFormat;
import java.util.Date;
Output : 01/08/2024
SimpleDateFormat(String pattern_args):
SimpleDateFormat(String pattern_args, Locale
locale_args):
Calendar Class:
Calendar methods:
• The Calendar is a built-in abstract class in java used to convert date between a specific
instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. The Calendar
class is available inside the java.util package.
As the Calendar class is an abstract class, we cannot create an object using it.