type wrapper in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
type wrapper
Java uses primitive types such as int, double or floats to hold the basic data types for the sake of performance. Despite the performance benefits offered by the primitive types, there is a situation when you will need an object representation. For example, many data structures in Java operate on objects so you cannot use primitive types with those data structures.
To handle these situations Java provides type Wrappers which provide classes that encapsulate a primitive type within an object.
- Character: It encapsulates the primitive type char within an object.
Character (char ch)
- Boolean: It encapsulates primitive type boolean within an object.
Boolean (boolean boolValue)
- Numeric type wrappers: It is the most commonly used type wrapper.
Byte Short Integer Long Float Double Above mentioned Classes comes under Numeric type wrapper. These classes encapsulate byte, short, int, long, float, double primitive type.
Autoboxing and Unboxing
- Autoboxing and Unboxing features were added in Java5.
- Autoboxing is a process by which primitive type is automatically encapsulated(boxed) into its equivalent type wrapper
- Auto-Unboxing is a process by which the value of an object is automatically extracted from a type wrapper.
Example of Autoboxing and Unboxing
class Test { public static void main(String[] args) { Integer iob = 100; //Autoboxing of int int i = iob; //unboxing of Integer System.out.println(i+" "+iob); Character cob = 'a'; /Autoboxing of char char ch = cob; //Auto-unboxing of Character System.out.println(cob+" "+ch); } }
Output :
100 100 a a
Autoboxing / Unboxing in Expressions
Whenever we use the object of Wrapper class in an expression, automatic unboxing and boxing are done by JVM.
Integer iOb;
iOb = 100; //Autoboxing of int
++iOb;
When we perform an increment operation on Integer object, it is first unboxed, then incremented and then again reboxed into Integer type object.
This will happen always when we will use Wrapper class objects in expressions or conditions etc.
Benefits of Autoboxing / Unboxing
- Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
- We don't have to perform Explicit typecasting.
- It helps prevent errors but may lead to unexpected results sometimes. Hence must be used with care.
Java performs I/O through Streams. A Stream is linked to a physical layer by Java I/O system to make input and output operation in java. In general, a stream means a continuous flow of data. Streams are a clean way to deal with input/output without having every part of your code to understand the physical.
Java encapsulates Stream under the java.io package. Java defines two types of streams. They are,
- Byte Stream: It provides a convenient means for handling input and output of byte.
- Character Stream: It provides a convenient means for handling input and output of characters. Character stream uses Unicode and therefore can be internationalized.
Byte Stream Classes
The byte stream is defined by using two abstract class at the top of a hierarchy, they are InputStream and OutputStream.
These two abstract classes have several concrete classes that handle various devices such as disk files, network connection etc.
Some important Byte stream classes.
Stream class | Description |
---|---|
BufferedInputStream | Used for Buffered Input Stream. |
BufferedOutputStream | Used for Buffered Output Stream. |
DataInputStream | Contains method for reading java standard datatype |
DataOutputStream | An output stream that contains a method for writing java standard data type |
FileInputStream | Input stream that reads from a file |
A fileoutputstream | Output stream that writes to a file. |
InputStream | Abstract class that describe stream input. |
OutputStream | Abstract class that describe stream output. |
PrintStream | Output Stream that containsprint() and println() method |
These classes define several key methods. Two most important are
read()
: reads a byte of data.write()
: Writes a byte of data.
Character Stream Classes
Character stream is also defined by using two abstract class at the top of a hierarchy, they are Reader and Writer.
These two abstract classes have several concrete classes that handle Unicode character.
Some important Character stream classes.
Stream class | Description |
---|---|
BufferedReader | Handles buffered input stream. |
BufferedWriter | Handles buffered output stream. |
FileReader | Input stream that reads from a file. |
A file writer | Output stream that writes to file. |
InputStreamReader | Input stream that translates byte to character |
OutputStreamReader | Output stream that translates character to byte. |
PrintWriter | Output Stream that containsprint() and methodprintln() . |
Reader | Abstract class that defines character stream input |
Writer | Abstract class that defines character stream output |
Reading Console Input
We use the object of BufferedReader class to take inputs from the keyboard.
Reading Characters
read()
a method is used with BufferedReader object to read characters. As this function returns integer type value as we need to use typecasting to convert it into char type.int read() throws IOException
Below is a simple example explaining character input.
class CharRead
{
public static void main( String args[])
{
BufferedReader br = new Bufferedreader(new InputstreamReader(System.in));
char c = (char)br.read(); //Reading character
}
}
Reading Strings
To read string we have to use a function
readLine()
with BufferedReader class's object.String readLine() throws IOException
Program to take String input from Keyboard in Java
import java.io.*;
class MyInput
{
public static void main(String[] args)
{
String text;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
text = br.readLine(); //Reading String
System.out.println(text);
}
}
Program to read from a file using BufferedReader class
import java. Io *; class ReadTest { public static void main(String[] args) { try { File fl = new File("d:/myfile.txt"); BufferedReader br = new BufferedReader(new FileReader(fl)) ; String str; while ((str=br.readLine())!=null) {System.out.println(str);
} br.close(); fl.close(); } catch (IOException e) { e.printStackTrace(); } } }
Program to write to a File using FileWriter class
import java. Io *; class WriteTest { public static void main(String[] args) { try { File fl = new File("d:/myfile.txt"); String str="Write this string to my file"; FileWriter fw = new FileWriter(fl) ; fw.write(str); fw.close(); fl.close(); }catch (IOException e)
{ e.printStackTrace(); } } }
Comments
Post a Comment