Spring 2006CISC101 - Prof. McLeod1 Today! Build “HelloWorld” yourself in BlueJ and Eclipse. Look at all the Java keywords. Primitive Types.
Spring 2006CISC101 - Prof. McLeod2 “HelloWorld” in BlueJ 1.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.) 2.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. 3.Close this window, and start BlueJ as you normally would.
Spring 2006CISC101 - Prof. McLeod3 “HelloWorld” in BlueJ, Cont. 4.Click on “Project”, then “New Project…”. 5.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!). 6.Click on “Create” to close the New Project window. The new project name is now at the top of the window. 7.Click on the “New Class” button at the left. 8.Type in the class’s name: HelloWorld and leave the “class” checked off.
Spring 2006CISC101 - Prof. McLeod4 “HelloWorld” in BlueJ, Cont. 9.Click on “OK” to close the New Class window. 10.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!). 11.Double click on the little box, to open the code editor window. 12.Delete all the junk in the editor window by pressing A followed by. We don’t need all this stuff! (code “auto-generation”).
Spring 2006CISC101 - Prof. McLeod5 “HelloWorld” in BlueJ, Cont. 13.Type in the class’ container code: 14.Save your program by typing s or go to “Class” then choose “Save”. (Save your code often!!!) public class HelloWorld { }
Spring 2006CISC101 - Prof. McLeod6 “HelloWorld” in BlueJ, Cont. 15.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
Spring 2006CISC101 - Prof. McLeod7 “HelloWorld” in BlueJ, Cont. 16.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
Spring 2006CISC101 - Prof. McLeod8 “HelloWorld” in BlueJ, Cont. 17.Save your program again. Note message at bottom right corner – saying “Saved”. 18.Click on the “Compile” at the top left. Fix any compilation errors. 19.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. 20.Fix all errors and re-compile.
Spring 2006CISC101 - Prof. McLeod9 “HelloWorld” in BlueJ, Cont. 21.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. 22.Right click on your little box and choose “ void main(String[] args) ” to run your program. 23.Click on “OK” without changing any parameters in the little text box. 24.Nothing happens right?
Spring 2006CISC101 - Prof. McLeod10 “HelloWorld” in BlueJ, Cont. 25.Our program did what we told it to do – nothing!! 26.Go back to the editor window and add the following output statement: 27.Save, compile, and then run your program, as before. (Always, always – Save before running!) System.out.println("Hello World!");
Spring 2006CISC101 - Prof. McLeod11 “HelloWorld” in BlueJ, Cont. 28.You should see the following little exciting window – called the “Terminal” or “Console” window: 29.On your own, from the project window, choose “Help” then “BlueJ Tutorial”, if you wish to know more.
Spring 2006CISC101 - Prof. McLeod12 “HelloWorld in Eclipse 1.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”. 2.For now, click on the “Workbench” link at the top right. 3.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.
Spring 2006CISC101 - Prof. McLeod13 “HelloWorld in Eclipse, Cont. 4.In the top menu bar, go to “Window” then “Preferences”. 5.Click on the “+” beside “Java” and then choose “Installed JRE’s”. You should see the window on the following slide:
Spring 2006CISC101 - Prof. McLeod14
Spring 2006CISC101 - Prof. McLeod15 “HelloWorld in Eclipse, Cont. 6.Make sure the 1.5.0… JRE is checked off. 7.Now choose “Compiler” and make sure the “Compiler Compliance Level” is set to “5.0”. 8.You should see the following window:
Spring 2006CISC101 - Prof. McLeod16
Spring 2006CISC101 - Prof. McLeod17 “HelloWorld in Eclipse, Cont. 9.There are lots of other options in “Preferences” but we do not have to worry about any of them right now! 10.Click on “OK” to close the Preferences window. This might take a little while and if you are asked to re-compile, say “OK”. 11.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:
Spring 2006CISC101 - Prof. McLeod18 “HelloWorld in Eclipse, Cont. 12.Click on “Project” – you will see the Project creation wizard: 13.Click on the “Next >” button.
Spring 2006CISC101 - Prof. McLeod19 “HelloWorld in Eclipse, Cont. 14.On this screen enter a name for your project. You will see: 15.Click on “Finish”.
Spring 2006CISC101 - Prof. McLeod20 “HelloWorld in Eclipse, Cont. 16.Your new project will now be listed at the left in the “Package Explorer”. Click on the “+” beside the project’s name. 17.There is a link to the JRE System Library, that we are going to ignore. 18.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:
Spring 2006CISC101 - Prof. McLeod21 19.Type in “HelloWorld” for the name of your class. Make sure the method stub checkbox for “main” is checked (to make your life easier!).
Spring 2006CISC101 - Prof. McLeod22 “HelloWorld in Eclipse, Cont. 20.Click on “Finish” and your new class will show up in the editor window. 21.Delete comments and/or add your own. 22.Add the System.out.println() line inside the main method, as you did before. 23.You will notice some advanced editor features when you hit period for example, and other things such as auto indentation. 24.Save your program using s or by clicking on the little floppy disk button at the top left.
Spring 2006CISC101 - Prof. McLeod23 “HelloWorld in Eclipse, Cont. 25.Click on the little green arrow thing at the top, as shown on the next slide. 26.Note that Eclipse compiles your program when it saves it.
Spring 2006CISC101 - Prof. McLeod24 “HelloWorld in Eclipse, Cont.
Spring 2006CISC101 - Prof. McLeod25 “HelloWorld in Eclipse, Cont. 27.Provided you have not made any compiler errors, you should see your output in the Console window at the bottom. 28.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.
Spring 2006CISC101 - Prof. McLeod26 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…)
Spring 2006CISC101 - Prof. McLeod27 Java Keywords abstractdoubleintsuper assertelseinterfaceswitch booleanenumlongsynchronized breakextendsnativethis bytefornewthrow casefinalpackagethrows catchfinallyprivatetransient charfloatprotectedtry classgotopublicvoid constifreturnvolatile continueimplementsshortwhile defaultimportstatic doinstanceofstrictfg
Spring 2006CISC101 - Prof. McLeod28 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.
Spring 2006CISC101 - Prof. McLeod29 Java Keywords for CISC101 abstractdoubleintsuper assertelseinterfaceswitch booleanenumlongsynchronized breakextendsnativethis bytefornewthrow casefinalpackagethrows catchfinallyprivatetransient charfloatprotectedtry classgotopublicvoid constifreturnvolatile continueimplementsshortwhile defaultimportstatic doinstanceofstrictfg
Spring 2006CISC101 - Prof. McLeod30 Java Keywords for CISC101, by Category Primitive TypesConditionalsLoopsStructural boolean byte if else do for class import char double float int switch case while continue break new public return static long short final void
Spring 2006CISC101 - Prof. McLeod31 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.
Spring 2006CISC101 - Prof. McLeod32 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 “ ; ”
Spring 2006CISC101 - Prof. McLeod33 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!!)
Spring 2006CISC101 - Prof. McLeod34 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;
Spring 2006CISC101 - Prof. McLeod35 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;
Spring 2006CISC101 - Prof. McLeod36 Expressions Now, let’s consider each of these elements again, in more detail: variables, literal values, operators.
Spring 2006CISC101 - Prof. McLeod37 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.
Spring 2006CISC101 - Prof. McLeod38 Primitive Types - Cont. IntegerRealCharacterBoolean bytefloatcharboolean shortdouble int long
Spring 2006CISC101 - Prof. McLeod39 An Integer Primitive Type Variable Declare using the “ int ” keyword. From to , inclusive (4 bytes). (A “byte” is 8 bits, where a “bit” is either 1 or 0.) For example: int aNum;
Spring 2006CISC101 - Prof. McLeod40 More Integer Primitive Types Other primitive types: byte, short and long : For byte, from -128 to 127, inclusive (1 byte). For short, from to 32767, inclusive (2 bytes). (For int, from to , inclusive (4 bytes). ) For long, from to , inclusive (8 bytes).
Spring 2006CISC101 - Prof. McLeod41 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 to is 0 in base is 1x x x2 2 + … + 1x2 7 = 255, base 10.
Spring 2006CISC101 - Prof. McLeod42 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: , which is -1x2 7, or -128 base 10.
Spring 2006CISC101 - Prof. McLeod43 Storage of Integers - Cont. binarybase
Spring 2006CISC101 - Prof. McLeod44 Storage of Integers - Cont. For example, the binary number is 1x x x x2 7 = = -107 base 10 Now you can see how the primitive integer type, byte, ranges from -128 to 127.
Spring 2006CISC101 - Prof. McLeod45 Storage of Integers - Cont. Suppose we wish to add 1 to the largest byte value: 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 , which is -128 in base 10! So integer numbers wrap around, in the case of overflow - no warning is given!
Spring 2006CISC101 - Prof. McLeod46 Storage of Integers - Cont. An int is stored in 4 bytes using “two’s complement”. An int ranges from: to or to in base 10
Spring 2006CISC101 - Prof. McLeod47 A Double Primitive Type Variable Declare using the “ double ” keyword. For double, (8 bytes) roughly ±4.9 x to ±1.7 x to 15 significant digits. For example: double aVal;
Spring 2006CISC101 - Prof. McLeod48 The Other Real Primitive Type Also have float : For float, (4 bytes) roughly ±1.4 x to ±3.4 x to 7 significant digits. For double, (8 bytes) roughly ±4.9 x to ±1.7 x to 15 significant digits.
Spring 2006CISC101 - Prof. McLeod49 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.
Spring 2006CISC101 - Prof. McLeod50 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.
Spring 2006CISC101 - Prof. McLeod51 char Primitive Type Declared as: char aChar; Char literals look like: ‘a’‘B’‘1’‘ ’ (a space)
Spring 2006CISC101 - Prof. McLeod52 char Primitive Type - Cont. A char primitive type can also refer to a “Unicode” character. For example: char aChar = ‘\u03C0’; Where 03C0 is the hexadecimal (base 16) value for the Unicode character: Since two bytes are used to designate the character value, it is possible to refer to 2 16 = 65,536 different characters.
Spring 2006CISC101 - Prof. McLeod53 Aside - Unicode Characters The ASCII code system only goes to 255 since it only uses one byte. At the moment, just over 49,000 of the Unicode positions are in use. See
Spring 2006CISC101 - Prof. McLeod54 A Boolean Primitive Type Variable Declare using the “ boolean ” keyword. Either true or false For example: boolean aFlag; boolean literals are: true, false
Spring 2006CISC101 - Prof. McLeod55 Aside - String’s String ’s are not primitive data types, but are Objects. We will discuss the difference later… A String can be declared in the same way as a primitive type using the keyword: String. For example: String response;
Spring 2006CISC101 - Prof. McLeod56 Variable Declaration To declare a variable, use the Java “keyword” appropriate for the type of variable you are declaring followed by a variable name you have created, followed by a semicolon. Examples: int aNum; double totalVolume; String userPrompt;
Spring 2006CISC101 - Prof. McLeod57 Legal Variable Names Java names may contain any number of letters, numbers and underscore (“_”) characters, but they must begin with a letter. Standard Java Naming Convention: –Names beginning with lowercase letters are variables or methods. –Names beginning with uppercase letters are class names. –Successive words within a name are capitalized. –Names in all capital letters are constants. (We’ll get to “constants” shortly).
Spring 2006CISC101 - Prof. McLeod58 Legal Variable Names - Cont. Legal names: myData x1 AnotherName TWO_PI Illegal names: _toUpper 1Day Variable or method names Class names Constant
Spring 2006CISC101 - Prof. McLeod59 Literal Values int ’s, for example: double numbers, for example: E E45 boolean, for example: true String literals: “Hello!”“ spaces”
Spring 2006CISC101 - Prof. McLeod60 Variable Declaration - Cont. int and double variables initially are given a value of zero unless they are initialized to a value. Java may prevent you from using variables that are not initialized. So, it is often necessary to initialize your variables before use, for example: int numDaysInYear = 365; double avgNumDaysInYear = ; String greetingLine = “Hello there!”;
Spring 2006CISC101 - Prof. McLeod61 Variable Declaration - Cont. All these statements could be carried out in two lines, for example: int numDaysInWeek = 7; Is the same as: int numDaysInWeek; numDaysInWeek = 7;
Spring 2006CISC101 - Prof. McLeod62 Constants The Java keyword, final can be used to make sure a variable value is no longer “variable”. It becomes a constant, because Java will not allow your program to change its value once it has been declared: final int NUM_DAYS_IN_YEAR = 365; final double MM_PER_INCH = 25.4;
Spring 2006CISC101 - Prof. McLeod63 Arithmetic Operators The standard arithmetic operators in Java are: –Addition ( + ) –Subtraction ( - ) –Multiplication ( * ) –Division ( / ) –Modulo (or “remainder”) (%) All of these operations apply to all numeric data types. All require values on both sides of the operator.
Spring 2006CISC101 - Prof. McLeod64 Aside - Strings and the “ + ” Operator Not only can “ + ” operate on numeric values, but it can also handle String ’s on either or both sides. If one side is not a String, it will be changed to one, and then it will be concatenated to the String on the other side: 4 + “you” evaluates to “4you” “apples” + “oranges” + 99 evaluates to “applesoranges99” “little piggies” evaluates to “10little piggies”
Spring 2006CISC101 - Prof. McLeod65 Logical Operators Return either true or false. All these require values on both sides of the operator. == equals to != not equals to > greater than < less than >= greater than or equal to <= less than or equal to && logical “And” || logical “Or”
Spring 2006CISC101 - Prof. McLeod66 Logical Operators - Cont. “Truth” tables for the “And” and “Or” operators: && testa testbtruefalse true false || testa testbtruefalse true falsetruefalse boolean result = testa && testb; boolean result = testa || testb;
Spring 2006CISC101 - Prof. McLeod67 Assignment Operator = set equal to This is all we need to be able to build Java expressions. There are other assignment operators (+=, -=, *=, /=), but we’ll look at these later. We will also consider “unary” operators later too.
Spring 2006CISC101 - Prof. McLeod68 Expressions Expressions are combinations of variables, literal values, and operators. For example: int aNum = * 7;// aNum is 25 int aNum = (4 + 3) * 7;// aNum is 49 (4 > 7) || (10 > -1)// yields true (5.5 >= 5.0) && (4.0 != 1.0)// yields true double circ = 3.14 * 2.0 * r; –(“ // ” precedes a comment in Java)
Spring 2006CISC101 - Prof. McLeod69 Precedence Rules Operator precedence rules determine which operations take place in what order: –First *, /, % –Then +, - –Then, = –Then ==, != –Then &&, || –Last = Use “( )” to control order of operations, as the expression inside “( )” will be evaluated before stuff outside of “( )”.