Variable Declarations

Download Report

Transcript Variable Declarations

Java
• Words, Symbols and Rules
Java Program Structure
• JAVA program:
 A program is made up of one or more classes.
The inside name of the class should always match the
outside name of the class.
 A class contains one or more methods
You need to have a main method. Java looks for it in
order to begin execution.
 A method contains program statements
Such as: System.out.println(“Some String”);
Program Statements
• Almost every statement ends in a semicolon. There are very few exceptions.
• Be careful with statement indentation
 You will increase indentation after an opening
bracket “{“
 You will decrease indentation after a closing
bracket “}”
Elements of Programming
• We need to reserve memory in order to store
the different values that will be used by our
program to perform calculations.
Variable Declaration and
Initialization
• Declaring a variable
int x;
• Initializing a variable
x = 5;
• Declaring and initializing
int x = 5;
5
Variable declaration and
comments
• One variable declaration per line please
• After the declaration/initialization you
should describe what the variable does.
int age, yearsOfMarriage, numberOfChildren;
int age = 25; //the persons age
In Memory
int x = 5;
5
X
Overwrite
int x = 5;
x = 3;
5
3
X
Identifiers
• Identifiers/Variable names are the words a programmer uses in
a program
• An identifier can be made up of letters, digits, the underscore
character ( _ ), and the dollar sign
• Identifiers cannot begin with a digit
• Java is case sensitive
9
Primitive Data types
•int
•double
•char
•boolean
Reserved Words
• A reserved word cannot be used in any other way
• The Java reserved words:
abstract
assert
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum
extends
false
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
true
try
void
volatile
while
11