Sunday 7 June 2015

Variables, Fields and Primitive Data Types

class Man {
        int age = 36;
        static String sex = "Male";
        static final int numEyes = 2;
}

public class variablesAndFields {

        /*
         * The Java language has four different kinds of variable.
         * Parameters, local variables, static and non-static fields.
         * Also known as class variables and instance variables
         * respectively.
         * 
         * Variables will also be one of eight primitive data types.
         * Which are byte, short, int, long, float, double, boolean
         * and char.
         * 
         * String is not a primitive data type. It is an object provided
         * by the java.lang.String class.
         */

        public static void main(String[] args) {
                /* Literals
                 * Literals do not need to be defined with the keyword
                 * "new" as they are part of the language and not objects.
                 */
                boolean result = true;
                char character = 'A';
                byte b = 100;
                short s = 1000;
                int i = 100000;
                float pi = 3.14F;

                // We can reference locally declared variables directly.
                System.out.println("Primitive Data Types");
                System.out.println("boolean: " + result);
                System.out.println("char: " + character);
                System.out.println("byte: " + b);
                System.out.println("short: " + s);
                System.out.println("int: " + i);
                System.out.println("float: " + pi);

                /* A variable becomes a field when we try to access it
                 * from "outside" the class or object.
                 */
                Man Bob = new Man();
                Man Joe = new Man();

                System.out.println("\nPrinting the content of a field.");
                System.out.println("Bob.age has a value of " + Bob.age);
                System.out.println("Joe.age has a value of " + Joe.age);

                /* If a field is defined with the "static" keyword
                 * only one instance of that field will exist no matter
                 * however many iterations of the object exist.
                 * 
                 * So if we change object Bob's sex to "female" then
                 * object Joe's sex will also change to "female" because
                 * they are referencing the same instance of the same field.
                 */
                System.out.println("Bob is a " + Bob.sex);
                System.out.println("Joe is a " + Joe.sex);

                Bob.sex = "Female"; // This affect Bob and Joe.
                Joe.age = 50; // This will only affect Joe.

                System.out.println("\nBob is a " + Bob.sex);
                System.out.println("Joe is a " + Joe.sex);
                System.out.println("Bob.age has a value of " + Bob.age);
                System.out.println("Joe.age has a value of " + Joe.age);

                /* If the "final" keyword is added. That variable or field
                 * cannot be changed.
                 * 
                 * Remove the // from the line below and the program will
                 * not run because numEyes was defined in the Man class
                 * with the "final" keyword.
                 */
                //Bob.numEyes = 30;
                System.out.println("\nBob has " + Bob.numEyes + " eyes.");

        }
}

No comments:

Post a Comment

Translate