In Java, a Reader
is an abstract class that represents a character stream that can be read. It is the base class for all classes that read character streams, such as FileReader
, InputStreamReader
, and StringReader
.
A Reader
object reads characters from the input source and stores them in a buffer. The buffer is then used to read characters one by one, until the end of the input source is reached.
The Reader
class provides several methods for reading characters from the input source, including read()
, read(char[] cbuf)
, read(char[] cbuf, int offset, int length)
, and skip(long n)
. These methods can be used to read characters into a buffer, skip characters, or read a single character at a time.
One of the benefits of using a Reader
is that it can handle different character encodings, such as UTF-8, UTF-16, and ISO-8859-1. This means that it can read text files in different languages and character sets without any issues.
Here’s an example of how to use a Reader
to read characters from a file:
import java.io.*; public class FileReaderExample { public static void main(String[] args) { try { Reader reader = new FileReader("myfile.txt"); int character; while ((character = reader.read()) != -1) { System.out.print((char) character); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
In this example, we create a Reader
object using the FileReader
class, which reads characters from a file. We then use a while
loop to read characters from the file one by one until the end of the file is reached. Finally, we close the Reader
object using the close()
method to release any system resources used by the object.
Fields:
In Java, a field is a variable that is declared as a member of a class. Fields represent the state of an object, which is the data that an object holds.
Fields can be declared with different access modifiers such as public
, private
, protected
, or default
. The access modifier determines whether the field can be accessed from within the class, from outside the class, or from within a subclass.
Here’s an example of a class with fields:
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
In this example, the Person
class has two fields, name
and age
, both of which are declared as private
. This means that they can only be accessed from within the Person
class.
The class also has getter and setter methods for the name
and age
fields, which allow other classes to read and modify the fields in a controlled manner.
To create an instance of the Person
class, we can use the following code:
Person person = new Person("John Doe", 30);
This creates a new Person
object with the name “John Doe” and age 30. We can then access the fields of the object using the getter methods:
String name = person.getName(); // returns "John Doe" int age = person.getAge(); // returns 30
We can also modify the fields using the setter methods:
person.setName("Jane Doe"); person.setAge(25);
This sets the name
field to “Jane Doe” and the age
field to 25.
Methods:
In Java, a method is a block of code that performs a specific task or action. A method can be thought of as a subprogram within a larger program that can be called and executed independently.
A method is defined within a class and has a name, a return type (if any), a list of parameters (if any), and a body of code. The method’s return type specifies the data type of the value that the method returns, if any. The list of parameters specifies the type and name of any data that the method needs to perform its task.
Here’s an example of a method:
public int add(int a, int b) { int sum = a + b; return sum; }
In this example, the method is called add
and takes two parameters of type int
, a
and b
. The method calculates the sum of a
and b
, stores the result in a local variable called sum
, and then returns the value of sum
.
To call this method, we can use the following code:
int result = add(5, 3);
This calls the add
method with the values 5 and 3 as the arguments for the a
and b
parameters, respectively. The method returns the sum of 5 and 3, which is 8, and this value is stored in the variable result
.
Methods can also have no return type, in which case they are declared with the void
keyword. Here’s an example:
public void printMessage(String message) { System.out.println(message); }
This method is called printMessage
and takes one parameter of type String
, message
. The method simply prints the value of message
to the console using the System.out.println()
method.
To call this method, we can use the following code:
printMessage("Hello, world!");
This calls the printMessage
method with the argument “Hello, world!”. The method prints this message to the console.
Example:
Here’s an example of a Java program that uses methods to calculate the area and perimeter of a rectangle:
public class Rectangle { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getArea() { return length * width; } public double getPerimeter() { return 2 * (length + width); } public static void main(String[] args) { Rectangle rectangle = new Rectangle(5.0, 3.0); System.out.println("Area: " + rectangle.getArea()); System.out.println("Perimeter: " + rectangle.getPerimeter()); } }
In this example, the Rectangle
class has two private fields, length
and width
, which represent the dimensions of the rectangle. The class also has getter and setter methods for these fields, which allow other classes to read and modify the fields in a controlled manner.
The class also has two methods, getArea
and getPerimeter
, which calculate the area and perimeter of the rectangle, respectively.
In the main
method, we create a new Rectangle
object with length 5.0 and width 3.0, and then print the area and perimeter of the rectangle to the console using the getArea
and getPerimeter
methods.
When we run this program, we get the following output:
Area: 15.0 Perimeter: 16.0
This demonstrates how methods can be used to encapsulate functionality within a class, making it easier to use and modify.