the system.exit() method
DESCRIPTION
The System.exit() Method. The System.exit method requires an integer argument. System.exit( 0 ); This argument is an exit code that is passed back to the operating system. This code is usually ignored, however, it can be used outside the program: - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/1.jpg)
The System.exit() Method
• The System.exit method requires an integer argument.System.exit(0);
• This argument is an exit code that is passed back to the operating system.
• This code is usually ignored, however, it can be used outside the program:– to indicate whether the program ended successfully or as
the result of a failure.– The value 0 traditionally indicates that the program ended
successfully.• We will learn about handling Exceptions so this will become
less important except perhaps in debugging or when you want your program to end as part of the exception handling.
– From Gaddis – Chapter 2 – slide 9
![Page 2: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/2.jpg)
The Parse Methods• Each of the numeric wrapper classes, Chapter 8,
has a static method that converts a string to a number.– The Integer class has a method that converts a
string to an int,– The Double class has a method that converts a
string to a double, and– etc.
• These methods are known as parse methods because their names begin with the word “parse.”
• From Gaddis – Chapter 2 – slide 11
![Page 3: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/3.jpg)
The Parse Methodsbyte bVar = Byte.parseByte("1"); // Store 1 in bVar.int iVar = Integer.parseInt("2599"); // Store 2599 in iVar.short sVar = Short.parseShort("10"); // Store 10 in sVar.long lVar = Long.parseLong("15908"); // Store 15908 in lVar.float fVar = Float.parseFloat("12.3"); // Store 12.3 in fVar.double dVar = Double.parseDouble("7945.6"); // Store 7945.6 in dVar. From Gaddis – Chapter 2 – slide 12
![Page 4: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/4.jpg)
Review of the following Topics
– The if Statement– The if-else Statement– The if-else-if Statement– Nested if Statements– Logical Operators– Comparing String Objects– More about Variable Declaration and
Scope– The switch Statement– From Gaddis – Chapter 3 – slides 4 and 5
![Page 5: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/5.jpg)
The if Statement• The if statement is one of a group of statements known
as selection statements.• The if statement allows the programmer to make
decisions whether a section of code executes or not. (i.e. does the program select these statements or not?)
• The if statement uses a boolean expression OR a boolean variable as an argument to decide if the next statement or block of statements executes.if (boolean expression is true){ execute what’s in this block }if (boolean_variable){ execute what’s in this block} From Gaddis – Chapter 3 – slides 6 modified
![Page 6: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/6.jpg)
Relational OperatorsRelational Operator
Meaning
> is greater than< is less than>= is greater than or equal to<= is less than or equal to== is equal to!= is not equal to
• Shown above are the 6 relational operators with their meanings
• They are frequently used as boolean expressions in if statements.
![Page 7: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/7.jpg)
*** Important ***• The result of the evaluation of a boolean
expression is a boolean value.• if (7 > 5) { System.out.println ( “7 is bigger than 5);}
• A boolean variable also has a boolean value.• boolean done;• done = true;• if (done) { System.out.println (“ done”); }
• In an if statement a boolean variable is either true or false so we never compare it to true or false.
![Page 8: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/8.jpg)
If Statement Programming Style• What is to be done when an if statement is true or false
follows the condition. Although it is legal, we will NEVER put it on the same line.
if(average > 95) System.out.println(“That’s a great score!”);
• Multiple statements are grouped into a block by using curly braces {} to enclose them.if(expression) {
statement1;statement2;statement3;
}• Remember that if the curly braces are not used, then only the
next statement after the if condition will be executed conditionally.
![Page 9: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/9.jpg)
Use of Flags• A flag is a boolean variable that
monitors some condition in a program.• When a condition is true, the flag is set
to a true value.• The flag can be set to indicate a
situation if(average > 95) highScore = true;
• The state of the flag can be tested:if(highScore)
System.out.println(“That’s a high score!);
![Page 10: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/10.jpg)
Comparing Characters• Characters can be tested using the relational
operators.• Characters are stored in the computer using
the unicode character format.• Each unicode character is stored using sixteen
(16) bits.• Characters are ordinal, meaning they have an
order in the unicode character set.• Since characters are ordinal, they can be
compared to each other.
![Page 11: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/11.jpg)
Digression• Discussion of meaning of ordinal• Not only ordered but we know what the
next element is.– for integers, given x, the next value is x+1
(integers are ordinal numbers)– for floating point numbers, given x.y, the
next value is not known, could be x.y1 or x.y01, or x.y001 or … (floating point numbers are not ordinals)
![Page 12: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/12.jpg)
if-else Statements• The if-else statement adds the ability to conditionally execute
code based if the expression of the if statement is false.• if-else-if statements can become very complex.
if it is very cold, wear a heavy coat,else, if it is chilly, wear a light jacket,else, if it is windy wear a windbreaker,else, if it is hot, wear no jacket.
• Care must be used since else statements match up with the immediately preceding unmatched if statement.
![Page 13: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/13.jpg)
Digression
• Discussion of – Sign magnitude representation– Ones complement representation– Twos complement representation
![Page 14: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/14.jpg)
Nassi-Shneiderman Diagrams
• http://en.wikipedia.org/wiki/Nassi-Shneiderman_diagram
• http://www.smartdraw.com/tutorials/software/nassi/tutorial_01.htm
• http://www.smartdraw.com/examples/view/index.aspx?catID=.Examples.SmartDraw.Software_Design.Nassi-Shneiderman_Diagrams
![Page 15: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/15.jpg)
if-else-if Flowchart
![Page 16: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/16.jpg)
Nested if Statements• If an if statement appears inside of
another if statement (single or block) it is called a nested if statement.
• The nested if is only executed if the if statement it is in results in a true condition.
• Nested if statements can get very complex, very quickly.
![Page 17: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/17.jpg)
Logical Operators• Java provides two binary logical
operators (&& and ||) that are used to combine boolean expressions.
• Java also provides one unary (!) logical operator to reverse the truth of a boolean expression.
![Page 18: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/18.jpg)
Logical OperatorsOperato
rMeanin
gEffect
&& ANDConnects two boolean expressions into one. Both expressions must be true for the overall expression to be true.
|| OR
Connects two boolean expressions into one. One or both expressions must be true for the overall expression to be true. It is only necessary for one to be true, and it does not matter which one.
! NOT
The ! operator reverses the truth of a boolean expression. If it is applied to an expression that is true, the operator returns false. If it is applied to an expression that is false, the operator returns true.
![Page 19: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/19.jpg)
The && Operator• The logical AND operator (&&) takes two
operands that must both be boolean expressions.
• The resulting combined expression is true iff (if and only if) both operands are true.
Expression 1
Expression 2
Expression1 && Expression2
true false falsefalse true falsefalse false falsetrue true true
![Page 20: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/20.jpg)
The || Operator• The logical OR operator (||) takes two operands
that must both be boolean expressions.• The resulting combined expression is false iff
(if and only if) both operands are false.
Expression 1
Expression 2
Expression1 || Expression2
true false truefalse true truefalse false falsetrue true true
![Page 21: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/21.jpg)
The ! Operator• The ! operator performs a logical NOT
operation.• If an expression is true, !expression will
be false.if (!(temperature > 100))
System.out.println(“Below the maximum temperature.");
Expression 1 !Expression1
true falsefalse true
![Page 22: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/22.jpg)
DeMorgan’s Law• The negation of
– (A and B) is (not A or not B)– (A or B) is (not A and not B)
![Page 23: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/23.jpg)
Short Circuiting• Logical AND and logical OR
operations perform short-circuit evaluation of expressions.
• Logical AND will evaluate to false as soon as it sees that one of its operands is a false expression.
• Logical OR will evaluate to true as soon as it sees that one of its operands is a true expression.
![Page 24: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/24.jpg)
Order of Precedence• The ! operator has a higher order
of precedence than the && and || operators.
• The && and || operators have a lower precedence than relational operators like < and >.
• Parenthesis can be used to force the precedence to be changed.
![Page 25: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/25.jpg)
Order of PrecedenceOrder of
Precedence
Operators Description
1 (unary negation) ! Unary negation, logical NOT
2 * / % Multiplication, Division, Modulus3 + - Addition, Subtraction
4 < > <= >=Less-than, Greater-than, Less-than or equal to, Greater-than or equal to
5 == != Is equal to, Is not equal to6 && Logical AND7 || Logical NOT
8 = += -=*= /= %=
Assignment and combined assignment operators.
![Page 26: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/26.jpg)
Comparing String Objects
• In most cases, you cannot use the relational operators to compare two String objects.
• Reference variables contain the address of the object they represent.
• Unless the references point to the same object, the relational operators will not return true.
![Page 27: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/27.jpg)
Ignoring Case in String Comparisons
• In the String class the equals and compareTo methods are case sensitive.
• In order to compare two String objects that might have different case, use: …
![Page 28: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/28.jpg)
Variable Scope• In Java, a local variable does not have to
be declared at the beginning of the method.
• The scope of a local variable begins at the point it is declared and terminates at the end of the method.
• When a program enters a section of code where a variable has scope, that variable has come into scope, which means the variable is visible to the program.
![Page 29: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/29.jpg)
The switch Statement• The if-else statements allow the
programmer to make true / false branches.
• The switch statement allows the programmer to use an ordinal value to determine how a program will branch.
• The switch statement can evaluate an integer type or character type variable and make decisions based on the value.
![Page 30: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/30.jpg)
The switch Statement• The switch statement takes the
form:switch (SwitchExpression){ case CaseExpression: // place one or more statements here break; case CaseExpression: // place one or more statements here break; // case statements may be repeated //as many times as necessary default: // place one or more statements here}
![Page 31: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/31.jpg)
The switch Statement• The switch statement takes an ordinal
value (byte, short, int, long, char) as the SwitchExpression.switch (SwitchExpression){
…}
• The switch statement will evaluate the expression.
• If there is an associated case statement that matches that value, program execution will be transferred to that case statement.
![Page 32: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/32.jpg)
The switch Statement• Each case statement will have a
corresponding CaseExpression that must be unique.case CaseExpression: // place one or more statements here break;
• If the SwitchExpression matches the CaseExpression, the Java statements between the colon and the break statement will be executed.
![Page 33: The System.exit() Method](https://reader035.vdocuments.site/reader035/viewer/2022062305/56814a47550346895db76535/html5/thumbnails/33.jpg)
The switch Case• The break statement ends the case
statement.• The break statement is optional.• If a case does not contain a break, then
program execution continues into the next case.
• The default case is optional and will be executed if no CaseExpression matches the SwitchExpression.