Week 3 - Data Types and Variables Flashcards
What are the two categories of data types in Java?
they are primitive data types and reference data types.
Give example values of primitive data types (4 in total), and explain their usage.
they include numbers( both integers and floats) , Boolean Values and characters like ‘?’, ‘G’ and ‘b’. The primitive types are built into the language and so are available to a programmer for use in constructing more complex data types.
What are reference data types?
these types are defined by classes and are used in relation to objects. (they are used to store an object reference).
What is an IDE?
an Integrated Development Environment.
How many integer types are there? Name them.
There are 4 integer types:
byte - 8bits of memory, smallest, -128 to 127
short - 16 bits, -32768 to 32767
int - 32 bits
long - 64 bits
The values of each of these types are positive and negative whole numbers and 0.
The differences between these types are in the range of values they can represent and the amount of memory each requires.
How does Java implicitly sign numeric types? What does this mean?
It means that each numeric type has a positive or negative value,.
Java uses the leftmost bit of the number expressed in binary to hold the ‘sign’ 0 indicates a positive and 1 indicates a negative.
What are the two floating type points that are used?
float: takes up 32 bits of memory
double: takes up 64 bits of memory. (greater accuracy than float.)
How many bits are required for a character type?
16 bits, this is for the one and only character primitive data type, char.
What is a primitive type variable?
they are variables declared to hold values of some primitive data type, they can also be referred to as primitive variables or a value type variable.
What is strong typing?
it is a feature of programming languages, it is a set of rules that make it impossible to accidentally use incompatible types, for example trying to store 1.7 in an int .
Write out 2 java statements, one which will declare a character variable called letter and then another that will assign the character ‘D’ to the variable.
char letter;
letter = ‘D’;
Describe the three steps that take place when the statement
kermit = new Frog ( );
is executed.
First, the keyword new causes a new instance of Frog to be created.
Second, the constructor Frog () causes the new instance of Frog to have its position instance variable initialised to 1 and its colour instance variable initialised to OUColour.GREEN.
Finally, the assignment operator = causes a reference to the new, initialised instance of Frog to be assigned to the reference variable kermit.
After these steps we can also more simply say that kermit references a Frog object.
Which of the following are valid variable identifiers in Java? Give a reason in each of the cases where you consider that the text is not an identifier.
a) kermit
b) hover frog
c) hoverfrog
d) my+Frog
e) myAcc
f) 3.2
g) “iAmAVariable”
h) MyNumber
i) $myMoney
a)
b) has a space
c)
d) has a + sign
e)
f) starts with an integer also not very informative
g) starts with “
h) ok but starts with a capital so could be confused with a class
i) ok but does not conform to M250 rules.
What is an expression?
An expression is a piece of code that can be evaluated to produce a single value.
When would integer division occur and what is it?
It would occur when one integer is divided by another, Java would drop the decimal and give an integer answer, in order to get an accurate answer you would need to make one of the arguments a double.
for example 7/2 would evaluate to 3
but 7.0/2 would evaluate to 3.5