Java - Tutorials

Setting Up The Java Environment
Java Environment
Overveiw Of Java Programming
Java Data Types
Java Operators
Java Flow Control

Exam Objectives Covered


Data Types


Literals


Integer Primitives

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


Storing Negative Integers


Floating Point Primitives

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

char myChar = 'a';
char tab = '\t';  // backslash (\) is the escape character in Java

boolean Primitive

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?


Primitives - Review Chart


Variables

variable-type variable-name;
int age;

Assignment

variable-name = value;
variable-type variable-name = value;
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

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


The final Modifier

final type variable-name = value;
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


Word Variables

String companyName = "nTier";

Lab 4.3 - Printing Strings