Lecture 2 - OutlineJava Program StructureCharacter StringsInteractive Program - OutputThe print MethodNote:Exercise:String ConcatenationSpecial Characters:Escape SequencesSlide 11OutlineVariablesVariablesPrimitive DataNumeric Primitive DataPrimitive Data - CharactersPrimitive Data - Character SetsPrimitive Data - CharactersSlide 20BooleanConstantsSlide 23Slide 24AssignmentExpressionsDivision and RemainderOperator PrecedenceOrder of OperationsSlide 30Slide 31Slide 32Slide 33Assignment RevisitedSlide 35Increment and DecrementPostfix and Prefix operatorsSlide 38Order of Operations:Assignment OperatorsSlide 41Examples:Slide 43Data ConversionSlide 45Slide 46PromotionAssignment ConversionCastingSlide 50Strings as ObjectsSlide 52Slide 53Strings MethodsString MethodsSlide 56Interactive Programs - InputReading InputInput TokensSlide 60In Class ChallengeLecture 2 - Outline2-1Character StringsVariablesPrimitive Data Types ConstantsAssignments and ExpressionsData ConversionThe String ClassInteractive ProgramsJava Program Structure1-2public class HelloWorld{}// Hello World Applicationpublic static void main (String[] args){}// Java application to display the message// Hello World on the screenSystem.out.println("Hello World!");Character StringsA string literal is a sequence of zero or more characters enclosed by double quotes.Examples:"Hello World!""This is a string literal.""123 Main Street""X"The empty string ("") is the string with no charactersString literals cannot extend over more than one line.2-3System.out.println( "Hello World!“);The Java compiler will report a syntax errorInteractive Program - Output•To display information on the console, Java uses methods of the object System.out•The System.out object represents the monitor screen to which we can send output.•The method println display a string to the screen, then move to the next line2-4System.out.println ("Go Dawgs!!!.");objectmethodnameinformation provided to the method(argument/parameter)The print Method•The print method is similar to the println method,But, it does not skip to the next line after displaying the string. It leaves the cursor where ever the last letter was printedExample:2-5System.out.print("Merry Christmas"); System.out.println("and Happy New Year.");Merry Christmasand Happy New Year.Note:•If you want to leave a space between the words “Christmas ” and “and” you must add it either after Christmas or before and. System.out.print (“Merry Christmas ”); System.out.print (“and happy new year”); 2-6Exercise:•What is the output of the following statements? System.out.print (“I am”); System.out.print(“a student”); System.out.println (“ of:”); System.out.println (“Java”);2-7I ama student of:JavaString ConcatenationThe string concatenation operator (+) is used to append one string to the end of anotherSystem.out.println ("cats " + "and dogs“);You can also be used to append a number to a string.System.out.println("UGA Sugar Bowl Champions "+ 2009);The + operator is the concatenation operator when one of the operands is a string.2-8Special Characters:•How can we display the following output ?Error. Why ?System.out.println (“He said “time is a great teacher”);2-9He Said “Time is a great teacher”Escape Sequences•Special characters are represented in a string using a escape sequence.•An escape sequence is a series of characters that represents a special character•An escape sequence begins with a backslash character (\).2-10Escape Sequence\b\t\n\r\"\'\\Meaningbackspacetabnewlinecarriage returndouble quotesingle quotebackslashSystem.out.println(“He said: \“ Time is a great teacher \“ “); System.out.println(“Harry Potter\nis very-very \n\nfamous “); 2-11He Said “Time is a great teacher”Harry Potter is very-veryFamousOutline2-12Character StringsVariables,Primitive Data Types and ConstantsAssignments and ExpressionsData ConversionThe String ClassInteractive ProgramsVariablesA variable is a name for a location of memory that stores a valueThe value of the variable may change during the execution of the programA variable must be declared by specifying the variable's name and the type of data that it will holdBy convention, variables names starts with a lowercase letter 2-13int total;int x, y, z;data typevariable nametotalxyzVariables•After declaring a variable inside a method (main method in our case) you can use it anywhere inside the body of that method.int total ;total = 50; // java is case sensitive.//therefore Total = 50; is a syntax error2-14•A variable can be given an initial value in the declarationint sum = 0;int base = 32, max = 149;sum0base32max149Primitive DataThere are eight primitive data types in JavaInteger data types:byte, short, int, long Floating point numbers data types :float, doubleCharacters:charBoolean:boolean2-15Numeric Primitive Data•The difference between the various numeric primitive types is their size, and therefore the range of values they can hold:2-16TypebyteshortintlongfloatdoubleStorage8 bits16 bits32 bits64 bits32 bits64 bitsMin Value-128-32,768-2,147,483,648< -9 x 1018+/- 3.4 x 1038 7 significant digits+/- 1.7 x 10308 15 significant digitsMax Value12732,7672,147,483,647> 9 x 1018TypebyteshortintlongfloatdoubleStorage8 bits16 bits32 bits64 bits32 bits64 bitsMin Value-128-32,768-2,147,483,648< -9 x 1018+/- 3.4 x 1038 7 significant digits+/- 1.7 x 10308 15 significant digitsMax Value12732,7672,147,483,647> 9 x 1018Primitive Data - Characters•A char variable stores a single character•Character literals are delimited by single quotes:'a' 'X' '7' '$' ',' '\n'•Example declarations:char topGrade = 'A';char terminator = ';', separator = ' ';•A character variable holds only one character. 2-17Primitive Data - Character SetsA character set is an ordered list of characters, with each character corresponding to a unique number or codeJava uses the Unicode character set It is an international character set, containing symbols and characters from many world languagesUses sixteen bits per character, allowing for 65,536 unique charactersA char variable in Java can store any character from the Unicode character set.2-18Primitive Data - Characters•The ASCII character set is older and smaller than Unicode, but is still quite popular•The ASCII character set is a subset of the Unicode character
View Full Document