Top Banner
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING
24

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.

Dec 17, 2015

Download

Documents

Barbra Matthews
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

JAVA BASICS

SYNTAX, ERRORS, AND DEBUGGING

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

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.

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

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

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

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.

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

How Java Works!

Page 6: 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.

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.

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

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!

Page 8: 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.

public class BareBones{

} // end class BareBones

All Java programs start with a class.

Page 9: 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.

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

**Type this program in Dr. Java and run it.

OUTPUTJava Rules!

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

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.

Page 11: 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.

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

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

Page 12: 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.

Never put a ; before an open { brace

;{ //illegal}; //legal

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

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.

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

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.

Page 15: 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.

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!”);}

}

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

Basic Output Commands

print()println()

Page 17: 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.

System.out.print("Hello" );

object / reference

command / method

OUTPUTHello

Page 18: 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.

System.out.print("Hello");System.out.print("Hello");

OUTPUTHelloHello

Page 19: 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.

System.out.println("Hello");

OUTPUTHello

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

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

OUTPUTHelloHello

Page 21: 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.

System.out.println("hel\tlo\n");

\n newline\t tab\r carriage return

OUTPUThel lo

Page 22: 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.

System.out.println("\\hello\"/");

\\ Prints out \\“ Prints out “\’ Prints outs ’

OUTPUT\hello"/

Page 23: 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.

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

Page 24: 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.