1 / 26

Lecture 3 Java Basics

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

gladys
Download Presentation

Lecture 3 Java Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 3Java Basics Richard Gesick

  2. Java Basics • Java Application Structure • Data Types, Variables, and Constants • Expressions and Arithmetic Operators

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

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

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

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

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

  8. 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" );

  9. 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" ); }

  10. 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 )

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

  12. 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 */

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

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

  15. 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 */

  16. 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 }

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

  18. 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 );

  19. Data Types, Variables, and Constants • Declaring Variables • Primitive Data Types • Initial Values and Literals • String Literals and Escape Sequences • Constants

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

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

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

  23. 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;

  24. 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;

  25. 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;

  26. boolean Data Type • Two values only: true false • Used for decision making or as "flag" variables Example declarations: boolean isEmpty; boolean passed, failed;

More Related