Defining variables

  • What happens inside the computer when you define a variable int a:

      1. The computer will find some used memory cells and mark them as used   (a.k.a.: allocate memory)

      2. Then the computer equates (= "records") the name a to the memory location of the reserved cells

    Schematically:

Data type of a variable

  • Each variable has a data type

  • The data type specifies the kind of information

  • Each kind of information has its own way of representation

    The way of representation is called a code

  • Encoding method of a data type

      • Information of a data type are represented by binary numbers

      • The encoding method defines the way to interpret a binary number

  • My way to explain "data type":

      • Data type provide the context to interpret a number and obtain the meaning of the number

Example that context in necessary to interpret a number value

  • The data type determines how the Java program will interpret the value 65:

    public class datatype
    {
        public static void main(String[] args)
        {
            int x = 65;
            System.out.println(x); // Print 65 as an integer
    
            char y = 65;
            System.out.println(y); // Print 65 as Unicode
        }
    }

  • Conclusion:

    • The date type provides the context on how to interpret a number inside the computer

DEMO: demo/02-review/datatype.java

The reason why 65 is the letter A is the Unicode used to represent symbols:

Example of the effect of the context in the English language

  • Make a correct English sentence that begins with:

       You is ....
    

  • Answer:

       You is ....
    

Example of the effect of the context in the English language

  • Make a correct English sentence that begins with:

       You is ....
    

  • Answer:

       You is a word in the English language
    

  • Explanation:

    • "You" is usually used as a personal pronounce (= context)

    • You must not use the word "you" in this context to solve this puzzle

Java's primitive data types

Data type Encoding method
byte   2's complement encoding using 8 bits
short   2's complement encoding using 16 bits
int   2's complement encoding using 32 bits
long   2's complement encoding using 64 bits
float   IEEE 754 encoding using 32 bits
double   IEEE 754 encoding using 64 bits
char   Unicode encoding using 16 bits
boolean   Enumeration encoding using 0 = false and 1 = true

You will learn about these encoding methods in CS 255