type wrapper in java

type wrapper in java
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.
type wrapper in java

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.
    ByteShortIntegerLongFloatDouble
    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

  1. Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
  2. We don't have to perform Explicit typecasting.
  3. It helps prevent errors but may lead to unexpected results sometimes. Hence must be used with care.
IO Stream in java

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.
IO Stream in java

Java encapsulates Stream under the java.io package. Java defines two types of streams. They are,
  1. Byte Stream: It provides a convenient means for handling input and output of byte.
  2. 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.
IO Stream in java













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 classDescription
BufferedInputStreamUsed for Buffered Input Stream.
BufferedOutputStreamUsed for Buffered Output Stream.
DataInputStreamContains method for reading java standard datatype
DataOutputStreamAn output stream that contains a method for writing java standard data type
FileInputStreamInput stream that reads from a file
A fileoutputstreamOutput stream that writes to a file.
InputStreamAbstract class that describe stream input.
OutputStreamAbstract class that describe stream output.
PrintStreamOutput Stream that containsprint() and println() method
These classes define several key methods. Two most important are

  1. read() : reads a byte of data.
  2. 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.
Character Stream Classes













These two abstract classes have several concrete classes that handle Unicode character.

Some important Character stream classes.

Stream classDescription
BufferedReaderHandles buffered input stream.
BufferedWriterHandles buffered output stream.
FileReaderInput stream that reads from a file.
A file writerOutput stream that writes to file.
InputStreamReaderInput stream that translates byte to character
OutputStreamReaderOutput stream that translates character to byte.
PrintWriterOutput Stream that containsprint() and methodprintln().
ReaderAbstract class that defines character stream input
WriterAbstract class that defines character stream output

Reading Console Input

We use the object of BufferedReader class to take inputs from the keyboard.
Reading Console Input











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 functionreadLine() 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