Transcript JAVA BASICS

JAVA BASICS: Variables and
References
SYNTAX, ERRORS, AND DEBUGGING
Vocabulary & Terms
• Computer programming
• Machine language
• Machine code
• Compiler
• Source code
• Integrated Development
•
Environment (IDE)
Statement
• Bytecode
• Java virtual machine (JVM)
• Class
• Object
• Reference
• Command
• Method
College Board
Computer Science A Topics Covered
• Program Design - Read and understand a
•
•
•
problem's description, purpose, and goals.
Program Implementation - Console output
(System.out.print/println)
Program Analysis - Identify and correct errors.
Computing Context - Major hardware
components; Language translators/compilers ;
Virtual machines
1.
2.
3.
4.
How Java Works!
You create a source document using an established
protocol (in our case, the Java language).
Then your program is run through a source code
compiler. The source code compiler checks for errors
and won’t let you compile until it’s satisfied that
everything will run correctly.
The compiler creates a new document,coded into Java
bytecode. Any device capable of running Java will be
able to interpret/translate this file into something it
can run. The compiled bytecode is platform
independent.
The Java Virtual Machine (JVM) translates the
bytecode into something the underlying platform
understands, and runs your program.
The next slide shows an illustration of this sequence.
How Java Works!
What is Computer Programming?
Before we jump into Java, let’s talk a bit about
exactly what computer programming is.
• Computer programming is the art of creating a
set of instructions, called a computer program,
for a computer.
• Computers have no judgment, so instructions
must be detailed and unambiguous!
Unfortunately, creating complex sets of
unambiguous instructions is not easy. It requires
both training and practice.
public class BareBones
{
}
// end class BareBones
All Java programs start with a class.
public class CompSci
{
public static void main( String args [] )
{
System.out.println(“Java Rules!");
}
OUTPUT
}
Java Rules!
Type this program in Dr. Java and run it. Then change String args [] to
String [] args and run the program again. You should notice no change!
public class CompSci
{
public static void main( String args [] )
{
System.out.println(“Java Rules!");
}
}
Every method and every class must have an
opening ( { ) brace and a closing ( } ) brace.
public class CompSci
{
public static void main(String args[])
{
System.out.println(“Java Rules!");
}
}
Every program statement is terminated with
a semi-colon ( ; ).
Never put a ;
before an open { brace
;{ //illegal
}; //legal
public class CompSci
{
public static void main(String args[])
{
System.out.println(“Java Rules!");
}
}
Indent all code 3 spaces to make it easier to read.
What is a Convention?
• In Java, a convention is an accepted practice, not
necessarily a rule.
• Indenting 3 spaces is a Java convention that you will see
in many textbooks. In reality, the compiler doesn’t really
care about spacing!
• Spacing is for the human readers, not the computer.
Conventions For Use In This Class
• You should download and print out a copy of the
document Conventions, which is the next link.
• You must follow the conventions listed on this document
in every program that you turn in for a grade!
• You will lose points on your programs if you do not follow
these conventions!
A reference variable refers to a location in
memory that stores an object.
Monster fred = new Monster();
Monster sally = new Monster();
In the above example Monster is a class.
fred and sally are references to Monster objects.
new is a keyword which is used to create an object. It is followed by
the constructor method, which is the class name, and parenthesis.
Monster fred = new Monster();
Monster sally = new Monster();
The first part: Monster fred – declares a reference
variable to hold a Monster object.
The second part: = new Monster(); - creates the
Monster object, and assigns fred to the location
where this object is in memory.
Monster fred = new Monster();
fred
0xF5
0xF5
Monster
Monster fred creates a reference.
new Monster(); creates the Monster at location
0xF5.
The assignment operator (=) gives fred the
address 0xF5 to store
What is a variable?
A variable is a storage location for some type of
value. Type the statements below into the
interactions pane. Type the name of the variable to
see what is stored in the variable. What is stored in
yesOrNo?
int days = 102;
double taxRate = 7.75;
boolean yesOrNo;
days
taxRate
102
7.75
yesOrNo
int days = 102;
days
102
int days – declares days as a variable that
will store integer values.
= 102 assigns days to store the integer
value 102.
How do you name
variables when
defining them?
When naming a variable follow these rules and conventions:
• Make the variable name meaningful. That means that L is not a
meaningful variable name but length is meaningful. When reading
your program, I shouldn’t be guessing what the variable is meant to
hold.
• Start all variable names with a lower-case letter.
• Variable names may also contain letters, numbers and underscores.
• If a variable name is more than one word long, capitalize each of the
other words without adding any spaces.
VARIABLE NAMES
Below are some examples of valid variable names:
number
sum32
testAverage
area_Of_Trapezoid
and below are some examples of invalid variable names:
2ndValue test Average question#2
Which of these would be legal
variable identifiers?
int 1stYear;
double jump Up;
double feet2Inches;
IDENTIFIER is a fancy
word for variable.
Java is case sensitive – it interprets
upper and lower case letters as
different letters.
Brandon does not equal brandon.
Brandon != brandon
Keywords are reserved words that the
language uses for a specific purpose.
You cannot use keywords as identifier names.
Some that you’ve seen are:
int double return void main public
static long break continue class
You can find the complete list of keywords here:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html