Subscribe For Free Updates!

We'll not spam mate! We promise.

Thursday, May 16, 2013

DataTypes Examples


 

Java boolean Example
/*
    Java boolean Example
    This Java Example shows how to declare and use Java primitive boolean variable
    inside a java class.
*/


public class JavaBooleanExample {

    public static void main(String[] args) {

        /*
         * boolean is simple Java type which can have only of two values; true or false.
         * All rational expressions retrun this type of value.
         *
         * Declare boolean varibale as below
         *
         * boolean <variable name> = <default value>;
         *
         * here assigning default value is optional.
         */

        boolean b1 = true;
        boolean b2 = false;
        boolean b3 = (10 > 2)? true:false;

        System.out.println("Value of boolean variable b1 is :" + b1);
        System.out.println("Value of boolean variable b2 is :" + b2);
        System.out.println("Value of boolean variable b3 is :" + b3);       
    }
}


/*
Output would be
Value of boolean variable b1 is :true
Value of boolean variable b2 is :false
Value of boolean variable b3 is :true
*/

Java byte Example
/*
    Java byte Example
    This Java Example shows how to declare and use Java primitive byte variable
    inside a java class.
*/


public class JavaByteExample {

    public static void main(String[] args) {

        /*
         * byte is smallest Java integer type.
         * byte is 8 bit signed type ranges from –128 to 127.
         * byte is mostly used when dealing with raw data like reading a
         * binary file.
         *
         * Declare byte varibale as below
         *
         * byte <variable name> = <default value>;
         *
         * here assigning default value is optional.
         */

         byte b1 = 100;
         byte b2 = 20;

        System.out.println("Value of byte variable b1 is :" + b1);
        System.out.println("Value of byte variable b1 is :" + b2);
    }
}


/*
Output would be
Value of byte variable b1 is :100
Value of byte variable b1 is :20
*/

Java char Example
/*
    Java char Example
    This Java Example shows how to declare and use Java primitive char variable
    inside a java class.
*/


public class JavaCharExample {

    public static void main(String[] args) {

        /*
         * char is 16 bit type and used to represent Unicode characters.
         * Range of char is 0 to 65,536.
         *
         * Declare char varibale as below
         *
         * char <variable name> = <default value>;
         *
         * here assigning default value is optional.
         */

        char ch1 = 'a';
        char ch2 = 65; /* ASCII code of 'A'*/

        System.out.println("Value of char variable ch1 is :" + ch1);   
        System.out.println("Value of char variable ch2 is :" + ch2);       
    }
}


/*
Output would be
Value of char variable ch1 is :a
Value of char variable ch2 is :A
*/

Java double Example
/*
    Java double Example
    This Java Example shows how to declare and use Java primitive double variable
    inside a java class.
*/


public class JavaDoubleExample {

    public static void main(String[] args) {

        /*
         * double is 64 bit double precision type and used when fractional precision
         * calculation is required.
         *
         * Declare double varibale as below
         *
         * double <variable name> = <default value>;
         *
         * here assigning default value is optional.
         */

        double d = 1232.44;
        System.out.println("Value of double variable d is :" + d);       
    }
}
 

/*
Output would be
Value of double variable f is :1232.44
*/

Java int Example
/*
    Java int Example
    This Java Example shows how to declare and use Java primitive int variable
    inside a java class.
*/


public class JavaIntExamples {

    public static void main(String[] args) {

        /*
         * int is 32 bit signed type ranges from –2,147,483,648
         * to 2,147,483,647. int is also most commonly used integer
         * type in Java.
         * Declare int varibale as below
         *
         * int <variable name> = <default value>;
         *
         * here assigning default value is optional.
         */

         int i = 0;
         int j = 100;

         System.out.println("Value of int variable i is :" + i);
         System.out.println("Value of int variable j is :" + j);
    }
}


/*
Output would be
Value of int variable i is :0
Value of int variable j is :100
*/

Java long Example
/*
    Java long Example
    This Java Example shows how to declare and use Java primitive long variable
    inside a java class.
*/


import java.util.*;

public class JavaLongExample {

    public static void main(String[] args) {

        /*
         * long is 64 bit signed type and used when int is not large
         * enough to hold the value.
         *
         * Declare long varibale as below
         *
         * long <variable name> = <default value>;
         *
         * here assigning default value is optional.
         */

        long timeInMilliseconds = new Date().getTime();
        System.out.println("Time in milliseconds is : " + timeInMilliseconds);       
    }
}


/*
Output would be
Time in milliseconds is : 1226836372234
*/

Java short Example
/*
    Java short Example
    This Java Example shows how to declare and use Java primitive short variable
    inside a java class.
*/


public class JavaShortExample {

    public static void main(String[] args) {

        /*
         * short is 16 bit signed type ranges from –32,768 to 32,767.
         *
         * Declare short varibale as below
         *
         * short <variable name> = <default value>;
         *
         * here assigning default value is optional.
         */

         short s1 = 50;
         short s2 = 42;

        System.out.println("Value of short variable b1 is :" + s1);
        System.out.println("Value of short variable b1 is :" + s2);
    }
}


/*
Output would be
Value of short variable b1 is :50
Value of short variable b1 is :42
*/

Socializer Widget
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment