1 / 69

Today!

Today!. Build “HelloWorld” yourself in BlueJ and Eclipse. Look at all the Java keywords. Primitive Types. “HelloWorld” in BlueJ. Find “BlueJ” in the start menu, but start the “Select VM” program instead (you will only need to do this once for every installation of BlueJ.)

zenap
Download Presentation

Today!

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. Today! • Build “HelloWorld” yourself in BlueJ and Eclipse. • Look at all the Java keywords. • Primitive Types. CISC101 - Prof. McLeod

  2. “HelloWorld” in BlueJ • Find “BlueJ” in the start menu, but start the “Select VM” program instead (you will only need to do this once for every installation of BlueJ.) • Make sure the JDK selected is version 1.5.0… or better. If it is not, you may have to open the bottom of the window and browse or search for a newer JDK. If you can’t find one, you will need to install a newer JDK from java.sun.com. • Close this window, and start BlueJ as you normally would. CISC101 - Prof. McLeod

  3. “HelloWorld” in BlueJ, Cont. • Click on “Project”, then “New Project…”. • Browse to a convenient folder (where you can find the project again), and type in a name for the Project – don’t use any spaces (always a good idea with Java filenames and folders!). • Click on “Create” to close the New Project window. The new project name is now at the top of the window. • Click on the “New Class” button at the left. • Type in the class’s name: HelloWorld and leave the “class” checked off. CISC101 - Prof. McLeod

  4. “HelloWorld” in BlueJ, Cont. • Click on “OK” to close the New Class window. • You can now see a new little box representing your class. You can enlarge the box and/or drag it around the screen (entertaining, but not useful!). • Double click on the little box, to open the code editor window. • Delete all the junk in the editor window by pressing <Cntrl>A followed by <Del>. We don’t need all this stuff! (code “auto-generation”). CISC101 - Prof. McLeod

  5. “HelloWorld” in BlueJ, Cont. • Type in the class’ container code: • Save your program by typing <Cntrl>s or go to “Class” then choose “Save”. (Save your code often!!!) public class HelloWorld { } CISC101 - Prof. McLeod

  6. “HelloWorld” in BlueJ, Cont. • You can choose to add a comment at the top of your program, and another comment by the “}”: // My very first Java program! // By me! public class HelloWorld { } // end HelloWorld CISC101 - Prof. McLeod

  7. “HelloWorld” in BlueJ, Cont. • Add the container for the main method: // My very first Java program! // By me! public class HelloWorld { public static void main (String[] args) { } // end main } // end HelloWorld CISC101 - Prof. McLeod

  8. “HelloWorld” in BlueJ, Cont. • Save your program again. Note message at bottom right corner – saying “Saved”. • Click on the “Compile” at the top left. Fix any compilation errors. • Just in case you did not have any – make an error (like removing a “}” or “{“) and compile again to see what happens. Click on the “?” to see a semi-useful message about the compilation error. • Fix all errors and re-compile. CISC101 - Prof. McLeod

  9. “HelloWorld” in BlueJ, Cont. • Leave the editor window open, but move to the original “Project” window. Note how the box looks different if your class has not been compiled. You cannot run your program until it has been compiled. • Right click on your little box and choose “void main(String[] args)” to run your program. • Click on “OK” without changing any parameters in the little text box. • Nothing happens right? CISC101 - Prof. McLeod

  10. “HelloWorld” in BlueJ, Cont. • Our program did what we told it to do – nothing!! • Go back to the editor window and add the following output statement: • Save, compile, and then run your program, as before. (Always, always – Save before running!) System.out.println("Hello World!"); CISC101 - Prof. McLeod

  11. “HelloWorld” in BlueJ, Cont. • You should see the following little exciting window – called the “Terminal” or “Console” window: • On your own, from the project window, choose “Help” then “BlueJ Tutorial”, if you wish to know more. CISC101 - Prof. McLeod

  12. “HelloWorld in Eclipse • The first time you start Eclipse, you will see the “Welcome” page. You should check out some of the information here – particularly one of the simple “Tutorials” and the “Overview”. • For now, click on the “Workbench” link at the top right. • Before proceeding, and for the first time only, you should check the configuration of the program, to make sure it is running the correct Java JDK. CISC101 - Prof. McLeod

  13. “HelloWorld in Eclipse, Cont. • In the top menu bar, go to “Window” then “Preferences”. • Click on the “+” beside “Java” and then choose “Installed JRE’s”. You should see the window on the following slide: CISC101 - Prof. McLeod

  14. CISC101 - Prof. McLeod

  15. “HelloWorld in Eclipse, Cont. • Make sure the 1.5.0… JRE is checked off. • Now choose “Compiler” and make sure the “Compiler Compliance Level” is set to “5.0”. • You should see the following window: CISC101 - Prof. McLeod

  16. CISC101 - Prof. McLeod

  17. “HelloWorld in Eclipse, Cont. • There are lots of other options in “Preferences” but we do not have to worry about any of them right now! • Click on “OK” to close the Preferences window. This might take a little while and if you are asked to re-compile, say “OK”. • Just as in BlueJ, you need to create a new project. Projects can contain many java files, however. Click on the little folder thing at the top left: CISC101 - Prof. McLeod

  18. “HelloWorld in Eclipse, Cont. • Click on “Project” – you will see the Project creation wizard: • Click on the “Next >” button. CISC101 - Prof. McLeod

  19. “HelloWorld in Eclipse, Cont. • On this screen enter a name for your project. You will see: • Click on “Finish”. CISC101 - Prof. McLeod

  20. “HelloWorld in Eclipse, Cont. • Your new project will now be listed at the left in the “Package Explorer”. Click on the “+” beside the project’s name. • There is a link to the JRE System Library, that we are going to ignore. • Click on that little folder thing at the top left, and choose “Class” this time to start the New Java Class wizard. You should see the window on the next slide: CISC101 - Prof. McLeod

  21. Type in “HelloWorld” for the name of your class. Make sure the method stub checkbox for “main” is checked (to make your life easier!). CISC101 - Prof. McLeod

  22. “HelloWorld in Eclipse, Cont. • Click on “Finish” and your new class will show up in the editor window. • Delete comments and/or add your own. • Add the System.out.println() line inside the main method, as you did before. • You will notice some advanced editor features when you hit period for example, and other things such as auto indentation. • Save your program using <Cntrl>s or by clicking on the little floppy disk button at the top left. CISC101 - Prof. McLeod

  23. “HelloWorld in Eclipse, Cont. • Click on the little green arrow thing at the top, as shown on the next slide. • Note that Eclipse compiles your program when it saves it. CISC101 - Prof. McLeod

  24. “HelloWorld in Eclipse, Cont. CISC101 - Prof. McLeod

  25. “HelloWorld in Eclipse, Cont. • Provided you have not made any compiler errors, you should see your output in the Console window at the bottom. • Make some deliberate errors in your code to see what happens. Eclipse pre-compiles your program as you write it, and will not run it until you have fixed all the errors it has detected. CISC101 - Prof. McLeod

  26. Development Environments • You can choose BlueJ or Eclipse (or any other tool you like!). • I will usually use Eclipse for class demonstrations. • (I’m not going to try the command prompt again…) CISC101 - Prof. McLeod

  27. Java Keywords CISC101 - Prof. McLeod

  28. Java Keywords, Cont. • The goto keyword is reserved but does not do anything in java. • On the next slide I have highlighted the keywords that we will be using in this course. CISC101 - Prof. McLeod

  29. Java Keywords for CISC101 CISC101 - Prof. McLeod

  30. Java Keywords for CISC101, by Category CISC101 - Prof. McLeod

  31. Other “Things” in Java Programs • Comments • Variable names • Literal values • “Punctuation” like: ; . , ( ) { } [ ] • Operators like: + - / % > < == = ! && || • We will also see lots of other object names along with their “members” – attributes or methods. CISC101 - Prof. McLeod

  32. Expressions • A line of code in java often contains expressions. • They are combinations of any or all of: • Variables • Literal Values • Operators • Method Invocations • A line of code in java is terminated by a “;” CISC101 - Prof. McLeod

  33. Variables • Java is a “declarative” language. • This means that you have to declare a variable before you can use it. • What is a variable anyways? • A name for a piece of memory used to store something. • The “something” can be a primitive type value or a pointer to an object (a memory reference). • (Don’t worry about pointers, yet!!) CISC101 - Prof. McLeod

  34. Variables, Cont. • To create a variable, you must state what type it is going to be. • For example, to create a variable called aNum of type int, you would use the following line of code: int aNum; • You could assign a value to aNum using something like: aNum = 200; CISC101 - Prof. McLeod

  35. Back to Expressions • What is a literal value? • We just saw one: 200 • What is an operator? • We just saw one of those too: = • Other operators, for example, are: + - / * • And, what is an expression? • We just saw one of those too!: aNum = 200; CISC101 - Prof. McLeod

  36. Expressions • Now, let’s consider each of these elements again, in more detail: variables, literal values, operators. CISC101 - Prof. McLeod

  37. Java Primitive Types • “Primitive Type” variables are those that are not “Objects” in Java. • Primitive Type Variables fall into the categories of integer types, real types, characters and booleans. CISC101 - Prof. McLeod

  38. Primitive Types - Cont. CISC101 - Prof. McLeod

  39. An Integer Primitive Type Variable • Declare using the “int” keyword. • From -2147483648 to 2147483647, inclusive (4 bytes). • (A “byte” is 8 bits, where a “bit” is either 1 or 0.) • For example: int aNum; CISC101 - Prof. McLeod

  40. More Integer Primitive Types • Other primitive types: byte, short and long: • For byte, from -128 to 127, inclusive (1 byte). • For short, from -32768 to 32767, inclusive (2 bytes). • (For int, from -2147483648 to 2147483647, inclusive (4 bytes). ) • For long, from -9223372036854775808 to 9223372036854775807, inclusive (8 bytes). CISC101 - Prof. McLeod

  41. Aside - Storage of Integers • Computers like to store numbers in binary - a memory location is either “on” or “off”. • An “un-signed” 8 digit binary number can range from 00000000 to 11111111 • 00000000 is 0 in base 10. • 11111111 is 1x20 + 1x21 + 1x22 + … + 1x27 = 255, base 10. CISC101 - Prof. McLeod

  42. Storage of Integers - Cont. • So, how can a negative binary number be stored? • Use the “two’s complement” system of storage. • Make the most significant bit a negative number: • So, the lowest “signed” binary 8 digit number is now: 10000000, which is -1x27, or -128 base 10. CISC101 - Prof. McLeod

  43. Storage of Integers - Cont. CISC101 - Prof. McLeod

  44. Storage of Integers - Cont. • For example, the binary number 10010101 is 1x20 + 1x22 + 1x24 - 1x27 = 1 + 4 + 16 - 128 = -107 base 10 • Now you can see how the primitive integer type, byte, ranges from -128 to 127. CISC101 - Prof. McLeod

  45. Storage of Integers - Cont. • Suppose we wish to add 1 to the largest byte value: 01111111 +00000001 • This would be equivalent to adding 1 to 127 in base 10 - the result would normally be 128. • In base 2, using two’s complement, the result of the addition is 10000000, which is -128 in base 10! • So integer numbers wrap around, in the case of overflow - no warning is given! CISC101 - Prof. McLeod

  46. Storage of Integers - Cont. • An int is stored in 4 bytes using “two’s complement”. • An int ranges from: 10000000 00000000 00000000 00000000 to 01111111 11111111 11111111 11111111 or -2147483648 to 2147483647 in base 10 CISC101 - Prof. McLeod

  47. A Double Primitive Type Variable • Declare using the “double” keyword. • For double, (8 bytes) roughly ±4.9 x 10-308 to ±1.7 x 10308 to 15 significant digits. • For example: double aVal; CISC101 - Prof. McLeod

  48. The Other Real Primitive Type • Also have float: • For float, (4 bytes) roughly ±1.4 x 10-38 to ±3.4 x 1038 to 7 significant digits. • For double, (8 bytes) roughly ±4.9 x 10-308 to ±1.7 x 10308 to 15 significant digits. CISC101 - Prof. McLeod

  49. Integer Literals • A literal integer value is assumed to be of type “int”. • If you want the literal to be a long, then you must add the letter “L” to the end of the literal value. CISC101 - Prof. McLeod

  50. Double Literals • Java assumes a literal number like “2.5” or “3.45e-7” is a double literal. • If you want a literal to be recognized as a float type, then you must add the letter “F” to the end of the number. CISC101 - Prof. McLeod

More Related