260 likes | 465 Views
Lecture 3 Java Basics. Richard Gesick. Java Basics. Java Application Structure Data Types, Variables, and Constants Expressions and Arithmetic Operators. Typical Program Structure. Input some data Perform some processing on the data Output the results In each execution of the program
E N D
Lecture 3Java Basics Richard Gesick
Java Basics • Java Application Structure • Data Types, Variables, and Constants • Expressions and Arithmetic Operators
Typical Program Structure • Input some data • Perform some processing on the data • Output the results • In each execution of the program • The data may be different • The instructions will be the same
Java Application Structure • All programs consist of at least one class. • Java source code file must have same name as class with a .java extension.
A Skeleton Application 1 /* An application shell 2 Anderson, Franceschi 3 */ 4 public class ShellApplication 5 { 6 public static void main( String [ ] args ) 7 { 8 // write your code here 9 } 10 } See Example 2.1SkeletonApplication.java
Identifiers - symbolic names • Identifiers are used to name classes, variables, and methods • Identifier Rules: • Must start with a "Java letter" • A - Z, a - z, _, $, and Unicode letters • Can contain essentially any number of Java letters and digits, but no spaces • Case sensitive!! • Number1 and number1 are different • Cannot be keywords or reserved words • See Appendix A
Examples Are these identifiers valid? taxRate Yes. char No. char is a keyword intNumber Yes, The keyword int is embedded, but the identifier is not identical to a keyword. 2008Taxrate No. The identifier starts with a digit
Program Building Blocks The Statement • Performs some action • Terminates with a semicolon (;) • Can span multiple lines Example: System.out.println( "Programming is " + "not a spectator sport" );
Building Blocks - The Block The Block • 0, 1, or more statements • Begins and ends with curly braces { } • Can be used anywhere a statement is allowed. Example: public static void main( String [] args ) { System.out.println( "Hello" ); }
Building Blocks - White Space • Space, tab, newline are white space characters • At least one white space character is required between a keyword and identifier • Any number of white space characters are permitted between identifiers, keywords, operators, and literals Examples: int a 1 + 2 public static void main( String [] args )
SOFTWARE ENGINEERING TIP To increase readability of your code, surround operators and operands with white space and skip lines between logical sections of the program
Building Blocks - Comments • Comments explain the program to yourself and others • Block comments • Can span several lines • Begin with /* • End with */ • Compiler ignores all text between /* and */ Example: /* This program will print a message Anderson, Franceschi */
SOFTWARE ENGINEERING TIP Include a block comment at the beginning of each source file. It should • identify the author of the program • briefly describe the function of the program
Building Blocks - Comments Line comments • Start with // • Compiler ignores all text from // to the end of the line Example System.out.println( "Hello" ); // output Hello
A Sample Program • This program calculates the area of a circle See Example 2.2 AreaOfCircle.java • Lines 1 – 3are a block comment identifying the function and authors of the program 1 /* Calculate the area of a circle 2 Anderson, Franceschi 3 */
The Outer Structure • Lines 5-8 and 24-25 are the same as the skeleton application, except that the name of the class is AreaOfCircle, so we save this file as AreaOfCircle.java • Lines 9 – 23 do the work of the program 5 public class AreaOfCircle 6 { 7 public static void main( String [] args ) 8 { … 24 } 25 }
Identify the Data • First, we identify the data we know: • final double PI = 3.14159; • We identify other data we will need: • double radius; • double area; • We give radius a value 17 radius = 3.5; • To calculate the area of a different circle, we change this value, recompile and re-run the program.
Perform the Calculation • The area of a circle is calculated using the formula: πr2 • We translate this formula into Java: 20 area = PI * radius * radius; • And output the result: 23 System.out.println( "The area is " + area );
Data Types, Variables, and Constants • Declaring Variables • Primitive Data Types • Initial Values and Literals • String Literals and Escape Sequences • Constants
Data Types • For all data, assign a name (identifier) and a data type • Data type tells the compiler: • How much memory to allocate • How to store the data • The types of operations you will perform on the data • Compiler monitors use of data • Java is a strongly typed language • Java has eight primitive data types byte, short, int, long, float, double, char, boolean
Declaring Variables • Variables hold one value at a time, but that value can change Syntax: dataType identifier; or dataType identifier1, identifier2, …; • Naming convention for variable names: • first letter is lowercase • embedded words begin with uppercase letter
SOFTWARE ENGINEERING TIP Names of variables should be meaningful and reflect the data they will store. • This makes the logic of the program clearer Don't skimp on characters, but avoid extremely long names. Avoid names similar to Java keywords.
Integer Types - Whole Numbers TypeSize Minimum Value Maximum Value in Bytes byte 1 -128 127 short 2 -32,768 32,767 int 4 -2, 147, 483, 648 2, 147, 483, 647 long 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807 Example declarations: int testGrade; int numPlayers, highScore, diceRoll; short xCoordinate, yCoordinate; byte ageInYears; long cityPopulation;
Floating-Point Data Types • Numbers with fractional parts TypeSize Minimum Positive Maximum Value in Bytes Nonzero Value float 4 1.4E-45 3.4028235E38 double 8 4.9E-324 1.7976931348623157E308 Example declarations: float salesTax; double interestRate; double paycheck, sumSalaries;
char Data Type • One Unicode character (16 bits - 2 bytes) TypeSize Minimum Value Maximum Value in Bytes char 2 character character encoded as 0 encoded as FFFF Example declarations: char finalGrade; char newline, tab, doubleQuotes;
boolean Data Type • Two values only: true false • Used for decision making or as "flag" variables Example declarations: boolean isEmpty; boolean passed, failed;