Charles Abzug, Ph.D. Department of Computer Science James Madison University Harrisonburg, VA 22807 Voice Phone: 540-568-8746, E-mail: [email protected].
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
Charles Abzug, Ph.D.Department of Computer Science
The compiler converts ALL of the Source Code to Object Code in abatch-type operation. Once the Object Code as been produced, the Source Code is no longer needed for the program to run; it could bediscarded without affecting the ability of the program to run successfully.
The Source Code is examined by the Interpreter line by line, and is converted line by line to Object Code. The Object Code is run on the computer line by line as it is generated. It is not retained, but isdiscarded immediately after being run.
• Compilation can be very efficient: – Compilation can take a lot of time on the computer.– However, compilation need be carried out only once per program.– The object code can be run an unlimited number of times thereafter.
– Disadvantage: Compiled object code is specific to a particular hardware architecture; code compiled on a MacIntosh won’t run on a “Wintel” family machine, or on a Sun, or on a VAX or on an IBM RS/2000 or on an SGI. (Underlined terms refer to computers of grossly different architectural design.)
• Interpretation is usually very inefficient.– Every time the program is run, it is necessary to scan the Source Code
line by line and to convert it line by line to object code.– Huge expenditure of processing overload.
– Advantage: Interpreters for a particular programming language can be produced for machines of different architecture. Program source code can then be run on all machines equipped with interpreters that understand that language.
Java source code is first compiled and converted to Java “byte code”. This is a form of pseudo-object-code, which runs not on a real computer, but instead on a “Java Virtual Machine”, which is a standardized kind of theoretical computer.
Each computer that runs Java code contains a “Java Real-Time Run Environment”, which interprets the “byte code” of the idealized Java Virtual Machine on the hardware of the actual computer.
The advantage of this outwardly labyrinthine process is that Java “byte code” can be generated on one architectural family of machines, and can then run on a wide variety of machines of grossly different hardware architecture.
The Java “byte code” is much closer to actual object code than is the Source Code of a High-Level Language. Therefore, the interpretation of “byte code” is not soinefficient as is the interpretation of High-Level-Language source code.
A Basic Java Program (continued)9. Naming rules and conventions:
b) Conventions for Identifiers:
i. The name of a class only begins with an Uppercase character.
ii. Names of primitive data types, of variables, of objects, and of methods all begin with lowercase letters.
iii. Names should be anti-laconic and fully descriptive of function, e.g., “numberOfVarieties” rather than “numVar”.
iv. Multi-word identifiers are strongly recommended, because these are more fully descriptive of function.
v. Set off from each other the various words of a multi-word identifier, by capitalizing the first letter of each word that follows another word (i.e., of each word except the first).
vi. Spell names of Constants with ALL capital letters. To set off from each other the individual words in the Identifier of a constant, use the Underscore character.
c) Position your type and class declarations as closely as possible to the top of the program (i.e., do not defer the declaration of a variable or of a constant to immediately before you use it.
10. General format of a Java program:
public class DemonstrationOfASimpleJavaProgram { //Purpose: to demonstrate the simplest possible Java program. The
// program accomplishes absolutely nothing!
public static void main(String[] args){} //end method main
} //end class DemonstrationOfASimpleJavaProgram
/* Note position of opening and closing “curly” braces: paired braces are
a) Two kinds: // This comment ends with NewLine: following line is Source Code.
/* This comment continues indefinitely, until it is explicitly brought to a halt by means of the comment-termination character sequence. */
b) Extensive comments are required. No matter how good your code, you MUST take the trouble to explain it.
c) Identifiers with VERY specific and detailed names can do a lot to lighten the burden of commenting your program, but they do NOT entirely eliminate the necessity to explain your code.
d) Standard boilerplate is required at the beginning of the program:
i. Your full name and the full date (day, month, & 4-digit year).ii. Course number and assignment number, OR Project Name.iii. Brief explanation of the function of the program.iv. Date and brief summary/explanation of each revision.
/* Name: Charles AbzugDate: 08 Oct 2001Project:CS-139, illustrative programPurpose: To Demonstrate the very simplest possible Java program: bare- bones content; program accomplishes absolutely nothing!
A Not Quite So Simple Java Program/* Name: Charles Abzug
Date: 08 Oct 2001Project:CS-139, illustrative programPurpose: To Demonstrate the very simplest possible Java program: bare- bones content; program accomplishes absolutely nothing!
Revision01: 08 Oct 2001: Added a simple output operation.*/
5. Declarations of Variables and of Constants are to be bunched near the top of the region of the program over which they have scope, even though the language itself does NOT require this.
6. ALL of Savitch’s recommendations regarding programming style are required.
7. Further requirements regarding program structure and style to be added later.
a) how much space needs to be allocated to store the data for each variable of that type.
b) how the data are represented within the storage location, i.e., how specific values of the data are mapped to specific storage representations.
c) the range of values that can be accommodated.
d) what operations are defined for the Data Type.
For example, all numeric Data Types can undergo addition, subtraction, multiplication, and division. However, it makes no sense to multiply or divide elements of character data (char).
b) usually includes several methods that specify permissible behavior of variables declared to be of that type.
c) may be provided as part of the standard Java package, or may be programmed by the same person or group responsible for the current project, or may be part of the organization’s infrastructure or borrowed or purchased from an external source.
Declaration of Variables and Constants (continued)
Character Data1. Formal name for character data type: char
2. Character literals: delineated by “Single Quotes” (apostrophes).
3. Underlying Representation: 2-Byte binary (216 possible values: UNICODE)
a) First 256 items (i.e., 000016 through 00FF16) are identical in value to ASCII (American Standard Code for Information Interchange), an old 1-Byte code.
b) Both printing and non-printing characters are represented.
c) Many languages represented, including diacritical marks, accents, etc.
d) To determine Sorting Order and underlying representation of printing characters used in English, see Savitch’s Appendix 3.
e) Upper-case and lower-case are clearly differentiated. ‘a’ != ‘A’, etc.
f) For characters of the Latin alphabet, representation of the upper-case and lower-case versions of the same letter differ by 32.
Declaration of a Named Constant1. Three reserved words are usually included in the declaration:
a) public: denotes that the contents of the declared variable (that is, its value) can be accessed from any class that references the current class. NOT REQUIRED, but if the item is declared to be private, then it can be accessed only from within the current class.
b) static: denotes that the value of the constant can be accessed by reference to the name of the class in which it is defined, in contrast to anon-static identifier, whose value can be accessed only by reference to a specific instance of the class in which it is used. NOT REQUIRED.
c) final: denotes that the value, once assigned, can NOT be changed (i.e., that the identifier denotes a constant and not a variable). REQUIRED if the identifier denotes a constant and not a variable.
2. Following the three magic words, the Type is declared, followed by the Identifier.
3. Usually (but not always), the value is assigned to the constant in the declaration, e.g.: public static final float PI = 3.14159;
2. Poor Practice (and therefore disallowed in this course): differentiating identifiers from each other, or from Reserved Words, by “clever” use of case.
Output to the VDT1. System.out.print(Some_String_Variable_or_Constant); //ready to continue on
//same line.
2. System.out.println(Some_String_Variable_or_Constant); //repositions after //output to a new line.
NOTE: “System.out” is a class.
NOTE: “print” and “println” are methods of the class “System.out”.
NOTE: “Some_String_Variable_or_Constant” can be a String literal, or can be the concatenation of any number of String variables and String constants (including literal constants).
NOTE: “Some_String_Variable_or_Constant” is a parameter being passed to the methods “print” or “println”.
NOTE: Numeric variables can be specified to be concatenated with strings; this produces an automatic conversion of the numeric value to a string.
NOTE: A String literal (a “quoted string”) is treated identically to a String variable, or to a defined String constant, or to an expression that evaluates to a String.
NOTE the difference between the String methods equals and equalsIgnoreCase, on the one hand, and the logical comparison operator “==“ for String types, and the reason.
6. Lexicographic ordering (i.e., Unicode value ordering) vs. Alphabetic ordering:
string_1.compareTo(string_2)
a negative value, if string_1 precedes string_2. zero, if the two strings are identical.a positive value, if string_ 2 precedes string_1.
Condition: an Expression of Type boolean (continued)