Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING."— Presentation transcript:

1

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

3 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

4 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

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

6 GCOC – A.P. Computer Science A How Java Works!

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

8 GCOC – A.P. Computer Science A public class BareBones { } // end class BareBones All Java programs start with a class.

9 GCOC – A.P. Computer Science A 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!

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

11 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 ( ; ).

12 GCOC – A.P. Computer Science A Never put a ; before an open { brace ;{ //illegal }; //legal

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

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

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

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

17 GCOC – A.P. Computer Science A fred Monster Monster fred = new Monster(); 0xF5 Monster fred creates a reference. new Monster(); creates the Monster at location 0xF5. The assignment operator (=) gives fred the address 0xF5 to store

18 GCOC – A.P. Computer Science A What is a variable?

19 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

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

21 GCOC – A.P. Computer Science A How do you name variables when defining them?

22 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

23 GCOC – A.P. Computer Science A Which of these would be legal variable identifiers? int 1stYear; double jump Up; double feet2Inches; IDENTIFIER is a fancy word for variable.

24 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

25 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 voidmain 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


Download ppt "JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING."

Similar presentations


Ads by Google