Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine.

Similar presentations


Presentation on theme: "JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine."— Presentation transcript:

1 JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING

2 OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine and byte code. Describe the structure of a simple Java program. Write a simple program. Edit, compile, and run a program using a Java development environment. Format a program to give a pleasing, consistent appearance. Understand and fix compile-time errors.

3 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 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 How Java Works!

6 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 Assignment – Paper Airplanes Write down a set of instructions on how to fold a piece of paper into a paper airplane. Write these instructions in the Discussions under the heading Paper Airplanes. You are to choose a classmates’ instructions, and follow the directions exactly as they are written! Respond to that classmate as to the success of your airplane based on his/her instructions. Revise your own instructions by posting a “corrected” set of instructions, based on the comments made about your original instructions. OK – Now on to some real Java programming!

8 public class BareBones { } // end class BareBones All Java programs start with a class.

9 public class CompSci { public static void main( String args [] ) { System.out.println(“Java Rules!"); } **Type this program in Dr. Java and run it. OUTPUT Java Rules!

10 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 public class CompSci { public static void main(String args[]) { System.out.println(“Java Rules!"); } Every program statement is terminated with a semi-colon ( ; ).

12 Never put a ; before an open { brace ;{ //illegal }; //legal

13 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 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 Type the following program in Dr. Java exactly as it is written below: // basic class example public class Hello World { public static void main(String args [ ]) { System.out.println(“Hello World!”); }

16 Basic Output Commands print() println()

17 System.out.print( " Hello" ); object / referencecommand / method OUTPUT Hello

18 System.out.print( " Hello"); OUTPUT HelloHello

19 System.out.println( " Hello"); OUTPUT Hello

20 System.out.println( " Hello"); OUTPUTHello

21 System.out.println( " hel\tlo\n " ); \nnewline \ttab \rcarriage return OUTPUT hel lo

22 System.out.println( " \\hello\ " / " ); \\Prints out \ \“Prints out “ \’Prints outs ’ OUTPUT \hello"/

23 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”);

24


Download ppt "JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine."

Similar presentations


Ads by Google