Unformatted text preview:

Chapter 1 An Overview of the Java Language 1 Introduction The purpose of this chapter is to give you a brief introduction to the Java language It is not a tutorial on how to program and even less a guide to how to program well We ll spend a large part of this book discussing how programs should be written but the aim of this chapter is simply to introduce you to the most basic syntax and intuitive semantics of Java If you already know Java you can probably just skim the chapter We assume that you already have experience programming in a high level language such as C C or Pascal including such basic programming concepts as variables assignment simple input and output I O conditional execution loops and subroutines called methods in Java parlance If you don t know how to program this chapter won t help you We take a spiral approach to introducing Java concepts introducing just enough at first to get you going and adding details later This approach works well pedagogically but makes for a frustrating reference We recommend that you have an introductory Java text handy that contains a detailed reference section It is not necessary to read and master this chapter all at one time We have concentrated the Java language material in this one chapter rather than spreading it out through the book You should read the first sections of this chapter before going on to Chapter 2 but other sections can wait until you are ready to read later chapters Each section in this chapter is keyed to later chapters Similarly each chapter has a prerequisite section telling which sections of this chapter are required reading So let s begin 2 A first program The simplest Java program is a sequence of zero statements interesting programs are necessarily longer We ll begin by introducing the parts of the language necessary for the 2001 Donald F Stanat and Stephen F Weiss Chapter 1 Java Overview Page 2 following program 1 Look over the program Some of it may look familiar other parts look like magic incantations and in some ways they are Program output is shown on the next page Program 0 A very gentle introduction to Java Description This program greets the user requests two integers then displays the sum product and quotient of the integers import java io public class Program0 public static void main String args throws IOException BufferedReader stdin new BufferedReader new InputStreamReader System in int num1 int num2 String name Holds the two integers entered Holds name Greet the user System out println Hello System out print What is your name name stdin readLine System out println Hello name Request and read two integers System out print Please enter the first integer num1 Integer parseInt stdin readLine System out print Please enter the second integer num2 Integer parseInt stdin readLine Display sum product and quotient of two integers 1The programs of this chapter are meant to illustrate the characteristics of the Java language and although we will make an effort not to violate rules of good programming practice many of the programs will be neither exemplary nor interesting 1 14 2019 1 14 2019 Chapter 1 Java Overview Page 3 System out println nThe sum of num1 and num2 is num1 num2 System out println The product of num1 and num2 is num1 num2 System out println The quotient of num1 and num2 is double num1 num2 Wait before quitting System out println nPlease hit enter to quit name stdin readLine System out println nEnd of job End of main method End of Program0 class This program contains comments that are directed to the reader of the program declarations of variables expressions that calculate values assignment statements that store the values of expressions in variables and input and output statements 3 Program annotations Comments Comments are program annotations that are intended to aid a reader in understanding a program what it does how it works limitations etc Comments do not affect program execution in any way 1 14 2019 1 14 2019 Chapter 1 Java Overview Page 4 Java comments occur in two distinct forms A comment of any size and covering any number of lines can be inserted anywhere in a Java program by enclosing the comment text within and which serve as left and right brackets for the comment For example Read in the radius of a circle and the length of a side of a square and display the areas of the circle and the square Comments delimited by the and can in fact occur in the midst of a program statement thus sam george This is not usually a good idea bill will have exactly the same effect as sam george bill Note that comments that are begun with must be explicitly terminated with a The second comment form is commonly used for brief comments Two consecutive slashes signal the beginning of the comment the comment is terminated by the return character that terminates each line of the program For example in the following program segment the first line contains variable declarations followed by a comment while the next non blank line contains only a one line comment int radius side The lengths of the radius and the side Prompt for and read input 4 Java Identifiers The power of a programming language is largely a function of the support it provides for abstraction An abstraction is usually represented by a name Two rules are needed The first rule establishes what names are acceptable The second rule establishes the scope of a name that is where in a program a name will be recognized or make sense We will look at names first and scope a little later 4 1 Identifier Names Program entities are either named or anonymous Identifiers are names given by a programmer to program entities including variables constants methods classes etc There are two rules plus some guidelines for creating identifiers First a Java identifier must begin with a letter which can then be followed by an arbitrary number of letters digits and the underscore character Upper and lower case letters are different Second the identifiers must not duplicate Java s reserved words As we learn Java we will encounter most of these reserved words Your Java reference will have a complete list 1 14 2019 1 14 2019 Chapter 1 Java Overview Page 5 The following identifiers are all legal and all different Rate of pay rate of pay rateOfPay r2d2 Average average The following names are all illegal for the reasons indicated 3D Rate of pay Rate of pay Delta for Starts with something other than a letter Contains illegal characters blanks Contains illegal


View Full Document

UNC-Chapel Hill COMP 401 - An Overview of the Java Language

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Loading Unlocking...
Login

Join to view An Overview of the Java Language and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view An Overview of the Java Language and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?