Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1/18R. Smith - University of St Thomas - Minnesota Today’s Class Recap - simple Java programRecap - simple Java program Simple types and printingSimple.

Similar presentations


Presentation on theme: "March 2005 1/18R. Smith - University of St Thomas - Minnesota Today’s Class Recap - simple Java programRecap - simple Java program Simple types and printingSimple."— Presentation transcript:

1 March 2005 1/18R. Smith - University of St Thomas - Minnesota Today’s Class Recap - simple Java programRecap - simple Java program Simple types and printingSimple types and printing “Primitive types” in general“Primitive types” in general Back to “Lab #1”Back to “Lab #1” – due first thing TOMORROW.

2 March 2005 2/18R. Smith - University of St Thomas - Minnesota Sample Java Program public class hello { public static void main (String[] args) { System.out.println(“Hello, world!”); }}

3 March 2005 3/18R. Smith - University of St Thomas - Minnesota “Compiling” - the mechanics Should work the same on PCs and MacsShould work the same on PCs and Macs You have to “compile” programs to run themYou have to “compile” programs to run them $ javac hello.java $ You have to use the “JVM” to run them, tooYou have to use the “JVM” to run them, too $ java hello Hello, world! $

4 March 2005 4/18R. Smith - University of St Thomas - Minnesota “Types” of variables First use: you say the typeFirst use: you say the type int = “integer”int = “integer” signed, no decimal part, less than 2Gsigned, no decimal part, less than 2G String = character stringString = character string Usually captured in quotesUsually captured in quotes Note that “String” is capitalizedNote that “String” is capitalized ExamplesExamples int sampleInt = 12; sampleInt = 25; System.out.println(sampleInt);

5 March 2005 5/18R. Smith - University of St Thomas - Minnesota String and int example String myname = “Joe”; int age = 25; System.out.print(“My name is “ +myname + “. I am “ + age +”.”); myname = “Barb”; age = 26 System.out.print(“My name is “ +myname + “. I am “ + age +”.”);

6 March 2005 6/18R. Smith - University of St Thomas - Minnesota Primitive Types Java programs deal with 2 kinds of thingsJava programs deal with 2 kinds of things –Primitive Types, like int –Objects, like String (note it’s capitalized) Four ‘types’ of primitive typesFour ‘types’ of primitive types –Integers in 4 sizes –Floating point numbers in 2 sizes –Booleans - Chars WHY???WHY??? –It’s based on computer hardware –Computers can easily deal with those 8 types

7 March 2005 7/18R. Smith - University of St Thomas - Minnesota Primitive types vs Objects Primitive Types Variables onlyVariables only Primitive operationsPrimitive operations One value eachOne value each No AttributesNo Attributes No MethodsNo Methods Objects Attributes Methods Multiple values

8 March 2005 8/18R. Smith - University of St Thomas - Minnesota Java is “Strongly Typed” Prevents “risky” data movementPrevents “risky” data movement ExampleExample byte little; int bigger; long biggest; bigger=little; // OK little=bigger; // ERROR; biggest=little; //OK little=biggest; //ERROR

9 March 2005 9/18R. Smith - University of St Thomas - Minnesota Chars and Strings are interesting Consider the following:Consider the following: int intThree = 3; String strThree = “3”; char charThreeA = 51; char charThreeB = ‘3’; We can print all of these.We can print all of these. –We can only do arithmetic on the first one The char holds a numeric codeThe char holds a numeric code –We get digits for numeric values 48 plus –We get letters for numeric values 65 plus A String is an “array” of charsA String is an “array” of chars

10 March 2005 10/18R. Smith - University of St Thomas - Minnesota Lab Problem #1 Given the text, write print and/or println method calls to print it out. Replace the name (“Jon”) and pronoun (“he”) with string variables. Set the variables to different values and produce the results: Name=Sally, pronoun=she Name=Frank, pronoun=he

11 March 2005 11/18R. Smith - University of St Thomas - Minnesota The Text Jon went to the store. When he arrived, he got a shopping cart. Then he walked to the produce section. Jon was surprised. The raspberries were fresh, so he picked up two boxes. Then he went to the cereal section and picked up three boxes. Jon really likes cereal. After he had visited the whole store, Jon pushed the cart to the front. There, he paid for the groceries. It took six bags to carry all of Jon's groceries. The End.

12 March 2005 12/18R. Smith - University of St Thomas - Minnesota Suggestion Do the first line, compile it, run it.Do the first line, compile it, run it. –Fix what doesn’t work, try again till it’s right. Add another line, compile, run.Add another line, compile, run. Etc.Etc. Find and fix problems incrementally.Find and fix problems incrementally. –It’s much easier than trying to fix 30 different errors.

13 March 2005 13/18R. Smith - University of St Thomas - Minnesota Summary of Data Types IntegerInteger FloatFloat BooleanBoolean CharChar

14 March 2005 14/18R. Smith - University of St Thomas - Minnesota Integer Data Types The SizesThe Sizes –byte = range +/- 127 –short = range +/- 32767 –int = range +/- 2 billion –long = range +/- 9 quintillion Literal valuesLiteral values –Must be all digits –No decimal point, or the letter E.

15 March 2005 15/18R. Smith - University of St Thomas - Minnesota Floating point data types Two floating point number sizesTwo floating point number sizes –float = 7 significant digits, +/- 10^38 –double = 15 significant digits +/- 10^308 Literal valuesLiteral values –Insert a decimal point, you get a double –End it with the letter “f” and you get a float –Scientific notation: 2.34E-23

16 March 2005 16/18R. Smith - University of St Thomas - Minnesota Boolean data type Yield values “true” and “false”Yield values “true” and “false” Actual variables take up the smallest practical amount of RAMActual variables take up the smallest practical amount of RAM Boolean expressions are essential for controlling your programsBoolean expressions are essential for controlling your programs Literal boolean values:Literal boolean values: –true, false

17 March 2005 17/18R. Smith - University of St Thomas - Minnesota Character ‘char’ data type Stores a single keystrokeStores a single keystroke –Letter, digit, punctuation, etc. –Strings are “arrays” of chars LiteralsLiterals –A character enclosed in single quotes ‘A’ ‘B’ ‘C’‘A’ ‘B’ ‘C’ –A ‘String’ is enclosed in double quotes “A B C”“A B C” A char is also a numberA char is also a number –A coded value for a keystroke

18 March 2005 18/18R. Smith - University of St Thomas - Minnesota


Download ppt "March 2005 1/18R. Smith - University of St Thomas - Minnesota Today’s Class Recap - simple Java programRecap - simple Java program Simple types and printingSimple."

Similar presentations


Ads by Google