1 / 15

Getting Started

Getting Started. Contents. Constructing a simple program Java identifiers The main( ) function Block structure Programming style Comments Programming tips. Early programs will consist of a single function -- main. Visibility modifier. Scope designation. Argument list. return type.

Download Presentation

Getting Started

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. Getting Started Contents • Constructing a simple program • Java identifiers • The main( ) function • Block structure • Programming style • Comments • Programming tips

  2. Early programs will consist of a single function -- main Visibility modifier Scope designation Argument list return type Package & class String constant to be output to screen function identifier Getting Started A first java program Write a program to write the string “Hello World” to the screen. Every operation in Java must belong to a class publicclass HelloWorld { publicstaticvoid main (String [ ] args) { System.out.println(“Hello World”); } } Class declaration Function header Function body

  3. Getting Started Identifiers A programmer must supply a name for all classes, objects, and primitive type variables that he or she introduces in a program. A properly formed java identifier must 1. Begin with a letter {a..z, A..Z} or $ or _ {underscore} • Be followed by zero or more of the following characters : • letters, digits, $, or _ 3. Must not be one of the reserved keywords of the language Such as: public, class, int, float, double, const, byte, etc.

  4. There is no limit on the length of an identifier, but this is a bad choice for readability, ease of coding, and error potential Getting Started Identifiers Improperly formed identifiers Examples of properly formed identifiers salesPrice _x _y item1 item2 item3 x_var $dump $double HelloWorld supercalifragilisticexpialidotious Starting digit 1stNumber sales tax @yahoo.com No spaces illegal chars @ and . Identifiers are case-sensitive Price, price and PRICE are three different identifiers

  5. Getting Started Identifiers – java conventions Class names start with a capital letter Ex. publicclass HelloWorld { The names of objects and variables start with a lower case letter Use capital letters to distinguish between words that are merged together salePrice xCoord distanceFromOrigin Use identifier names that indicate what the object or variable is being used for in the program. Bad choice: int x; Better choice: int numberSold;

  6. table memory 1234:5000 1234:5002 1234:5004 1234:5006 1234:5008 1234:500A Getting Started Just what is an identifier? An identifier is a means of locating the starting address of a segment of memory. When an identifier is declared, a table entry is established by the compiler which associates the identifier with the address in memory. The amount of memory that is allocated to an identifier depends upon the type specified in the declaration int example1 = 24; double example2 = 123.45; example1 1234:5000 example2 1234:5004 Binary 24 in 4 bytes Binary representation of 123.456 in 8 bytes

  7. Getting Started The main function The compiler will look for a main function to execute in the class that has the same name as the file being compiled In the Sun compiler, the program illustrated earlier will be saved in a file named HelloWorld.java To compile this file type: javac HelloWorld.java If there are no errors, this will produce a file named: HelloWorld.class To run the program, type: java HelloWorld

  8. Function main( ) takes as an argument an array of strings. You will use this feature later in the course, but it must always be included in the header Nothing is returned – initially the main( ) function will implement a specific task that will be fully contained within the function. Function main( ) must be declared as having static scope. It is implemented immediately without first creating an object of the class in which it appears. Function main( ) must be declared public to be visible outside of the class. Getting Started The main( ) function The main( ) function always has the same function header publicstaticvoid main (String [ ] args)

  9. Outer block contains all of the data and methods in the class Inner block contains all of the statements in the method Getting Started Block structure The body of the method main( ) follows the method header. The body of the method is enclosed in curly brackets { .. } publicclass HelloWorld { publicstaticvoid main(String [ ] args) { System.out.println( “HelloWorld”); } }

  10. Statements are left adjusted (lined up one directly below the previous) indent 2 to 4 spaces for each block. Getting Started Block structure Each statement inside of a block ends with a semicolon. Good style demands that there is one statement per line. publicstaticvoid main(String [ ] args) { statement1; statement2; statement3; } publicstaticvoid main(String [ ] args) { statement; statement; } Note! An alternate coding style:

  11. Getting Started Programming style In order to learn to write programs, you should first carefully read programs that you find in the text, notes, and lecture materials. Be sure you understand what every statement means, and be sure to observe and follow the style in which these programs are written. You are not writing poetry like e. e. cummings! Be sure to indent each new block by 2 to 4 spaces, and be sure every statement is properly aligned. Programs must be readable by more people than the original author, therefore it is important to adhere to a standard coding format.

  12. Getting Started Comments Comments are indicated by a preceding double slash // this is a comment Comments that extend beyond a single line are enclosed between a beginning /* and ending */ star/slash pairs /* Hello World program * Version 1 * written by J. Ten Eyck */ publicclass HelloWorld { publicstaticvoid main(String [ ] ) { System.out.println(“Hello World”); //print to the screen } } Comment block gives program information

  13. Getting Started Comments Comments should be used to make the code more readable, however, too many comments become intrusive and do not help make the program more understandable. Use comments only when they are needed for emphasis or to make something more understandable. Using identifiers that make their role explicit is more helpful in general than a comment. Example finaldouble taxRate = 0.05; double price, payment, salesTax; price = 24.99; salesTax = price * taxRate; payment = price + salesTax; finaldouble taxRate = 0.05; double x, y, z; x = 24.99; //x = the price y = x * taxRate; //y = the sales tax z = x + y; //z = the payment due clearer lessclear

  14. Getting Started Programming tips Start small! If a program is large or has some complex logical operations, do a smaller, straightforward portion of the program first and get it to compile and run. Then add additional code or complexity a little at a time (and recompile and run) until the whole program is complete. Don’t be afraid to make mistakes. That’s what error messages are for. Be like the stereotypical male driver and try to find your way to a solution by taking what you think might be the right road until you either succeed or get hopelessly lost and need to ask directions. You will be a wiser programmer for having made the mistakes and learned from them. If you want to know whether something can be done, try it! Keep a small test program shell like the one on the next page to try things that you are uncertain about.

  15. Getting Started A test shell program publicclass TestShell { publicstaticvoid main(String [ ] args) { //enter statements here } } Whenever you have a question as to whether something can be done in Java or what output will be produced from a given code fragment, plug the questionable code into this shell and run it. See what you get and then try to understand why you got such an output. OR use the interactive feature of your IDE

More Related