Download presentation
Presentation is loading. Please wait.
Published byClaud Fox Modified over 9 years ago
1
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING
2
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
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 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.
5
GCOC – A.P. Computer Science A How Java Works!
6
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.
7
GCOC – A.P. Computer Science A public class BareBones { } // end class BareBones All Java programs start with a class.
8
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!
9
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.
10
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 ( ; ).
11
GCOC – A.P. Computer Science A Never put a ; before an open { brace ;{ //illegal }; //legal
12
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.
13
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.
14
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!
15
GCOC – A.P. Computer Science A Basic Output Commands print() println()
16
GCOC – A.P. Computer Science A System.out.print( " Hello" ); object / referencecommand / method OUTPUT Hello System is the class name.out is an object that prints characters on the console window (black window on the screen). println is a method, or behavior, that the System.out object carries out. The characters enclosed in double quotation marks and are called a string. The string is printed in the console window. Each statement in a program ends with a semicolon (;). Try this in the interactions pane of Dr. Java. You should get the output to the left.
17
GCOC – A.P. Computer Science A Type the following into java: public class TestOutputs { public static void main(String [] args) { System.out.print("Hello"); } OUTPUT HelloHello
18
GCOC – A.P. Computer Science A public class TestOutputs { public static void main(String [] args) { System.out.println("Hello"); System.out.print("Hello"); } OUTPUT Hello
19
GCOC – A.P. Computer Science A Try the statements below in your TestOutputs program. Be sure to run each one and not any differences. Pay close attention to the difference between print and println.
20
GCOC – A.P. Computer Science A Java provides a facility for displaying special characters in a string. These special characters are indicated by placing a backslash (\) in the string. For example, since double quotation marks are used to indicate the start and end of a string, there is no obvious way to include quotation marks within a string. Therefore, the backslash must be used as in the statement: System.out.println(“You say \”Goodbye,\” and I say \“Hello.\””); Try this in the TestOutputs program. It should display: You say "Goodbye," and I say "Hello.“ The double quotation marks that follow the backslashes are displayed rather than interpreted as ending the string. The combination of symbols is called an escape character. The idea is that it escapes from the normal interpretation of characters in a string.
21
GCOC – A.P. Computer Science A Another special escape character is the end-of-line character. This is indicated by \n and can replace the use of –ln in println. For example: System.out.print(“This is the first line,\n”); System.out.print(“and this is the second line.”); Try this in the TestOutputs program. Displays: This is the first line, and this is the second line.
22
GCOC – A.P. Computer Science A System.out.println( " \\hello\ " / " ); \\Prints out \ \“Prints out “ \’Prints outs ’ \nGoes to next line \tTab(moves over 8 spaces OUTPUT \hello"/
23
GCOC – A.P. Computer Science A What is the output? (Attempt to answer the questions first, then type each into the interactions pane in Dr. Java to confirm your answers). System.out.println( “h\tello”); System.out.println( “hel\\lo\””); System.out.println( “hel\nlo”);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.