Sunday, 5 June 2016

How to read byte array using ByteArrayInputStream?


ByteArrayInputStream keep maintains a buffer and tracks the next byte to be supplied. With ByteArrayInputStream we can read bulk data and buffer is helpful for faster response.


Example Code:

package snippets11;
import java.io.ByteArrayInputStream;

/**
 * Created by root on 15/3/16.
 */
public class ByteArrayInputStreamExample {
    public static void main(String[] args) {
        String string="Programming World...";
        byte[] bytes=string.getBytes();

        ByteArrayInputStream ba=new ByteArrayInputStream(bytes);
        int ch;
        while((ch=ba.read())!=-1){
            System.out.print((char)ch);
        }
    }
}

click here.

basic data types in java

Data types in java are two type' s

  1. Primitive 
  2. Reference
Mostly Used Primitive Data Type' s
  1. boolean 
  2. char
  3. int
  4. byte
  5. float
  6. long
  7. double
  8. String
Data Type: boolean
Bytes: 1 bit.
Description: this data type represents 1 bit information, mostly used in the true/false conditions.
Possible values: true/false 


Data Type: char
Bytes: 16 bit unicode.
Description: this data type used to store character' s.

Data Type: int 
Bytes: 32 bit signed representation.
Description: this data type used to store integer of signed representation.

Data Type: byte 
Bytes: 8 bit signed representation.
Description: this data type used to store integer of signed representation, this type is useful when we need to store an array of integer's with max of 8 bit' s. so, we can save 24 bit' s when compare to the int data type.


Data Type: float 
Bytes: 32 bit
Description: this data type used to store integer with single-precision/decimal values.


Data Type: long
Bytes: 64 bit signed representation.
Description: this data type used to store integer with single-precision/decimal values.


Data Type: double
Bytes: 64 bit signed representation.
Description: this data type used to store integer with single-precision/decimal values.

Data Type: String
Bytes: 64 bit signed representation.
Description: this data type used to store integer with single-precision/decimal values.


Example code:
package snippets1;

/**
 * Created by root on 11/3/16.
 */
public class DataTypeTest {

    public static void main(String args[]){

        boolean status = true;
        char a = 'A';
        int b = 12;
        byte e = 12;
        float f=3.14f;
        long c = 1235;
        double d = 12345.23;
        String str = "programming";

        System.out.println("boolean: "+status+"\nchar: "+a+"int: "+b+"\nbyte: "+e+"\nfloat: "+f+"\nlong: "+c+"\ndouble: "+d);
    }
}