Follow
Publications: 0 | Followers: 0

Chapter #, Title - cs.ccsu.edu

Publish on Category: Birds 268

Escape Sequences
What if we wanted to print the quote character?The following line would confuse the compiler because it would interpret the second quote as the end of the stringSystem.out.println("I said "Hello" to you.");Anescape sequenceis a series of characters that represents a special characterAn escape sequence begins with a backslash character (\)System.out.println("I said \"Hello\" to you.");
Copyright © 2017 Pearson Education, Inc.
Escape Sequences
Some Java escape sequences:
Escape Sequence\b\t\n\r\"\'\\
Meaningbackspacetabnewlinecarriage returndouble quotesingle quotebackslash
Copyright © 2017 Pearson Education, Inc.
SeeRoses.java
Copyright © 2017 Pearson Education, Inc.
//********************************************************************// Roses.java Author: Lewis/Loftus//// Demonstrates the use of escape sequences.//********************************************************************public classRoses{//-----------------------------------------------------------------// Prints a poem (of sorts) on multiple lines.//-----------------------------------------------------------------public static voidmain(String[] args){System.out.println("Roses are red,\n\tViolets are blue,\n" +"Sugar is sweet,\n\tBut I have \"commitment issues\",\n\t" +"So I'd rather just be friends\n\tAt this point in our " +"relationship.");}}
Copyright © 2017 Pearson Education, Inc.
//********************************************************************//Roses.javaAuthor: Lewis/Loftus//// Demonstrates the use of escape sequences.//********************************************************************public classRoses{//-----------------------------------------------------------------// Prints a poem (of sorts) on multiple lines.//-----------------------------------------------------------------public static voidmain(String[]args){System.out.println("Roses are red,\n\tVioletsare blue,\n" +"Sugar is sweet,\n\tButI have \"commitment issues\",\n\t" +"So I'd rather just be friends\n\tAtthis point in our " +"relationship.");}}
OutputRoses are red,Violets are blue,Sugar is sweet,But I have "commitment issues",So I'd rather just be friendsAt this point in our relationship.
Variables
Avariableis a name for a location in memory that holds a valueAvariable declarationspecifies the variable's name and the type of information that it will hold
int total;int count, temp, result;
Multiple variables can be created in one declaration
data type
variable name
Copyright © 2017 Pearson Education, Inc.
Variable Initialization
A variable can be given an initial value in the declaration
int sum = 0;int base = 32, max = 149;
Copyright © 2017 Pearson Education, Inc.
When a variable is referenced in a program, its current value is usedSeePianoKeys.java
Copyright © 2017 Pearson Education, Inc.
//********************************************************************// PianoKeys.java Author: Lewis/Loftus//// Demonstrates the declaration, initialization, and use of an// integer variable.//********************************************************************public classPianoKeys{//-----------------------------------------------------------------// Prints the number of keys on a piano.//-----------------------------------------------------------------public static voidmain(String[] args){intkeys = 88;System.out.println("A piano has " + keys + " keys.");}}
Copyright © 2017 Pearson Education, Inc.
//********************************************************************// PianoKeys.java Author: Lewis/Loftus//// Demonstrates the declaration, initialization, and use of an// integer variable.//********************************************************************public classPianoKeys{//-----------------------------------------------------------------// Prints the number of keys on a piano.//-----------------------------------------------------------------public static voidmain(String[] args){intkeys = 88;System.out.println("A piano has " + keys + " keys.");}}
OutputA piano has 88 keys.
Assignment
Anassignment statementchanges the value of a variableThe assignment operator is the=sign
total = 55;
Copyright © 2017 Pearson Education, Inc.
The value that was intotalis overwrittenYou can only assign a value to a variable that is consistent with the variable's declared typeSeeGeometry.java
Copyright © 2017 Pearson Education, Inc.
//********************************************************************// Geometry.java Author: Lewis/Loftus//// Demonstrates the use of an assignment statement to change the// value stored in a variable.//********************************************************************public classGeometry{//-----------------------------------------------------------------// Prints the number of sides of several geometric shapes.//-----------------------------------------------------------------public static voidmain(String[] args){intsides = 7;// declaration with initializationSystem.out.println("A heptagon has " + sides + " sides.");sides = 10;// assignment statementSystem.out.println("A decagon has " + sides + " sides.");sides = 12;System.out.println("A dodecagon has " + sides + " sides.");}}
Copyright © 2017 Pearson Education, Inc.
//********************************************************************//Geometry.javaAuthor: Lewis/Loftus//// Demonstrates the use of an assignment statement to change the// value stored in a variable.//********************************************************************public classGeometry{//-----------------------------------------------------------------// Prints the number of sides of several geometric shapes.//-----------------------------------------------------------------public static voidmain(String[]args){intsides = 7;// declaration with initializationSystem.out.println("A heptagon has " + sides + " sides.");sides = 10;// assignment statementSystem.out.println("A decagon has " + sides + " sides.");sides = 12;System.out.println("A dodecagon has " + sides + " sides.");}}
OutputA heptagon has 7 sides.A decagon has 10 sides.a dodecagon has 12 sides.
Constants
Aconstantis an identifier that is similar to a variable except that it holds the same value during its entire existenceAs the name implies, it is constant, not variableThe compiler will issue an error if you try to change the value of a constantIn Java, we use thefinalmodifier to declare a constantfinal int MIN_HEIGHT = 69;
Copyright © 2017 Pearson Education, Inc.
Primitive Data
There are eight primitive data types in JavaFour of them represent integers:byte,short,int,longTwo of them represent floating point numbers:float,doubleOne of them represents characters:charAnd one of them represents boolean values:boolean
Copyright © 2017 Pearson Education, Inc.
Numeric Primitive Data
The difference between the numeric primitive types is their size and the values they can store:
Typebyteshortintlongfloatdouble
Storage8 bits16 bits32 bits64 bits32 bits64 bits
Min Value-128-32,768-2,147,483,648< -9 x 1018+/- 3.4 x 1038with 7 significant digits+/- 1.7 x 10308with 15 significant digits
Max Value12732,7672,147,483,647> 9 x 1018
Copyright © 2017 Pearson Education, Inc.
Characters
Acharvariable stores a single characterCharacter literals are delimited by single quotes:'a' 'X' '7' '$' ',' '\n'Example declarations:char topGrade = 'A';char terminator = ';', separator = ' ';Note the difference between a primitive character variable, which holds only one character, and aStringobject, which can hold multiple characters
Copyright © 2017 Pearson Education, Inc.
Character Sets
Acharacter setis an ordered list of characters, with each character corresponding to a unique numberAcharvariable in Java can store any character from theUnicode character setThe Unicode character set uses sixteen bits per character, allowing for 65,536 unique charactersIt is an international character set, containing symbols and characters from many world languages
Copyright © 2017 Pearson Education, Inc.
Boolean
Abooleanvalue represents a true or false conditionThe reserved wordstrueandfalseare the only valid values for a boolean typeboolean done = false;Abooleanvariable can also be used to represent any two states, such as a light bulb being on or off
Copyright © 2017 Pearson Education, Inc.
Expressions
Anexpressionis a combination of one or more operators and operandsArithmetic expressionscompute numeric results and make use of the arithmetic operators:
AdditionSubtractionMultiplicationDivisionRemainder
+-*/%
Copyright © 2017 Pearson Education, Inc.
If either or both operands are floating point values, then the result is a floating point value
Division and Remainder
If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded)
14 / 3equals48 / 12equals0
Copyright © 2017 Pearson Education, Inc.
The remainder operator (%) returns the remainder after dividing the first operand by the second
14 % 3equals28 % 12equals8
Quick Check
Copyright © 2017 Pearson Education, Inc.
What are the results of the following expressions?
12 / 212.0 / 2.010 / 410 / 4.04 / 104.0 / 1012 % 310 % 33 % 10
Quick Check
Copyright © 2017 Pearson Education, Inc.
What are the results of the following expressions?
12 / 212.0 / 2.010 / 410 / 4.04 / 104.0 / 1012 % 310 % 33 % 10
= 6= 6.0= 2= 2.5= 0= 0.4= 0= 1= 0
Operator Precedence
Operators can be combined into larger expressionsresult = total + count / max - offset;Operators have a well-defined precedence which determines the order in which they are evaluatedMultiplication, division, and remainder are evaluated before addition, subtraction, and string concatenationArithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation order
Copyright © 2017 Pearson Education, Inc.
Assignment Revisited
The right and left hand sides of an assignment statement can contain the same variable
First, one is added to theoriginal value ofcount
Then the result is stored back intocount(overwriting the original value)
count = count + 1;
Copyright © 2017 Pearson Education, Inc.

0

Embed

Share

Upload

Make amazing presentation for free
Chapter #, Title - cs.ccsu.edu