Chapter 2 Flashcards
What is a statement?
A single command that causes something to happen
What punctuation mark terminates every statement in Java?
a semicolon (;)
A group of statements between an opening brace – { – and a closing brace – } – is called a…
block, or block statement
What is the difference between an instance variable and a local variable?
An instance variable defines an attribute of an object.
A local variable is used inside a method definition, or within even smaller blocks of statements inside the method. They cease to exist afterwards.
How would you declare a variable named “loanLength” that will hold an integer representing the length of a loan?
int loanLength;
How would you declare a variable named “message” that will hold text?
String message;
What is the most efficient way of declaring three String variables called “street,” “city” and “state”?
String street, city, state;
If you declare a Boolean variable without specifying its value, what initial value is it given automatically?
A value of “false”
How do you declare an integer variable called “box” that has an initial value of 200?
int box = 200;
Explain what would be wrong about declaring a variable like the following:
int 2013spending = 3000;
Variables may not begin with numbers.
They may begin with a letter, underscore character or dollar sign.
Describe the formatting convention that most Java programmers use when naming a variable.
Join several words together that describe the variable’s content.
Use lowercase for the first letter, then capitalize each successive word.
Why are data types such as integers and Booleans called “primitive” types?
They are built-in parts of the Java language. They are not objects, which makes them easier to create and use.
Since byte, short, int and long are all data types for storing integers, when might you set up a variable as a “byte” data type rather than an “int”?
A “byte” variable, which is limited to the range of -128 to 127, would use less memory. But since memory is more plentiful in today’s computers, it’s better to use larger data types like int.
What is the largest number that an “int” variable can hold?
just over 2 billion (2,147,483,647)
What are floating-point numbers?
Numbers with a decimal point