Upgrade to Pro — share decks privately, control downloads, hide ads and more …

M250 Unit 3 Subsection 2 - Data types and variables (primitive) (11)

Matt
January 11, 2020

M250 Unit 3 Subsection 2 - Data types and variables (primitive) (11)

Matt

January 11, 2020
Tweet

More Decks by Matt

Other Decks in Education

Transcript

  1. What is a variable? We can think of a variable

    as a named block of data. The data could contain many things: a very long sentence, the number 125, or even just the letter ‘a’. It doesn’t matter. A variable allows us to access the data. It gives us the means to use some stored data, or perhaps change the data to something else.
  2. The two kinds of Java variables In Java we deal

    with two types of variables: • Primitive type variables which store values of a primitive data type. • Reference type variables which store a reference to an object. We’ll just look at just primitive type variables for now.
  3. Primitive type variables Note: [Here we talk about “primitive type

    variables”, but in casual speech it is fine to just say “primitive variables”. Also we talk about “primitive data types”, however just saying “data type” is acceptable the context is there.] Whenever we want to store a value in a variable, we first have to declare to the variable. Firstly this means that we need to name it. And secondly we have to define what type of values it’s going to hold. Primitive variables are any variables that store values of a primitive data type. We need to declare a variable to be of the required data type for the values we want to store in it. So variables are of a certain types because the values we want to store in them are of certain types.
  4. Primitive data types Primitive data types are built-in to the

    Java language itself (hence the term primitive). There are four categories of primitive data types: • Integer types (for integer values) • Floating-point types (for decimal values) • A character type (for character values) • A boolean type (for the values of true/false only) When we’re talking about these different data types we’re actually talking about the possible types of values that exist. These values can be assigned to variables, but only to variables of the suitable primitive type.
  5. Integer types There are four kinds of integer types: •

    byte • short • int • long Each of these represent a different size of integer. In M250 we only care about int, and we virtually never talk about the others.
  6. Primitive variables of type int can store integer values that

    are positive, negative, or zero. Say we wanted the variable x to hold a value of 5, then we’d first need to declare x to hold an int value. That is, we’d need to declare that x can store values of the primitive data type int. Normally we’d just say something like “x holds an int value”.
  7. Floating-point types There are two kinds of floating-point types: •

    float • double The difference between them is in how many decimal places each can hold. In M250 we really only care about double and virtually never talk about float.
  8. Primitive variables of type double store decimal values. So, if

    we wanted the variable accountBalance to store the value 52.63, we’d need to declare accountBalance to be of data type double. Normally we’d just say something like “accountBalance holds a double value”.
  9. Character type There is just one character type: • char

    Primitive variables of type char store character values. For example, we might assign the variable lastLetter to ‘Z’, or the variable middleInitial to ‘B’. char values are placed in single quotation marks so that the compiler knows they are char values. Also case does matter. ‘r’ is not the same as ‘R’.
  10. boolean type There is just one boolean type: • boolean

    Primitive variables of type boolean hold either the value true or the value false. They cannot be assigned any other.
  11. What is a literal? All primitive variables hold values, but

    we don’t see these values themselves. We can’t see the value 27 for example, we instead just see a textual representation of the value 27. It looks like this: 27. Every value that can be stored in a primitive variable is represented by a literal. So when we see a primitive value returned as output we are really just seeing the literal representation of it. A literal simply represents its equivelent value textually. For example, if a variable of type double holds the value 23.2, then 23.2 is the literal equivalent, because that’s what we can see the value as. If a boolean variable holds the value true, then true is the literal. If a char variable holds the value ‘d’, then ‘d’ is the literal. So a literal simply represents the value of a primitive type variable. Literals also exists outside of variables. If I need to use a number in my code, for example 55 as part of a calculation, then that number is a literal. Or if I need to use a char, then that char is a literal.
  12. How to store a value in a primitive variable We

    need to do two things: 1] declare the variable 2] assign the variable a value
  13. To declare a primitive variable we simply write its data

    type and then an identifier (the name of the variable) as a statement. Remember, if it’s a statement it needs to end in a semi-colon. Examples: int aNumber; - declares the variable aNumber to be of primitive data type int. char aLetter; - declares the variable aLetter to be of primitive data type char. boolean isOvenOff; - declares the variable isOvenOff to be of primitive data type boolean.
  14. The idea of declaring a variable is that we are

    reserving space for the variable on the computer. So if we declare a char variable for example then we’re telling the computer (via the OS) to reserve space on the hard drive for a char value.
  15. Once we’ve declared a variable we can assign it a

    value. We do this using the assignment operator = Let’s say we’ve declare a variable with the statement: int x; Then to assign x the value 50 we can execute: x=50; It’s that simple. The value on the right-side of the assignment operator is copied to the variable on the left-side. x now holds the value of 50. We can change the value of this variable as many times as we want. If our next statement that we execute is: x=25; Then x now holds the value of 25.
  16. The value that we try to assign to variable must

    be compatible with its declared data type. If x is declared as an int, we can’t assign it the value ‘a’, or true, or 3.1415 because these are char, boolean, and floating-point values, respectively, and not int values. The compiler will not allow this to happen, which is a feature of Java called strong typing. Strong typing is when strong restrictions are put into place to stop values being assigned to variables that a declared as incompatible data type. If this were allowed to happen, errors could occur in the programme. So it’s a preventative measure.
  17. Declaring and assigning in one statement It’s can often be

    easier to both declare and assign a variable in the same statement. For example: int x = 22; This says, “declare the variable x to be of type int and assign it the value 22”. Note: Each variable must be uniquely named. We can’t declare a variable with a certain name if we’ve already done so.
  18. Question What value does the variable happy hold after the

    statements in each part are executed? (i) happy = 5; (ii) int happy = 5; (iii) int happy = 5; int happy = 25; (iv) int happy = 5; happy = 25;
  19. Answer (i) happy = 5; (ii) int happy = 5;

    (iii) int happy = 5; int happy = 25; (iv) int happy = 5; happy = 25; (i) Nothing. We never declared happy! There’s no variable to assign a value of 5 to. (ii) happy is declared and assigned a value of 5. (iii) happy holds a value of 5. The second statement won’t execute because we are attempting to declare another variable called happy, and one already exists. There’s no need to declare it a second time! (iv) happy holds a value of 25. This is what we wanted the last code to do. In the first statement we declare happy to be of type int and assign it the value 5, then in the second statement we assign happy the value 25.