Chapter 2 Flashcards
What are identifiers in Java?
Identifiers are the names that identify elements such as classes, methods, and variables in a program.
What can an identifier be made up of?
An identifier can be made up of letters, digits, the underscore character (_), and the dollar sign ($).
How must an identifier start in Java?
An identifier must start with a letter (A-Z, a-z), an underscore (_), or a dollar sign ($). It cannot begin with a digit.
Is Java case sensitive when it comes to identifiers?
Yes, Java is case sensitive. Total
, total
, and TOTAL
are different identifiers.
What is the convention for naming class identifiers in Java?
By convention, class names use title case (e.g., Lincoln
).
What is the convention for naming constants in Java?
By convention, constants are written in upper case (e.g., MAXIMUM
).
What is lower camel case in Java?
Lower camel case is when the first letter of each word is uppercase, except the first one (e.g., dayOfWeek
).
What is the convention for naming method and variable identifiers in Java?
Methods and variables start with a lowercase letter (e.g., myMethod()
).
What do variables do in Java programs?
Variables store data in memory and are used to “remember” information in the program.
What are the components of a variable in Java?
A variable has a name, data type, and value.
Example: int count = 1;
What must a variable declaration specify in Java?
A variable declaration must specify the variable’s name and the type of information it will hold.
What is an example of declaring multiple variables in one statement?
```java
int total = 0, count = 0, temp = 0, result = 0;
~~~
What are global variables in Java?
Global variables exist throughout the entire program’s life and are accessible from any part of the program.
What are local variables in Java?
Local variables exist only temporarily, until the block or bracket in which they were declared is closed.
What is the general programming practice regarding global variables in Java?
It is generally good programming practice to avoid using global variables, but sometimes they are necessary.
This is for the sake of preserving memory
what does it mean to initialize a variable?
it means to give it its initial value
Why do we need to specify a data type when creating a variable in Java?
A data type is required so the system can reserve the appropriate amount of memory for the variable.
What are the two basic groupings of data types in Java?
The two basic groupings of data types are:
- Primitive Data Types
- Class Data Types (e.g., Strings)
What are primitive data types in Java?
Primitive data types, also known as ‘Built In’ data types, are the building blocks of data manipulation in Java. They are stored in memory directly with their value.
What is the default value for primitive data types in Java?
The default value for primitive data types is 0.
Are primitive data types in Java case sensitive?
Yes, primitive data types in Java are all lowercase. String is not a primitive type.
How many primitive data types are there in Java?
There are 8 primitive data types: byte, char, short, int, long, float, double, and boolean.
What are the two categories of primitive numeric data types in Java?
The two categories are:
- Integers: byte, short, int, long
- Floating-point numbers: float, double
What is the difference between float
and double
in Java?
float
: Stores decimal numbers with single precision (e.g., 3.14159).double
: Stores decimal numbers with double precision and more decimal places.
What is the purpose of the char
data type in Java?
The char
data type is used to store individual characters such as ‘a’, ‘b’, ‘c’, etc.
What does the boolean
data type represent in Java?
The boolean
data type can store two possible values: true or false.
What is the size of byte
, short
, int
, and long
in terms of bits?
byte
: 8 bitsshort
: 16 bitsint
: 32 bitslong
: 64 bits
What is the size of float
and double
in terms of bits?
float
: 32 bitsdouble
: 64 bits
What is the range of values for the byte
, short
, int
, and long
types?
byte
: -128 to 127short
: -32,768 to 32767 int
: -2,147,483,648 to 2,147,483,647long
: Less than -9 x 10^18 , around 9 x 10^18
What are the ranges for the float
and double
data types?
float
: +/- 3.4 x 10^38 with 7 significant digitsdouble
: +/- 1.7 x 10^308 with 15 significant digits
What is a numeric literal in programming?
A literal is a constant that appears directly in a program.
Example: int numberOfYears = 34;
What happens if you assign a value that exceeds the range of a variable’s data type?
If the value exceeds the range, it will cause a compile error.
For example, byte b = 128;
causes a compile error because byte
can only hold values from -128 to 127.
How can floating-point literals be written in Java?
Floating-point literals can be written in scientific notation in the form of:
a x 10^b
Can a char
variable hold multiple characters?
No, a char
variable holds only one character, while a String
object can hold multiple characters.
What does the boolean
data type represent?
The boolean
data type represents a true or false condition.
What can a boolean
variable be used to represent?
A boolean
variable can represent any two states, such as a light bulb being on or off.
What is a String
data type in Java?
A String
is a data type that represents a sequence of characters surrounded by quotes.
How does Java treat everything inside a String
?
Everything inside a String
is printed, including mathematical operations.
Example: String subjects = "We are learning Java and Data structures";
What are the two ways to create a String
object in Java?
- By string literal:
String name = "Alex";
(does not create a new instance) - By the
new
keyword:String s = new String("Hello");
(creates a new object)
What operator is used for string concatenation in Java?
The plus (+
) operator is used to concatenate two or more strings.
How do you concatenate two strings in Java?
You can concatenate strings using the +
operator.
Example:String firstName = "John";
String lastName = "Allen";
String fullName = firstName + lastName;
Can a string literal be broken across two lines in a program?
No, a string literal cannot be broken across two lines in a program.
Can numeric values be concatenated with a string?
Yes, numeric values can be concatenated with a string using the +
operator.
Will add values if in paratheses
How does the +
operator behave when one operand is a string and the other is a number?
If one operand is a string and the other is a number, the +
operator performs string concatenation.
How does the +
operator behave when both operands are numeric?
If both operands are numeric, the +
operator performs arithmetic addition when enclosed by parantheses.
otherwise they are concatenated
What happens if you try to change the value of a constant in Java?
The compiler will issue an error if you try to change the value of a constant.
How do you declare a constant in Java?
You declare a constant in Java using the final
modifier.
Example: final int MIN_HEIGHT = 69;
Why are constants useful in programming?
Constants are useful for three important reasons:
1. They give meaning to unclear literal values.
2. They facilitate program maintenance.
3. They formally establish that a value should not change.
How do constants make program maintenance easier?
Constants facilitate program maintenance by ensuring that if a constant is used in multiple places, its value needs to be updated only in one place.
What actually happens when you declare a variable in Java?
When you declare a variable, you give it a name, and the compiler allocates a memory location for that variable, based on its type. The memory location is called the variable’s address.
Where are expressions commonly found in an assignment statement?
Expressions are usually on the right side of an assignment statement.
List the five arithmetic operators in Java.
+
(Addition), -
(Subtraction), *
(Multiplication), /
(Division), %
(Remainder)
What happens if either operand in an arithmetic expression is a floating point?
The result will be a floating point.
What is a relational expression? Give an example.
A relational expression compares values. Example: boolean isEqual = (5 == 5);
What is a logical expression? Give an example.
A logical expression combines boolean values. Example: boolean result = (5 > 3) && (2 < 4);
What is the order of precedence for evaluating expressions in Java?
PEMDAS
What is JShell?
JShell is a command-line tool for quickly evaluating expressions and executing statements.
What is JShell commonly known as?
It is commonly known as REPL, which stands for Read-Evaluate-Print-Loop.
What is data conversion in Java?
Data conversion is the process of converting data from one type to another, often for computation purposes.
Does data conversion change the type of a variable?
No, data conversion does not change the type of a variable; it only converts a value during a computation.
Why must data conversions be handled carefully?
To avoid losing information, especially in narrowing conversions.
What are the two types of conversions in Java?
Widening conversions (safe, small to large type) and Narrowing conversions (may lose information, large to small type).
What are the three ways data conversion can occur in Java?
Assignment conversion, promotion, and casting.
What is assignment conversion?
When a value of one type is assigned to a variable of another type, but only widening conversions can happen this way.
Can Boolean values be converted to other types in Java?
No, Boolean values cannot be cast to other types.
What is promotion in Java?
When operators in expressions automatically convert operands to a compatible type.
What is widening conversion?
A type conversion where a smaller data type is converted into a larger data type automatically.
List an example of widening conversion.
byte → short → int → long → float → double
What is an example of automatic type conversion in Java?
```java
int i = 100;
long l = i; // automatic conversion
float f = l; // automatic conversion
~~~
What is narrowing conversion?
A type conversion where a larger data type is converted into a smaller data type, which may result in data loss.
List an example of narrowing conversion.
double → float → long → int → short → byte
What is an example of explicit type casting in Java?
```java
double d = 100.04;
long l = (long) d; // explicit casting
int i = (int) l; // explicit casting
~~~
What is casting in Java?
A technique to convert one data type to another by placing the target type in parentheses before the value.
Give an example of casting for floating point division.
```java
float result = (float) total / count;
~~~