Chapter 2 How to Compile and Execute a Simple Program.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

The Web Warrior Guide to Web Design Technologies
IT151: Introduction to Programming
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Object Oriented Programming in JAVA
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Getting Started with Java
Introduction to scripting
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Java Applications & Program Design
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
 Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Creating a Java Application and Applet
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CSC 110 – Intro to Computing - Programming
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Programming what is C++
Chapter 6 JavaScript: Introduction to Scripting
CSC201: Computer Programming
Chapter 3 GC 101 Java Fundamentals.
Completing the Problem-Solving Process
Console Output, Variables, Literals, and Introduction to Type
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 2 Introduction to Java Applications
Data types and variables
Fundamentals of Java Programs, Input/Output, Variables, and Arithmetic
Computer Programming-1 CSC 111
Instructor: Alexander Stoytchev
Presentation transcript:

Chapter 2 How to Compile and Execute a Simple Program

When you work with Java, you work either within a vendor-specific development environment or within a general-purpose editor, with which you write your program, and a compiler, with which you translate your program into a form with which your Java virtual machine can work. If you try to learn Java using a vendor-specific development environment, you learn a great deal about the vendor-specific development environment, but not enough about Java itself. Accordingly, this book introduces Java in the expectation that you will use a traditional editor and compiler.

In its original form, your program is text or source code. Once translated, the source code becomes byte code. You use the Java virtual machine to execute your program, or, said another way, you run your program.

You generally go around two key loops many times as you search for bugs. * * | Write or edit source code | <--* < * * * | | | | Fix bugs that | v | emerge during | * * | compilation | | Compile source code | ---* | * * | | Fix bugs that | v emerge during | * * execution | | Execute program | * * *

All Java programs contain one or more class definitions, each of which may contain various method definitions. In particular, every standalone Java program must contain a class definition that defines a method named main. When you start a Java program, the Java virtual machine performs the computations specified in the main method, thereby executing your program.

Applets developed exclusively for use in web browsers do not contain a main method, because they are not standalone programs.

Java's methods take the place of the functions or procedures that programmers talk about when they work with other languages. Methods are much like functions and procedures, except that each method definition must be embedded in a class definition.

Suppose, for example, that you want to compute the overall rating of a movie, given integers that specify individual ratings for the script, acting, and direction. For the moment, assume each individual rating contributes equally to the total rating. In Java, your program will contain a simple summing expression:

To arrange for your arithmetic expression to be evaluated, you define main, inside a class definition, such that the arithmetic expression appears in that method. public static void main (String argv[]) { ; }

Keywords are words to which Java attributes special meanings. Three such keywords appear in the example. *-- Keywords | v public static void main (String argv[]) { ; } –The keyword public indicates how accessible the main method is to be. –The keyword static indicates that the main method is a class method, rather than an instance method. –The keyword void indicates that the main method returns no value.

Following the method name, main, you see a parameter specification surrounded by parentheses. *-- Parameter specification | v public static void main (String argv[]) { ; }

Finally, you come to the method's body. In general, a method's body consists of matched braces surrounding a sequence of one or more statements, which tell the Java compiler what computations to perform. In the example, the body exhibits only one statement: public static void main (String argv[]) { ; <-- Body statement } The statement, like most Java statements, consists of an expression, , and the statement terminator, a semicolon, ;.

At this point, you are ready to embed the main method in a class definition. Because you are defining a demonstration program, you name the class Demonstrate. You must store the definition of the Demonstrate class in a source file. The source file's file name must be Demonstrate, the name of the class contained in the file, and the source file's extension must be java.

Most Java programmers, by convention, start each class name with an uppercase letter. You should adhere to this convention; otherwise, other programmers may have difficulty understanding your work.

The definition of the Demonstrate class begins with two keywords: public and class. *-- Keywords | v public class Demonstrate {... } –The keyword public indicates how accessible the Demonstrate class is to be. –The keyword class indicates that a class is about to be defined.

Following the name of the class, Demonstrate, you come to the body of the class definition. In the example, the body contains a single method definition—the one for the main method—which computes the rating of a movie, given ratings for the movie's script, acting, and direction. public class Demonstrate { public static void main (String argv[]) { ; } }

The semicolon, ;, the parentheses, (), and the braces, {}, act as punctuation. Occasionally, such markers, in such contexts, are called punctuators.

Note that the sample program is catatonic: It accepts no input data and produces no output result. And, because the program does nothing with the arithmetic it performs, discriminating Java compilers refuse to compile it.

To relieve programs of their catatonia, you can include display statements that tell the Java compiler that you want information to be displayed, as in the following revised program, which, when executed, displays The rating of the movie is 23 : public class Demonstrate { public static void main (String argv[]) { System.out.print("The rating of the movie is "); System.out.println( ); } }

The display methods, print and println, display information delimited by parentheses. Whenever you use println instead of print, Java not only displays information, but also terminates the line on which the information is displayed.

In Java, whenever quotation marks delimit a sequence of characters, those characters are the contents of a string. In the sample program, there are two display statements, the first of which displays a string. *-- String | v System.out.print("The rating of the movie is ");

In the second instance, the display method displays the result produced by an arithmetic expression: *-- Arithmetic expression | v System.out.println( );

The System.out part of the display statement stipulates that the information is to be shown on your computer's display: *-- Stipulates where information is to be sent | v System.out.print("The rating of the movie is ");

Said with more precision, print and println are display methods that are defined to work on instances of the PrintStream class. System.out is an expression that produces the particular instance of the PrintStream class associated with your display.

A value that appears explicitly in a program is said to be a literal. –Explicit numbers, such as 6, are integer literals. –Explicit strings, such as "The rating of the movie is ", are string literals.

Spaces, tabs, line feeds, and carriage returns are said to be whitespace characters. Java is blank insensitive: It treats all sequences of whitespace characters—other than those in strings—as though there were just a single space.

Thus, the following are equivalent: public class Demonstrate { public static void main (String argv[]) { System.out.print("The rating of the movie is "); System.out.println( ); } } public class Demonstrate { public static void main (String argv[]) { System.out.print("The rating of the movie is "); System.out.println( ); } }

Java is case sensitive; if you write Main or MAIN when you mean main, Java cannot understand your intent.

At this point, you have seen sample uses of just one Java operator: the addition operator. In general, an operator is a built-in method that works on inputs supplied to it according to the conventions of arithmetic; such methods are interspersed among their inputs, and those inputs are called operands.

To initiate compilation on a Windows system, you open a window. Next, you type the following, assuming that your class definition is in a source file named Demonstrate.java : javac Demonstrate.java Such a line is called a command line. The example command line has two parts: *-- Name of the Java compiler | | *-- Source file's name and extension | v v javac Demonstrate.java The Java compiler places the resulting byte code in a file named Demonstrate.class.

Once the Java compiler has placed the resulting byte code in Demonstrate.class, you can execute the main program defined inside the class definition by typing another command line: *-- Name of the Java virtual machine | | *-- Object file's name | v v java Demonstrate

Although the sample program communicates with you by way of output data displayed on your screen, it does not receive any input data after it has been compiled. Instead, it works with data that were supplied as the program was written. Such data are said to be wired in or hard coded.