Java Programming, Lecture Notes # 22, Revised 01/03/99.
Preface
Introduction
Operators
Arithmetic Operators
Relational and Conditional (Logical) Operators
Bitwise Operators
Assignment Operators
Review
Students in Prof. Baldwin's Introductory Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson (except that they are not responsible for detailed information that is specific to C++).
The detailed material on C++ is provided as supplementary material for the benefit of those persons who are already familiar with C++ and who are making the transition into Java.
The first step
The first step in learning to use a new programming language is usually to learn the foundation concepts such as variables, operators, types, expressions, flow-of-control, etc. This lesson concentrates on the operators used in Java.
In some cases, a comparison is made with similar operators used in C++ for the benefit of those students who know how to program in C++. Knowledge of C++ is not a prerequisite for success in this course.
Unary and binary operators
Both Java and C++ provide a set of operators that can be used to perform an action on one or two operands. An operator that operates on one operand is called a unary operator, and an operator that operates on two operands is called a binary operator.
Some operators can behave either as a unary or as a binary operator, the best known of which is probably the minus sign. As a binary operator, the minus sign causes its right operand to be subtracted from its left operand. As a unary operator, the minus sign causes the algebraic sign of the right operand to be changed.
Overloaded operators
In addition to the "normal" use of operators, C++ makes it possible to "overload" operators. This means that the programmer can redefine the behavior of an operator with respect to objects of a new type defined by that program. Unfortunately, Java does not support operator overloading. |
Operators from previous programs
The following statements illustrate the use of several operators in a previous Java program by highlighting them in boldface.
int ch1, ch2 = '0'; while( (ch1 = System.in.read() ) != '#') ch2 = ch1; System.out.println("The char before the # was " + (char)ch2); |
Depending on the quality of the printer used to print these notes, the highlighted operators may or may not be obvious. Therefore, they are also listed below for clarity.
= != + (char) |
The plus operator
Of particular interest in this list is the plus sign (+) and the cast operator (char). In Java, the plus sign can be used to perform arithmetic addition.
It can also be used to concatenate strings. When the plus sign is used in this manner, the operand on the right is automatically converted to a character string before being concatenated with the operand on the left.
The cast operator
The cast operator is used in this case to purposely convert the integer value contained in the int variable ch2 to a character type suitable for concatenating with the string on the left of the plus sign. Otherwise, Java would attempt to convert the value of the int variable to a series of digits representing the numeric value of the character since the character is stored in a variable of type int.
C++ operators from previous programs
The following illustrates some of the statements in a previous C++ program that contains operators. |
void simple1::classMain(){ //input and save bytes while( (ch1 = getchar()) != '#') ch2 = ch1; |
The first line contains the C++ scope resolution operator (::). The second line contains some assignment operators (=) and a not equal logical operator (!=). |
Common operators
There are many operators that appear in both Java programs and C++ programs, using the same symbol to perform the same operation.
There are some other operators, such as the C++ scope resolution operator and the C++ insertion and extraction operators, which do not overlap the two languages. |
Common symbol, different behavior
There are also some operators that use the same symbol in both languages, but don't perform exactly the same operation.
For example, the plus sign is not automatically overloaded to perform string concatenation in C++ (but you can overload it if you want to). |
Commonality with Pascal operators
As you might imagine, many Java operators are also used in Pascal with the same symbol to perform essentially the same operation.
The increment operator
An extremely important unary operator that is used in both Java and C++ (but does not exist in Pascal) is the increment operator (++).
This operator is used in both prefix and postfix notation. The increment operator causes the value of its operand to be increased by one.
Incrementing pointer variables in C++
The increment operator behaves differently when applied to pointer variables in C++. Java does not support pointer variables. That is not an issue here. |
Prefix and postfix increment operators
With the prefix version, the operand appears to the right of the operator ( ++X), while with the postfix version, the operand appears to the left of the operator (X++).
What's the difference in prefix and postfix?
The difference in prefix and postfix has to do with the point in time that the increment actually occurs if the operator and its operand appear as part of a larger overall expression.
Prefix behavior
With the prefix version, the variable is incremented before it is used to evaluate the larger overall expression.
Postfix behavior
With the postfix version, the variable is used to evaluate the larger overall expression and then it is incremented.
Illustration of prefix and postfix behavior
The use of both the prefix and postfix versions of the increment operator is illustrated in the following Java program.
/*File incr01.java Copyright 1997, n Illustrates the use of the prefix and the postfix increment operator. The output from the program follows: a = 5 b = 5 a + b++ = 10 b = 6 c = 5 d = 5 c + ++d = 11 d = 6 *********************************************************/ class incr01 { //define the controlling class public static void main(String[] args){ //main method int a = 5, b = 5, c = 5, d = 5; System.out.println("a = " + a ); System.out.println("b = " + b ); System.out.println("a + b++ = " + (a + b++) ); System.out.println("b = " + b ); System.out.println(); System.out.println("c = " + c ); System.out.println("d = " + d ); System.out.println("c + ++d = " + (c + ++d) ); System.out.println("d = " + d ); }//end main }//End incr01 class. |
Comparison of increment operator in Java and C++
There is essentially no difference in the behavior of the Java increment operator and the C++ increment operator, so a C++ program is not shown for comparison. |
Binary operators and infix notation
Binary operators use infix notation, which means that the operator appears between its operands.
General behavior of an operator
As a result of performing the specified action, an operator can be said to return a value (or evaluate to a value) of a given type. The type of value returned depends on the operator and the type of the operands.
To evaluate to a value means that after the action is performed, the operator and its operands are effectively replaced in the expression by the value that is returned. |
Overloading operators in C++
The act of returning a value becomes much more obvious in C++ where it is possible to overload operators to specify their behavior relative to specific types of operands. (Java does not support operator overloading.) In this case, the programmer actually writes a method or function with a special name and argument list. When the program encounters the operator and the specified types of operands, the function is called. The overloaded operator function actually returns a value just like any other function or method. |
Operator categories
Some authors divide Java's operators into the following categories:
Both Java and C++ support various arithmetic operators on all floating point and integer numbers.
C++ also supports arithmetic operators on objects of newly defined types through operator overloading, but this capability is not supported by Java. |
The binary arithmetic operators
The following table lists the binary arithmetic operators supported by both Java and C++.
Operator Description + Adds its operands - Subtracts the right operand from the left operand * Multiplies the operands / Divides the left operand by the right operand % Remainder of dividing the left operand by the right operand |
String concatenation
Also as mentioned earlier, the plus operator (+) is also used to concatenate strings as in the following code fragment:
"MyVariable has a value of " + MyVariable + " in this program." |
Coercion
Note that this operation also coerces the value of MyVariable to a string representation for use in the expression only. The value stored in the variable is not modified in any lasting way.
String concatenation in C++
String concatenation using the plus operator is not supported by C++. Rather, a function named strcat() can be used to concatenate two strings. The use of this function can be dangerous because there is no protection against overwriting the bounds of the array that is used as the destination of the concatenated string. |
Unary operators
Both Java and C++ support the following unary arithmetic operators.
Operator Description + Indicates a positive value - Negates, or changes algebraic sign ++ Adds one to the operand, both prefix and postfix -- Subtracts one from operand, both prefix and postfix |
The result of the increment and decrement operators being either prefix or postfix was discussed earlier.
Binary Relational operators
Java and C++ support the same set of binary relational operators, as indicated in the following table. Relational operators in Java return either true or false as a boolean type.
Relational operators in C++ return an integer where the integer value of zero may be interpreted as false and any non-zero value may be interpreted as true. |
Except for the symbols used for the last operator in the table, these operators are also used in Pascal to perform similar operations.
Operator Returns true if > Left operand is greater than right operand >= Left operand is greater than or equal to right operand < Left operand is less than right operand <= Left operand is less than or equal to right operand == Left operand is equal to right operand != Left operand is not equal to right operand |
Conditional expressions
Relational operators are frequently used in the conditional expressions of control statement such as
if(LeftVariable <= RightVariable). . . |
Illustration of relational operators
The following two programs illustrate the result of applying relational operators in Java and C++. In each case, the output is shown in the comments at the beginning of the program. The Java program follows. Note that the Java program displays true and false as a result of applying the relational operators.
/*File relat01.java Copyright 1997, R.G.Baldwin Illustrates relational operators. Output is The relational 6<5 is false The relational 6>5 is true *********************************************************/ class relat01 { //define the controlling class public static void main(String[] args){ //main method System.out.println("The relational 6<5 is " +(6<5)); System.out.println("The relational 6>5 is " +(6>5)); }//end main }//End relat01 class. |
.
The next program is a C++ program that mimics the above Java program. Note that in this case, instead of displaying true and false, the program displays 1 and 0 instead. While you can count on false always being represented by 0 in C++, you cannot count on true always being represented by 1. The only stipulation is that true must be represented by a non-zero value. |
/*File relat01.cpp Copyright 1997, R.G.Baldwin Illustrates the use of relational operators. The output from this program is: The relational 6<5 is 0 The relational 6>5 is 1 *******************************************************/ #include<iostream.h> class relat01 { public: static void classMain(){ cout << "The relational 6<5 is " << (6<5) << endl; cout << "The relational 6>5 is " << (6>5) << endl; }//end classMain };//End relat01 class definition. //=====================================================// void main() { //call the class method named classMain relat01::classMain(); }//end main |
Conditional operators
The relational operators are often combined with another set of operators (referred to as conditional operators in Java and referred to as logical operators in C++) to construct more complex expressions.
Both Java and C++ support three such operators as shown in the following table.
Operator Typical Use Returns true if && Left && Right Left and Right are both true || Left || Right Either Left or Right is true ! ! Right Right is false |
.
With the exception of the manner in which true and false is maintained, these operators behave the same in Java and C++. However, because of the difference in the representation of true and false, the variety of ways that the operators are used in C++ may be much greater than in Java. |
Type of operands of conditional operators
In Java, the operands must be boolean types, or must have been created by evaluation of an expression that returns a boolean type.
In C++, the operands can be integer types, can be convertible to integer types, or can be created by any expression that returns an integer type. For example, in C++, a zero value is typically placed in a pointer variable to indicate that the pointer doesn't point to anything in particular. This is often referred to as a null pointer. It is then typical to construct expressions such as the following. |
if(PointerVariable) . . .; else . . .; |
Left to right evaluation
An important characteristic of the behavior of the && and || operators in both Java and C++ is that the expressions are evaluated from left to right, and the evaluation of the expression is terminated as soon as the result of evaluating the expression can be determined.
For example, in the following expression, if the variable a is less than the variable b , there is no need to evaluate the right operand of the || to determine the value of the entire expression. Thus, evaluation will terminate as soon as the answer can be determined.
(a < b) || (c < d) |
Don't confuse & with &&
As discussed in the next section, the & operator in Java is a bitwise and while the | operator is a bitwise or.
One author states that in Java, the & operator can be used as a synonym for && and the | can be used as a synonym for || if both of the operands are boolean.
Note however that according to a different author, in this case, the evaluation of the expression is not terminated until all operands have been evaluated, thus eliminating the possible advantage of the left-to-right evaluation.
A common programming error
A common programming error in C++ is to use the & operator when the logic requires the use of the && operator or to use the | operator when the logic requires the use of the || operator. This can be a very difficult error to find because the result will frequently be the same, strictly by accident. |
Java and C++ share a set of operators that perform actions on their operands one bit at a time.
One of the operations supported by Java (>>>) is not supported by C++ and the behavior of some of the operations is not always exactly the same between Java and C++. |
These Java operators are summarized in the following table.
Operator Typical Use Operation >> OpLeft >> Dist Shift bits of OpLeft right by Dist bits (signed) << OpLeft << Dist Shift bits of OpLeft left by Dist bits >>> OpLeft >>> Dist Shift bits of OpLeft right by Dist bits (unsigned) & OpLeft & OpRight Bitwise and of the two operands | OpLeft | OpRight Bitwise inclusive or of the two operands ^ OpLeft ^ OpRight Bitwise exclusive or (xor) of the two operands ~ ~ OpRight Bitwise complement of the right operand (unary) |
Populating vacated bits for shift operations
In Java, the signed right shift operation populates the vacated bits with the sign bit, while the left shift and the unsigned right shift populate the vacated bits with zeros.
Signed shifts in C++
C++ does not support the >>> operator or the concept of a signed right shift. In C++, if the type of the value being shifted to the right is an unsigned type, the vacated bits are populated by zeros. According to one author, if the type is a signed type, the vacated bits in a right-shift operation may be populated by either zeros or ones on a platform-dependent basis. In a left-shift operation, vacated bits are populated by zeros. |
What happens to bits shifted off the end?
In all cases, for both Java and C++, bits shifted off the end are lost.
The rule for bitwise
andThe bitwise and operation operates according to the rule that the bitwise and of two 1 bits is a 1 bit. Any other combination results in a 0 bit.
Bitwise
inclusive orFor the inclusive or, if either bit is a 1, the result is a 1. Otherwise, the result is a 0.
Bitwise
exclusive orFor the exclusive or, if either but not both bits is a 1, the result is a 1. Otherwise, the result is a 0. Another way to state this is if the bits are different, the result is a 1. If the two bits are the same, the result is a 0.
The complement operator
Finally, the complement operator changes each 1 to a 0 and changes each 0 to a 1.
Simple assignment operator
Just as in C++, the (=) is a value assigning binary operator in Java. The value stored in memory and represented by the right operand is copied into the memory represented by the left operand. Unlike C++, you cannot overload the assignment operator (or any other operator for that matter) in Java.
Using the assignment operator with reference variables
You need to be careful and think about what you are doing when you use the assignment operator with reference variables in Java. If you assign one reference variable to another, you simply end up with two reference variables that refer to the same object. You do not end up with two different objects.
The clone() method
If what you need is another copy of the object, you can use the clone() method to accomplish that.
Shortcut assignment operators
Also similar to C++, Java supports the following list of shortcut assignment operators. These operators allow you to perform an assignment and another operation with a single operator.
+= -= *= /= %= &= |= ^= <<= >>= >>>= |
For example, the two statements that follow perform the same operation.
x += y; x = x + y; |
The behavior of all the shortcut assignment operators follows this same pattern.
Note that C++ does not support the >>>= operator, because it doesn't support the >>> operator. |
Q - An operator performs an action on what? Provide the name.
A - An operator performs an action on one or two operands.
Q - What do we call an operator that operates on only one operand?
A - An operator that operates on only one operand is called a unary operator.
Q - What do we call an operator that operates on two operands?
A - An operator that operates on two operands is called a binary operator.
Q - Is the minus sign a unary or a binary operator, or both? Explain your answer.
A - Both. As a binary operator, the minus sign causes its right operand to be subtracted from its left operand. As a unary operator, the minus sign causes the algebraic sign of the right operand to be changed.
Q - Describe operator overloading.
A - For those languages that support it (such as C++) operator overloading means that the programmer can redefine the behavior of an operator with respect to objects of a new type defined by that program.
Q- Java programmers may overload operators: True or False?
A - False: Unfortunately, Java does not support operator overloading.
Q - Show the symbols used for the following operators in Java: assignment, not equal, addition, cast.
A - The above listed operators in order are:
= != + (char)
where the cast operator is being used to cast to the type char.
Q - Is any operator automatically overloaded in Java? If so, identify it and describe its overloaded behavior.
A - The plus sign (+) is automatically overloaded in Java. The plus sign can be used to perform arithmetic addition. It can also be used to concatenate strings. However, the plus sign does more than concatenate strings. It also performs a conversion to String type. When the plus sign is used to concatenate strings, the operand on the right is automatically converted to a character string before being concatenated with the operand on the left. This assumes that the compiler knows enough about the operand on the right to be able to successfully perform the conversion. It has that knowledge for all of the primitive types and most or all of the built-in reference types.
Q - What is the purpose of the cast operator?
A - The cast operator is used to purposely convert from one type to another.
Q - The increment operator is a binary operator: True or False?
A - False: The increment operator is a unary operator.
Q - Show the symbol for the increment operator.
A - The symbol for the increment operator is two plus signs with nothing between them (++).
Q - Describe the appearance and the behavior of the increment operator with both prefix and postfix notation. Show example, possibly incomplete, code fragments illustrating both notational forms.
A - The increment operator may be used with both prefix and postfix notation. Basically, the increment operator causes the value of the variable to which it is applied to be increased by one.
With prefix notation, the operand appears to the right of the operator ( ++X), while with postfix notation, the operand appears to the left of the operator (X++).
The difference in behavior has to do with the point in time that the increment actually occurs if the operator and its operand appear as part of a larger overall expression. With the prefix version, the variable is incremented before it is used to evaluate the larger overall expression. With the postfix version, the variable is used to evaluate the larger overall expression and then it is incremented.
Q - Show the output that would be produced by the following Java application.
class incr01 { //define the controlling class public static void main(String[] args){ //define main int x = 5, X = 5, y = 5, Y = 5; System.out.println("x = " + x ); System.out.println("X = " + X ); System.out.println("x + X++ = " + (x + X++) ); System.out.println("X = " + X ); System.out.println(); System.out.println("y = " + y ); System.out.println("Y = " + Y ); System.out.println("y + ++Y = " + (y + ++Y) ); System.out.println("Y = " + Y ); }//end main }//End incr01 class. Note no semicolon required //End Java application |
A - The output from this Java application follows:
Q - Binary operators use outfix notation: True or False? If your answer is False, explain why.
A - False: Binary operators use infix notation, which means that the operator appears between its operands.
Q - In practice, what does it mean to say that an operator that has performed an action returns a value (or evaluates to a value) of a given type?
A - As a result of performing the specified action, an operator can be said to return a value (or evaluate to a value) of a given type. The type depends on the operator and the type of the operands. To evaluate to a value means that after the action is performed, the operator and its operands are effectively replaced in the expression by the value that is returned.
Q - What are the four categories of operators described in Baldwin's Java tutorial on operators? Do you agree with this categorization? If not, explain why not.
A - Some authors divide Java's operators into the following categories:
Q - Show and describe at least five of the binary arithmetic operators supported by Java (Clarification: binary operators does not mean bitwise operators).
A - Java support various arithmetic operators on floating point and integer numbers. The following table lists five of the binary arithmetic operators supported by Java.
Operator Description + Adds its operands - Subtracts the right operand from the left operand * Multiplies the operands / Divides the left operand by the right operand % Remainder of dividing the left operand by the right operand |
Q - In addition to arithmetic addition, what is another use for the plus operator (+)? Show an example code fragment to illustrate your answer. The code fragment need not be a complete statement.
A - The plus operator (+) is also used to concatenate strings as in the following code fragment:
"MyVariable has a value of " + MyVariable + " in this program." |
Q - When the plus operator (+) is used as a concatenation operator, what is the nature of its behavior if its right operand is not of type String? If the right operand is a variable that is not of type String, what is the impact of this behavior on that variable.
A - In this case, the operator also coerces the value of the right operand to a string representation for use in the expression only. If the right operand is a variable, the value stored in the variable is not modified in any way.
Q - Show and describe four unary arithmetic operators supported by Java.
A - Java supports the following four unary arithmetic operators.
Operator Description + Indicates a positive value - Negates, or changes algebraic sign ++ Adds one to the operand, both prefix and postfix -- Subtracts one from operand, both prefix and postfix |
Q - What is the type returned by relational operators in Java?
A - Relational operators return the boolean type in Java.
Q - Show and describe six different relational operators supported by Java.
A - Java supports the following set of relational operators:
Operator Returns true if > Left operand is greater than right operand >= Left operand is greater than or equal to right operand < Left operand is less than right operand <= Left operand is less than or equal to right operand == Left operand is equal to right operand != Left operand is not equal to right operand |
Q - Show the output that would be produced by the following Java application:
class relat01 { //define the controlling class public static void main(String[] args){ //define main System.out.println("The relational 6<5 is " + (6<5 )); System.out.println("The relational 6>5 is " + (6>5 )); }//end main }//End relat01 class. Note no semicolon required //End Java application |
A - This program produces the following output:
The relational 6<5 is false
Q - Show and describe three operators (frequently referred to as conditional operators in Java and logical operators in C++) which are often combined with relational operators to construct more complex expressions (often called conditional expressions). Hint: The && operator returns true if the left and right operands are both true. What are the other two and how do they behave?
A - The following three logical or conditional operators are supported by Java.
Operator Typical Use Returns true if && Left && Right Left and Right are both true || Left || Right Either Left or Right is true ! ! Right Right is false |
Q - Describe the special behavior of the || operator in the following expression for the case where the value of the variable a is less than the value of the variable b.
(a < b) || (c < d)
A - An important characteristic of the behavior of the && and || operators in Java is that the expressions are evaluated from left to right, and the evaluation of the expression is terminated as soon as the result of evaluating the expression can be determined. For example, in the above expression, if the variable a is less than the variable b , there is no need to evaluate the right operand of the || to determine the value of the entire expression. Thus, evaluation will terminate as soon as it is determined that a is less than b.
Q - Show the symbols used for the bitwise and operator and the or operator.
A - The & operator in Java is a bitwise and while the | operator is a bitwise or.
Q - The logical and operator is represented in Java by the
&& symbol. What is the representation of the bitwise and operator in Java?A - The bitwise and operator is represented by the & symbol in Java.
Q - Show and describe five operators in Java that perform actions on the operands one bit at a time (bitwise operators).
A - The following table shows the seven bitwise operators supported by Java.
Operator Typical Use Operation >> OpLeft >> Dist Shift bits of OpLeft right by Dist bits (signed) >< OpLeft << Dist Shift bits of OpLeft left by Dist bits >>> OpLeft >>> Dist Shift bits of OpLeft right by Dist bits (unsigned) & OpLeft & OpRight Bitwise and of the two operands | OpLeft | OpRight Bitwise inclusive or of the two operands ^ OpLeft ^ OpRight Bitwise exclusive or (xor) of the two operands ~ ~ OpRight Bitwise complement of the right operand (unary) |
Q - In Java, the signed right shift operation populates the vacated bits with the zeros, while the left shift and the unsigned right shift populate the vacated bits with the sign bit: True or False. If your answer is False, explain why.
A - False: In Java, the signed right shift operation populates the vacated bits with the sign bit, while the left shift and the unsigned right shift populate the vacated bits with zeros.
Q - In a signed right-shift operation in Java, the bits shifted off the right end are lost: True or False. If your answer is False, explain why.
A - True: For both Java and C++, bits shifted off the right end are lost.
Q - Using the symbols 1 and 0 construct a table showing the four possible combinations of 1 and 0. Using a 1 or a 0, show the result of the bitwise and operation on these four combinations of 1 and 0.
A - The answer is:
1 and 1 produces 1
Q - Using the symbols 1 and 0 construct a truth table showing the four possible combinations of 1 and 0. Using a 1 or a 0, show the result of the bitwise inclusive or operation on these four combinations on these four combinations of 1 and 0.
A - The answer for the inclusive or is:
1 or 1 produces 1
Q - Using the symbols 1 and 0 construct a truth table showing the four possible combinations of 1 and 0. Using a 1 or a 0, show the result of the bitwise exclusive or operation on these four combinations on these four combinations of 1 and 0.
A - The answer for the exclusive or is:
1 xor 1 produces 0
Q - For the exclusive or, if the two bits are different, the result is a 1. If the two bits are the same, the result is a 0. True or False? If your answer is False, explain why.
A - True.
Q - Is the assignment operator a unary operator or a binary operator. Select one or the other.
A - The assignment operator is a binary operator.
Q - In Java, when using the assignment operator, the value stored in memory and represented by the right operand is copied into the memory represented by the left operand: True or False? If your answer is False, explain why.
A - True.
Q - Show two of the shortcut assignment operators and explain how they behave by comparing them with the regular (non-shortcut) versions. Hint: The (
^=) operator is a shortcut assignment operator.A - Java supports the following list of shortcut assignment operators. These operators allow you to perform an assignment and another operation with a single operator.
+= -= *= /= %= &= |= ^= <<= >>= >>>=
For example, the two statements that follow perform the same operation.
x += y;
The behavior of all the shortcut assignment operators follows this same pattern.
Q - Write a Java application that meets the following specifications.
/*File SampProg07.java from lesson 22 Copyright 1997, R.G.Baldwin Without reviewing the following solution, write a Java application that clearly illustrates the difference between the prefix and the postfix versions of the increment operator. Provide a termination message that displays your name. *********************************************************/ class SampProg07{ static public void main(String[] args){ int x = 3; int y = 3; int z = 10; System.out.println("Prefix version gives " + (z + ++x)); System.out.println("Postfix version gives " + (z + y++)); System.out.println("Terminating, Dick Baldwin"); }//end main }//end class SampProg07 |
Q - Write a Java application that meets the following specifications.
/*File SampProg08.java from lesson 22 Copyright 1997, R.G.Baldwin Without reviewing the following solution, write a Java application that illustrates the use of the following relational operators: < > <= >= == != Provide appropriate text in the output. Also provide a termination message with your name. *********************************************************/ class SampProg08 { //define the controlling class public static void main(String[] args){ //define main System.out.println("The relational 6<5 is " + (6<5 ) ); System.out.println("The relational 6>5 is " + (6>5 ) ); System.out.println("The relational 5>=5 is " + (5>=5 ) ); System.out.println("The relational 5<=5 is " + (5<=5 ) ); System.out.println("The relational 6==5 is " + (6==5 ) ); System.out.println("The relational 6!=5 is " + (6!=5 ) ); System.out.println("Terminating, Dick Baldwin"); }//end main }//End SampProg08 class. Note no semicolon required |
Q - Write a Java application that meets the following specifications.
/*File SampProg09.java from lesson 22 Copyright 1997, R.G.Baldwin Without reviewing the following solution, write a Java application that illustrates the use of the following logical or conditional operators: && || ! Provide appropriate text in the output. Also provide a termination message with your name. *********************************************************/ class SampProg09 { //define the controlling class public static void main(String[] args){ //define main System.out.println("true and true is " + (true && true) ); System.out.println("true and false is " + (true && false) ); System.out.println("true or true is " + (true || true) ); System.out.println("true or false is " + (true || false) ); System.out.println("false or false is " + (false || false) ); System.out.println("not true is " + (! true) ); System.out.println("not false is " + (! false) ); System.out.println("Terminating, Dick Baldwin"); }//end main }//End SampProg09 class. Note no semicolon required |
-end-