Blakes 21 Days Chapter 02 Document


Day 2, The ABCs of Programming

Statements and Expressions

Variables and Data Types

Creating Variables

Naming Variables

Variables Types

Data Types

  Type Size Values That Can Be Stored
  byte   8 bits -128 to 127
  short 16 bits -32,768 to 32, 767
  int 32 bits -2,147,483,648 to 2,147,483,647
  long 64 bits -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  Type Size Values That Can Be Stored
  float   1.4E-45 to 3.4E+38
  double   4.9E-324 to 1.7E+308

Class Types

Assigning Values to Variables

Constants

Chapter 2 First Project

Screen Shot of the Output Pane

144

Screen shots of the project


Comments

Single-line Comments

Multi-line Comments

Javadoc Comments

Literals

Number Literals

Boolean Literals

Character Literals

Escape Meaning
\n New line
\t Tab
\b Backspace
\r Carrage return
\f Formfeed
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\d Octal
\xd Hexadecimal
\ud Unicode character

String Literals

Expressions and Operators

Arithmetic

TABLE 2.3 Arithmetic Operators
Operator Meaning Example
+ Addition 3 + 4
- Subtraction 5 - 7
* Multiplication 5 * 5
/ Division 14 / 7
% Modulus 20 % 7

The next project

Listing 2.2

Listing 2.2 (from NetBeans) displayed below

150

Figure 2.2 goes here

More About Assignment

TABLE 2.4 Assignment Operators
Expression Meaning
x += y x = x + y
x -= y x = x - y
x *= y x = x * y
x /+ y x = x / y

Incrementing and Decrementing

Comparisons

TABLE 2.5 Comparison Operators
Operator Meaning Example
== Equal to x == 3
!= Not Equal to x != 3
< Less than x < 3
> Greater than x > 3
<= Less than or equal to x <= 3
>= Greater than or equal to x >= 3

Logical Operators

Operator Precedence

TABLE 2.6 Operator Precedence
Operator Notes
. [] () A period . is used for access to methods and variables within objects and classes. Square brackets [ ] are used for arrays. Parentheses ( ) are used to group expressions
++ -- ! - instanceof The instanceof operator returns true or false based on whether the object is an instance of the named class or any of that class's subclasses.
new (type) expression The new operator is used to create new instances of classes. The parentheses in this case are for casting a value to another type.
* / % Multiplication, division, modulus
+ - Addition, subtraction
<< >> >>> Bitwise left and right shift
< > <= >= Relational comparison tests
== != Equality
& AND
^ XOR
| OR
&& Logical AND
|| Logical OR
? : Ternary operator
= += -= *= /= %= ^= Various assignments
&= |= <<= >>= >>>= More assignments

String Arithmetic

TABLE 2.7 Operator Summary
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
< Less than
> Greater then
<= Less than or equal
>= Greater then or equal
== Equal to
!= Not equal to
&& Logical AND
|| Logical OR
! Logical NOT
& AND
| OR
^ XOR
= Assignment
++ Increment
-- Decrement
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Modulus and assign


Summary

Q & A

Quiz

  1. Which of the following is a valid value for a boolean variable?
    1. "false"
    2. false
    3. 10
  2. Which of these is NOT a convention for naming variables in Java"
    1. After the first word in the variable name, each successive word begins with a capital letter.
    2. The first letter of the variable name is lowercase.
    3. All letters are capitalized
  3. Which of these data types holds numbers from -32,768 to 32, 767
    1. char
    2. byte
    3. short

Answers

  1. B. In Java, a boolean can only be true or false. If you put quotation marks around the value, it is treated like a String rather than one of the two boolean values.
  2. C. Constant names are capitalized to make them stand out from other variables.
  3. C. The short primitive data type has that range of values.

Certification Practice

  1. Which of the following data types can hold the number 3,000,000,000 (3 billion)?
    1. short, int, long, float
    2. int, long, float
    3. long, float
    4. byte

Exercise



End of Day 2



Screenshots from running the first project in Chapter 2

The "New Empty Java File" dialog box

145

The code from the book

146

Getting ready to run the file

147

The Output Pane

148

Reserve