#### Exam Objectives Covered

• Declare and initialize variables including a variable using final

• Cast a value from one data type to another including automatic and manual promotion

#### Data Types

• All data in Java has a type

• Class or interface type (covered later) - String is a class

• One of the primitive types (no class for these)
• byte
• short
• int
• long
• float
• double
• char
• boolean

#### Literals

• Integer literals can be expressed as:
• Decimal/base 10 [default] 42
• Octal/base 8 [prefix 0] 052
• Hexadecimal/base 16 [prefix 0x (preferred) or 0X] 0x2A

• Floating point literals can be expressed as:
• Decimal 169.5
• Scientific/exponential notation [E (preferred) or e] 1.695E2

• Boolean literals are true or false

• Character literals are expressed using single quotes 'a'
• NOTE: single character only
• For a string of characters, use the String class (more later)

#### Integer Primitives

• Integer primitives
• byte
• short
• int
• long

• All are signed (positive or negative)

• Integer literals are int by default (and recommended)
``````byte smallNumber = 123;
short mediumNumber = 12345;
int averageNumber = 123456789;
long bigNumber = 123456789123L;  // 123456789123 is too big for int
int octNumber = 025;   // 21 in decimal (5 1's + 2 8's)
int hexNumber = 0x25;  // 37 in decimal (5 1's + 2 16's)``````

#### Integer Primitives - Range of Values • Because integers are signed, the leftmost bit is used for storing the sign of the number
• 0 for positive numbers and zero
• 1 for negative numbers #### Storing Negative Integers

• Java uses two's complement to store negative integers
• Invert the bits and add one to get the negative value

• More later--like conversions and bit shifting

#### Floating Point Primitives

• Floating point primitives
• float
• double

• IEEE 754 standard for 32-bit and 64-bit floating point numbers

• Floating point literals are double by default (recommended)
``````float oneFloat = 1000.23f;    // to indicate float, not double
float twoFloat = 1.22234F;    // F and f both work
double oneDouble = 333.33;
double twoDouble = 444.44d;   // d and D both work (not needed here)
double scientific = 4.8E12;   // 4.8x1012 (E and e both work)``````

#### char Primitive

• Use single quotes around a single literal character

• A char is stored internally as a 16-bit unsigned integer
• Its value is the Unicode value of the character (see notes)
• Unicode is a superset of ASCII (Why not just use ASCII?)
• Supports Latin and non-Latin alphabets
• QUIZ: how many characters can Unicode represent?
• HINT: ASCII is 7-bit and can represent 128 characters

• The Unicode value of a is 97; A is 65

• Since a char is really an integer, you can convert back and forth to other integer primitives (rules later)
``````char myChar = 'a';
char tab = '\t';  // backslash (\) is the escape character in Java``````

#### boolean Primitive

• Only values are true and false
• No C, C++ tricks with 0 and 1

• A boolean is stored internally as 0 or 1, but you can't see this

• Naming convention – Name booleans in the form of a question
• The value answers the question
``````boolean bool1 = true;
boolean bool2 = false;
boolean wrong = 0;          // ERROR: must be true or false
boolean noQuotes = "true";  // ERROR: "true" is a String!``````
``boolean isRaining = true;  // they must live in Seattle``

#### Why Have Primitive Types in Java?

• Why? Avoid overhead associated with objects and references

• We've now seen all 8 types

• Memorize them all
• TIP: "Bears Shouldn't Ingest Large Fluffy Dogs" #### Primitives - Review Chart #### Variables

• Location in memory that can hold values of a certain type

• Must be declared before use
• Declaration allocates memory to hold a value

• Declaration takes this form:
``variable-type variable-name;``
``int age;``

#### Assignment

• Assignment takes this form:
``variable-name = value;``
• Declaration and assignment together:
``variable-type variable-name = value;``
• Assignment is done from right to left
• Value is determined first, then assigned to the variable
• Value can be a literal or an expression
• Value can be returned from a method
``````int sum;         // declaration
sum = 3 + 7;     // assignment``````
``````// declaration and assignment together
int age = p.getAge();  // p is a Person object``````

#### Primitive Variables and Reference Variables

• Variable is a "holder" or "bucket" for piece of data or object

• Data held one of two ways
• Primitive variable – stores data value directly
• Reference variable – stores reference to an object (more later)
``````int i = 13;  // i is a primitive variable that holds the value 13

Person p = new Person();  // p is a reference or handle to a Person``````

#### Lab 4.1 – Printing Primitives

• Create a class called "Primitives"

• Within the main method, declare 8 variables and assign them each to a different data type
• The values and names are up to you

• Declare 2 additional int type variables and assign them octal and hexadecimal values

• Print out the value of all 10 variables to the console
• Check to verify that the values are printed correctly

#### The final Modifier

• final is a modifier that can be applied to variable declarations
``final type variable-name = value;``
• Variables marked final can never be reassigned
• This makes them constants
• Can be used to do math if the constant is not reassigned

• For now, think that variables marked final must be assigned at declaration
``````final int answerToLife = 42;   // declaration and assignment
answerToLife = 43;     	       // does not work!``````
``````final int i = 2;   // declaration and assignment of final variable
int fish = 2 + i;  // legal because "i" is not reassigned``````

#### Lab 4.2 – Working with Constants

• Create a class called "Constants"

• Within the main method, declare three constants
• A final double called "pi" assigned the first three digits of pi
• A final double called "euler" assigned the first three digits of Euler's number
• A final int called "myFavoriteNumber" assigned your favorite whole number

• Try to reassign the value of "pi" to "myFavoriteNumber"
• Compile and see what happens!

#### Word Variables

• The String class is used to declare variables that can be assigned word values
• Called "strings" because they are a string of characters

• Strings are not primitives, but they can be declared and assigned like primitives
• The value of a String is delimited by double quotes
``String companyName = "nTier";``
• There is much more to learn about the String class, but this will be covered in a later section

#### Lab 4.3 – Printing Strings

• Create a class called "IntroToStrings"