Sunday, 5 June 2016

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);
    }
}

Command line example in java

Passing command line arguments in java

1. check the number argument's passed, exit the program with status success when you received insufficient command line arguments.

        if(args.length==0){
            System.out.println("Insufficient Arguments..");
            System.exit(0);
        }

2.  Parse the arguments to Integer from String.
      System.out.print("sum="+Integer.parseInt(arg1)+Integer.parseInt(arg2));

for complete code click here.