Computer Science
Java Operators
Java operators are symbols used to perform operations on variables and values. They include arithmetic operators (+, -, *, /), comparison operators (==, !=, <, >), logical operators (&&, ||, !), and assignment operators (=, +=, -=). These operators are essential for manipulating data and controlling the flow of a Java program.
Written by Perlego with AI-assistance
Related key terms
1 of 5
6 Key excerpts on "Java Operators"
- eBook - PDF
- Scott Selikoff, Jeanne Boyarsky(Authors)
- 2022(Publication Date)
- Sybex(Publisher)
Operators OCP EXAM OBJECTIVES COVERED IN THIS CHAPTER: ✓ Handling date, time, text, numeric and boolean values ■ Use primitives and wrapper classes including Math API, parentheses, type promotion, and casting to evaluate arithmetic and boolean expressions Chapter 2 The previous chapter talked a lot about defining variables, but what can you do with a variable once it is created? This chapter introduces operators and shows how you can use them to combine existing variables and create new values. It shows you how to apply operators to various primitive data types, including introducing you to operators that can be applied to objects. Understanding Java Operators Before we get into the fun stuff, let’s cover a bit of terminology. A Java operator is a special symbol that can be applied to a set of variables, values, or literals—referred to as operands— and that returns a result. The term operand , which we use throughout this chapter, refers to the value or variable the operator is being applied to. Figure 2.1 shows the anatomy of a Java operation. The output of the operation is simply referred to as the result. Figure 2.1 actually con-tains a second operation, with the assignment operator ( = ) being used to store the result in variable c . We’re sure you have been using the addition (+) and subtraction ( - ) operators since you were a little kid. Java supports many other operators that you need to know for the exam. While many should be review for you, some (such as the compound assignment operators) may be new to you. Types of Operators Java supports three flavors of operators: unary, binary, and ternary. These types of operators can be applied to one, two, or three operands, respectively. For the exam, you need to know var c = a + b; Operands Operator Result assigned to c FIGURE 2.1 Java operation Understanding Java Operators 67 a specific subset of Java Operators, how to apply them, and the order in which they should be applied. - eBook - ePub
- Jeanne Boyarsky, Scott Selikoff(Authors)
- 2019(Publication Date)
- Sybex(Publisher)
Chapter 3 OperatorsOCP exam objectives covered in this chapter:-
Using Operators and Decision Constructs
- Use Java Operators including the use of parentheses to override operator precedence
-
Working With Java Primitive Data Types and String APIs
- Declare and initialize variables (including casting and promoting primitive data types)
In the previous chapter, we talked a lot about defining variables, but what can you do with a variable once it is created? This chapter introduces operators and shows how you can use them to combine existing values and create new values. We’ll show you how to apply operators to various primitive data types, including introducing you to operators that can be applied to objects.Understanding Java Operators
Before we get into the fun stuff, let’s cover a bit of terminology. A Javaoperatoris a special symbol that can be applied to a set of variables, values, or literals—referred to as operands—and that returns a result. The termoperand, which we’ll use throughout this chapter, refers to the value or variable the operator is being applied to. The output of the operation is simply referred to as the result. For example, in a + b , the operator is the addition operator (+) , and values a and b are the operands. If we then store the result in a variable c , such as c = a + b , then the variable c and the result of a + b become the new operands for our assignment operator (= ).We’re sure you have been using the addition (+) and subtraction (- ) operators since you were a little kid. Java supports many other operators that you need to know for the exam. While many should be review for you, some (such as the compound assignment operators) may be new to you.Types of Operators
In general, three flavors of operators are available in Java: unary, binary, and ternary. These types of operators can be applied to one, two, or three operands, respectively. For the exam, you’ll need to know a specific subset of Java Operators, how to apply them, and the order in which they should be applied. - eBook - ePub
Java
The Comprehensive Guide
- Christian Ullenboom(Author)
- 2022(Publication Date)
- SAP PRESS(Publisher)
2.4 Expressions, Operands, and Operators
Let’s start with some mathematical expressions and then illustrate how they are written in Java. A mathematical formula, such as the expression -27 * 9 , consists of operands and operators . For example, an operand is a variable, a literal, or the return of a method call. In the case of a variable, the value is read from the variable and the calculation is performed with it.The operators link the operands. Depending on the number of operands, the following types of operators are distinguished:-
An operator defined on exactly one operand is referred to as a unary operator (or monadic operator ). The minus (negative sign ) before an operand is a unary operator since it applies to exactly the following operand.
-
The usual operators (plus, minus, multiply, and divide) by are binary ( dyadic ) operators .
- A question mark operator is used for conditional expressions, which have 3 characters.
Operators allow the connection of individual expressions to new expressions. Some operators are familiar from school, such as addition, comparison, assignment, and others. C(++) programmers will recognize many old friends.2.4.1 Assignment Operator
In Java, the equal sign = is used for an assignment . [ 57 ] The assignment operator is a binary operator with the variable to be assigned on the left and an expression on the right.[eg] Example Let’s look at some expressions with assignments:int quantity = 12 , total ; total = quantity * 2 ;The multiplication calculates the product of 12 and 2 and stores the result in total . From all primitive variables that occur in the expression, the value is read and inserted into the expression. [ 58 ] This process is also called a value operation because the value of the variable is considered (and not its location or even its variable name).Only after the expression has been evaluated does the assignment operator copy the result into the variable. If runtime errors occur, for example, due to a division by zero, no write access to the variable is possible. - eBook - PDF
Elementary Synchronous Programming
in C++ and Java via algorithms
- Ali S. Janfada(Author)
- 2019(Publication Date)
- De Gruyter Oldenbourg(Publisher)
Java includes local, instance, and class variables. These types of variables are ex-amined in Section 3.3. 3.1.3 Operators Java programming language uses the same arithmetic, relational (comparative), log-ical, and assignment operators which C++ apply, and their behaviours are highly comparable. In a number of cases, Java Operators exert different results compared to their C++ equivalents. The division / operator generates an exception if we divide an integer by zero. For instance, 1.0/0.0 causes positive infinity ( Infinity on the screen) while − 1.0/0.0 leads to negative infinity (− Infinity on the screen). Addition- 52 | Fundamental concepts of programming in Java ally, 1.0/( − 1.0/0.0) , which is 1 over negative infinity and 0.0/0.0 , which is arith-metically undefined, cause in minus zero ( − 0.0 on the screen) and the not-a-number value ( NaN on the screen), respectively. As shown, Java defines positive and negative zeroes and infinities, as well as not-a-number values which indicate different habits as an operand in logical expressions. For example, the logical expression of 0.0 == − 0.0 is true whereas 0.0 > − 0.0 is false. Since NaN is unordered, all the comparison operators return false if either oper-and is NaN , except for != which always returns true if either operand is NaN . The results of some (mainly binary) calculations concerned with the above-men-tioned values are represented in Table 3.4. In this table, the results are what appear on the screen. The variable p is assumed a positive real number which can be replaced in the program calculations by, say, 1.0 . In addition, the variable inf is supposed to be the infinity. This variable may be replaced by 1.0/0.0 in the program calculations. - eBook - ePub
Introduction to Programming
Learn to program in Java with data structures, algorithms, and logic
- Nick Samoylov(Author)
- 2018(Publication Date)
- Packt Publishing(Publisher)
Java Language Elements and Types , we introduced input elements, those that are identified by the Java specification: whitespace, comment, and token. That is how the Java compiler parses the source code and makes sense of it. The list of tokens includes identifiers, keywords, separators, literals, and operators. That is how the Java compiler adds more meaning to the tokens it encounters.While discussing the input elements, we explained that they are used to build more complex elements of language. In this chapter, we will start with the operator token and show how an expression—a more complex Java element—is constructed with it.But, not all Java Operators are tokens. The instanceof and new operators are keywords, while the . operator (field access or method invocation), the :: method reference operator , and the ( type ) cast operator are separators.As we said in Chapter 2 , Java Language Basics , a statement in Java plays a role similar to a sentence in the English language, which expresses a complete thought. In a programming language, a statement is a complete line of code that performs some action .An expression, on the other hand, is a part of a statement that evaluates to a value. Every expression can be a statement (if the resulting value is ignored), while most statements do not include expressions.That is how the three core elements of Java—operator, expression, and statement— are related.Passage contains an image
Operators
Here is the list of all 44 operators in Java:
Unary means used with a single operand, while binary means it requires two operands. In the following subsections, we will define and demonstrate most of the operators, except the rarely used assignment operators &=, |=, ^=, <<=, >>=, and >>>=, and the bitwise operators.Operators Description+, -, *, /, % Arithmetic unary and binary operators ++, -- Increment and decrement unary operators ==, != Equality operators <, >, <=, >= Relational operators !, &, | Logical operators &&, ||, ?, : Conditional operators =, +=, -=, *=, /=, %= Assignment operators &=, |=, ^=, <<=, >>=, >>>= Assignment operators &, |, ~, ^, <<, >>, >>> Bitwise operators ->, :: Arrow and method reference operators new Instance creation operator . Field access/method invocation operator instanceof Type comparison operator ( target type ) Cast operator - eBook - PDF
- Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser(Authors)
- 2014(Publication Date)
- Wiley(Publisher)
These other kinds of operators are of the following form: variable op= expression where op is any binary operator. The above expression is generally equivalent to variable = variable op expression so that x *= 2 is equivalent to x = x * 2. However, if variable contains an expres- sion (for example, an array index), the expression is evaluated only once. Thus, the code fragment a[5] = 10; j = 5; a[j++] += 2; // not the same as a[j++] = a[j++] + 2 leaves a[5] with value 12 and j with value 6. 1.4. Java Expressions 27 Operator Precedence Operators in Java are given preferences, or precedence, that determine the order in which operations are performed when the absence of parentheses brings up eval- uation ambiguities. For example, we need a way of deciding if the expression, “5+2*3,” has value 21 or 11 (Java says it is 11). We show the precedence of the operators in Java (which, incidentally, is the same as in C and C++) in Table 1.3. Operator Precedence Type Symbols 1 array index [ ] method call () dot operator . 2 postfix ops exp++ exp−− prefix ops ++exp −−exp +exp −exp ˜exp !exp cast (type) exp 3 mult./div. * / % 4 add./subt. + − 5 shift << >> >>> 6 comparison < <= > >= instanceof 7 equality == != 8 bitwise-and & 9 bitwise-xor ˆ 10 bitwise-or | 11 and && 12 or || 13 conditional booleanExpression ? valueIfTrue : valueIfFalse 14 assignment = += −= *= /= %= <<= >>= >>>= &= ˆ= |= Table 1.3: The Java precedence rules. Operators in Java are evaluated according to the ordering above if parentheses are not used to determine the order of evaluation. Operators on the same line are evaluated in left-to-right order (except for assign- ment and prefix operations, which are evaluated in right-to-left order), subject to the conditional evaluation rule for boolean && and || operations. The operations are listed from highest to lowest precedence (we use exp to denote an atomic or parenthesized expression).
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.





