Variables, Statements, Expressions, Casting, Arrays


 

Variables

named memory location for storing a piece of information

supervisorFirstName
employeeFirstName

Literal

exactly what it is - ie: "joe"

a literal value is what is stored in a variable

supervisorFirstName = "joe"

Naming Conventions

standard prefixes - not used so much anymore, try to name the variable in such a way that someone reading it knows what it is

first letter is lower-case, the rest are proper cased

Primitive Variable Types

Non-Decimal Numbers byte
int
short
long
Decimal Numbers float
double
Other char
boolean

String variables are not considered primitive, because they actually use a Java Class file to manage them at all levels.

Declaring Variables

All declarations should be the first lines after the class or method signature, or the control structure that uses them (more on that later).

The format for declaring variables is:

DataType variableName = initialValue;

String firstName = "";
double semesterGrade = 0;

Java does not always initialize variables when they are declare.  The program may not compile if you do not initialize a variable declared inside a method.  It is considered good form to always initialize your variables when you declare them.

An argument to a method is a declaration that is not initialized.

Scope

used only for methods and class-level variables:

private - only the class in which it is declared can access the variable directly
protected - subclasses can access the variable directly
public - any class can access the variable directly

Variables declared inside an method can only be seen by that method.

Examples of class-level variables:

private String sFirstName = "";
private String sLastName = "";

Class level variables are usually declared as private, to prevent miss-use by an outside class.  They can be manipulated by almost any method inside the class.  In order to manipulate them from the "main" method, they must be declared "static":

private static String sFirstName = "";
private static String sLastName = "";

What does static mean?

Most of the time, you will instantiate a class as a variable before using it. There are times when you can't or don't want to:

public static void main(String args[])
constants like PI that never change

If it is declared static, you can call the method or variable without instantiating the class

Array Declarations:

declared using square brackets

int grades[] = new int[6];
String args[][] = new String[5][2];

Assignment Statement: assignment of a value into a variable (always use equal sign)

Statement: a line of code that does something

Expression: anything that evaluates to a value

int i = 2 + 5;
this is an assignment statement that is declaring a variable and initializing it using an expression

Binary Operators:

+ Addition 
- Subtraction 
/ Division 
* Multiplication 
% Modulus 

Unary Operators:

+=  
-=  
/=  
*=  
%=  
++ increment by 1
-- decrement by 1

Boolean Operators:

|,|| OR, at least one must be true
&, && AND, all must be true
! NOT, reverses the truth

Comparison Operators:

< Less than
<= Less Than or Equal To
> Greater Than
 >= Greater Than or Equal To
== Equal To
!= Not Equal to

Casting

changing a value from one primitive data type to another

implicit cast

cast is done without specifically telling the program to do it

int x = 5;
long y = x + 10;

x is implicitly cast from int to long

System.out.println(y);

y is implicitly cast from long to String

explicit cast

cast is done by specifically telling the program to do it

long y = (long)x + 10;

Parsing

changing a String to a number

String zipCodeAsString = "12345";
long zipCodeAsNbr = Long.parseLong(sZipCode);
int iZipCode = Integer.parseInt(sZipCode);

Parsing may cause what's called a "NumberFormatException."  More on that later.

Concatenating

use the "+" to combine Strings together into longer Strings

implicitly casts numbers to String

If you wanted to concatenate Strings and numbers to write out an equation

int iFirstNumber = 2;
int iLastNumber = 5;
int iSum = iFirstNumber + iLastNumber;

In VB iFirstNumber & " + " & iLastNumber & " = " & iSum
In Java iFirstNumber + " + " + iLastNumber + " = " + iSum;

 replace the & with + 

The resulting output would be: 2 + 5 = 7