2.4 Primitive Data Types
floating point numbers
A number that can have a fractional part.
How can you force this number to be treated as a float variable instead of a double value? float number; number = 23.5;
Add an "F" to the end of the number. float number; number = 23.5F;
Do you enclose String literals in single or double quotations?
Double quotations.
True or False: You can use commas in numeric literals.
False
True or False: String literals can be assigned to char variables.
False.
True or False: Double data type uses half the memory of a Float data type.
False. Double data type uses double the memory of a float data type. However, A float variable occupies 4 bytes of memory, whereas a double variable uses 8 bytes.
Fill in the Unicode codes for the characters below. The code for 'C' is: The code for 'F' is: The code for 'W' is:
The code for 'C' is: 67 The code for 'F' is: 70 The code for 'W' is: 87
True or False: boolean variables may hold only the value true or false.
True.
Boolean data type
a variable that is either true or false.
Can you use dollar signs ( $ ) or commas ( , ) when working with literals?
No, you can only use a period.
Declare a character variable named c.
char c;
Write a statement that declares a char variable named letter.
char letter;
Declare two double variables, one named length with a value of 3.5 and the other named width with a value of 1.55.
double length = 3.5; double width = 1.55;
Declare a double variable named netWeight.
double netWeight;
The following statement declares a float variable named number. The code causes an error because of two issues. Rewrite the statement correctly.
float number = 7.4f;
Declare a float variable named price.
float price;
Declare two integer variables named profitStartOfQuarter and cashFlowEndOfYear.
int profitStartOfQuarter, cashFlowEndOfYear;
Write a statement that displays the content of the letter variable.
System.out.println(letter);
E notation
exponential notation. The E can be upper or lowercase.
If a variable needs to hold whole numbers in the range -40,000 to +40,000,what primitive data type would hold the value and use the least amount of memory?
int
Declare an integer variable cardsInHand and initialize it to 13.
int cardsInHand = 13;
Refer to the Java primitive data types listed in Table 2-5 for these questions. If a variable needs to hold whole numbers in the range 32 to 6,000, which one would hold the value and use the least amount of memory?
short
Each Unicode requires how many bytes of memory?
two bytes of memory, so char variables occupy two bytes.
Unicode
which is a set of numbers that are used as codes for representing characters.
data type
which is the type of data that the variable can hold.
strongly typed language
which means that it only allows you to store values of compatible data types in variables.
Primitive data type: byte
1 byte; Integers in the range of −128 to +127
Primitive data type: short
2 bytes; Integers in the range of −32,768 to +32,767
What is the E Notation of 247.91?
2.4791E2
double data type
is considered a double precision data type. It can store a floating-point number with 15 digits of accuracy.
float data type
is considered a single precision data type. It can store a floating-point number with 7 digits of accuracy.
char data type
is used to store characters. A variable of the char data type can hold one character at a time.
Write a statement that assigns the letter A as a character literal to the letter variable.
letter = 65; letter = 'A'
What is the E Notation of 2,900,000?
2.9E6
Which literal will use more memory?
22.1
Primitive data type: float
4 bytes; Floating-point numbers in the range of ±3.4 × 10−38 to ±3.4 × 1038 with 7 digits of accuracy
Primitive data type: int
4 bytes; Integers in the range of −2,147,483,648 to +2,147,483,647
Write the number 6.31 * 10^17 as it would be represented in E notation.
6.31E17
What is the ascii value of A, B & C?
65, 66 & 67
What is the E Notation of 0.00072?
7.2E-4
Primitive data type: double
8 bytes; Floating-point numbers in the range of ±1.7 × 10−308 to ±1.7 × 10308 with 15 digits of accuracy
Primitive data type: long
8 bytes; Integers in the range of −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
True or False: The contents of a boolean variable may be copied to a variable of any type other than boolean.
False.
Declare a variable isACustomer suitable for representing a TRUE or FALSE value.
boolean isACustomer;
Do you enclose Character literals in single or double quotations?
Single quotations.
Write a statement that defines a bool variable named finished.
Write a statement that defines a bool variable named finished.
What data type allows you to create a variable that holds one of two possible values: true or false?
boolean
Declare a variable hasPassedTest, and initialize it to TRUE.
boolean hasPassedTest = true;