java basics: variables and references syntax, errors, and debugging

24
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING

Upload: trevor-pierce

Post on 01-Jan-2016

269 views

Category:

Documents


0 download

TRANSCRIPT

JAVA BASICS: Variables and References

SYNTAX, ERRORS, AND DEBUGGING

GCOC – A.P. Computer Science A

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

GCOC – A.P. Computer Science A

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

GCOC – A.P. Computer Science A

How Java Works!

1. You create a source document using an established protocol (in our case, the Java language).

2. 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.

3. 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.

4. 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.

GCOC – A.P. Computer Science A

How Java Works!

GCOC – A.P. Computer Science A

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.

GCOC – A.P. Computer Science A

public class BareBones{

} // end class BareBones

All Java programs start with a class.

GCOC – A.P. Computer Science A

public class CompSci{ public static void main( String args [] ) { System.out.println(“Java Rules!"); }}

OUTPUTJava 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!

GCOC – A.P. Computer Science A

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.

GCOC – A.P. Computer Science A

public class CompSci{ public static void main(String args[]) { System.out.println(“Java Rules!"); }}

Every program statement is terminated with a semi-colon ( ; ).

GCOC – A.P. Computer Science A

Never put a ; before an open { brace

;{ //illegal}; //legal

GCOC – A.P. Computer Science A

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.

GCOC – A.P. Computer Science A

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.

GCOC – A.P. Computer Science A

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!

GCOC – A.P. Computer Science A

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.

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.

GCOC – A.P. Computer Science A

fred

Monster

Monster fred = new Monster();

0xF5

0xF5

Monster fred creates a reference. new Monster(); creates the Monster at location 0xF5.The assignment operator (=) gives fred the

address 0xF5 to store

GCOC – A.P. Computer Science A

What is a variable?

GCOC – A.P. Computer Science A

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?

days

102

yesOrNo

int days = 102; double taxRate = 7.75; boolean yesOrNo;

taxRate

7.75

GCOC – A.P. Computer Science A

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.

GCOC – A.P. Computer Science A

How do you name variables when defining them?

GCOC – A.P. Computer Science A

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.

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

GCOC – A.P. Computer Science A

Which of these would be legal variable identifiers?

int 1stYear; double jump Up; double feet2Inches;

IDENTIFIER is a fancyword for variable.

GCOC – A.P. Computer Science A

Java is case sensitive – it interprets upper and lower

case letters as different letters.

Brandon does not equal brandon.

Brandon != brandon

GCOC – A.P. Computer Science A

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 publicstatic long break continue class

You can find the complete list of keywords here:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html