If you have thoroughly studied data types, variables, and
constants in the ** Java** programming language; see the previous post

**, then you are ready to get acquainted with operators in**

*here***Java**. If you are familiar with the

**programming language, and despite the fact that**

*C#***Java**inherited operators from the

**programming language, you may get the impression that**

*C++***C#**has the same operators as

**Java**. The only difference is that

**Java**does not have operator

**overloading**nor does it support pointers.

**C#**does not use pointers, but it supports them in

**unsafe**mode, which

**Java**does not. Another difference is that

**Java**categorizes operators in several ways. The most common way is that

**Java**divides operators into four basic groups, with everything else classified as

**additional**operators.

**Arithmetic Operators**

**Bitwise Operators**

**Comparison Operators**

**Logical Operators**

**Additional Operators**

**Students compare basic operators in Java with operators in other programming languages**

**Java**programming language and when we start programming more complex

**Java**applications. For now, make sure you fully understand how to use operators in

**Java**. If you don’t know how to create the simplest calculator application in any programming language, then you definitely don’t know how to use arithmetic operators in programming. Therefore, it’s best to start with arithmetic operators.

**Arithmetic Operators: First Steps in Programming**

The arithmetic operators you use on your calculators and
in mathematical expressions are often used in programming as well; and when it
comes to the **Java** programming language, the operands of arithmetic operators
must be of numeric type or char type. You cannot apply them to boolean types.
These are the arithmetic operators:

+ addition- subtraction, unary minus

* multiplication

/ division

% modulo, remainder of division

++ increment

-- decrement

+= addition assignment

-= subtraction assignment

*= multiplication assignment

/= division assignment

%= remainder assignment

**Arithmetic operators in the Java programming language**

**digitron-type**program, but we will create a simple program that will illustrate the use of all the listed arithmetic operators. With this knowledge you can create a digitron type application yourself. So, create a new program and call it

**arithmeticoperators**. Enter the following code.

package com.tutorial;

public
class arithmeticoperators {

public static void main(String[] args) {

int a = 28;

int b = 5;

int c;

double d, e, f;

System.*out*.println();

System.*out*.println("
*** ARITHMETIC OPERATORS *** ");

System.*out*.println();

System.*out*.println("a = " + a);

System.*out*.println("b = " + b);

System.*out*.println();

c = a + b;

System.*out*.println("a + b =
" + c);

c = a - b;

System.*out*.println("a - b =
" + c);

c = a * b;

System.*out*.println("a * b =
" + c);

*// to convert integer
into double
*d = a;

e = b;

f = d / e;

System.

*out*.println("a / b = " + f);

c = a % b;

System.

*out*.println("a % b = " + c);

System.

*out*.println();

a++;

System.

*out*.println("a++ = " + a);

a--;

System.

*out*.println("a-- = " + a);

System.

*out*.println();

a = 28;

a += b;

System.

*out*.println("a += b = " + a);

a = 28;

a -= b;

System.

*out*.println("a -= b = " + a);

a = 28;

a *= b;

System.

*out*.println("a *= b = " + a);

System.

*out*.println("a /= b = " + f);

a = 28;

a %= b;

System.

*out*.println("a %= b = " + a);

System.

*out*.println();

}

}

In the mentioned program, we initialize two variables of
type integer, then we test all arithmetic operators with those values. Since we
do not want the result **a / b** to be rounded to **5** when dividing, because we are
working with variables of type integer, it is necessary to create variables of
type float or double. In older versions of the **Java** programming language, they
would only do **cast - casting of type double**; but it doesn't work anymore.
That's why we declared three new variables **d, e, f** of type double so that they
automatically take values of type integer and convert them into type double.

That way we got a **floating-point** result; **a / b = 5.6 **It
is interesting to note that when printing to the terminal the double variable
and other numerical values in combination with the string type, it would not
be possible in the ** Python** programming language without

**str - string**type casting. It just confirms how similar the

**Java**programming language is to the

**C#**programming language. When you run the mentioned program, you will get the following results that show the values of all arithmetic expressions and, based on it, you can see how each arithmetic operator works.

***** ARITHMETIC OPERATORS
*****

**a = 28**

**a = 5**

**a + b = 33**

**a - b = 23**

**a * b = 140**

**a / b = 5.6**

**a % b = 3**

**a++ = 29**

**a-- = 28**

**a += b = 33**

**a -= b = 23**

**a *= b = 140**

**a /= b = 5.6**

**a %= b = 3**

You can watch this same program being created in the
following video.

**Java - 3. Arithmetic Operators**

**How do bitwise operators work in Java?**

To understand how a computer works with bits, octal and
hexadecimal values, and how bits are shifted left or right and filled with
zeros, you need to dedicate a significant amount of time. Find a few good old
books and study these concepts thoroughly outside of learning the **Java**
programming language. For now, at least get acquainted with the operators that
work with bits. These are:

~ NOT bitwise negation of the operand

& AND bitwise conjunction

| OR bitwise disjunction

^ XOR bitwise exclusive disjunction

>> right shift

>>> right shift with zero fill

<< left shift

&= assignment with bitwise conjunction

!= assignment with bitwise conjunction

^= assignment with bitwise exclusive disjunction

>>= assignment with right shift

>>>= assignment with right shift with zero fill

<<= assignment with left shift

**Bitwise operators in the Java programming language**

We will create a simulation using the operators listed.
Create a new program and name it **bitsoperators**. Enter the following code.

package com.tutorial;

public class bitoperators {

public static void main(String[] args) {

String
binaryNumbers[] = {

"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",

"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"

};

int a = 3; *// 0011
*int b = 5;

*// 0101*

System.

*out*.println();

System.

*out*.println(" *** BITS OPERATORS *** ");

System.

*out*.println();

System.

*out*.println(" a = " + binaryNumbers[a]);

System.

*out*.println(" b = " + binaryNumbers[b]);

System.

*out*.println();

int c = a | b;

System.

*out*.println(" a | b = " + binaryNumbers[c]);

int d = a & b;

System.

*out*.println(" a & b = " + binaryNumbers[d]);

int e = a ^ b;

System.

*out*.println(" a ^ b = " + binaryNumbers[e]);

int f = (-a & b) | (a & -b);

System.

*out*.println(" -a & b | a & -b = " + binaryNumbers[f]);

int g = -a + 0x0f;

System.

*out*.println(" -a + 0x0f = " + binaryNumbers[g]);

System.

*out*.println();

int h = a << 2;

System.

*out*.println(" a << 2 = " + binaryNumbers[h]);

int i = b >> 2;

System.

*out*.println(" a >> 2 = " + binaryNumbers[i]);

System.

*out*.println();

}

}

We know we haven't learned the arrays yet; we will study;
for now, just look at what an array of type String looks like. It is here
purely to represent all **16 bits** in bit numbers** 0** and **1** instead of declaring **16**
variables of type String. Then let's let our program do the bitwise operator
expressions instead of us doing it by foot and show us the results. This is a
very useful little program where you can see the results of how a** Java** program
works with bitwise operators. When you execute the specified code, you will get
the following result.

*****
BITS OPERATORS *****

** a
= 0011**

** b
= 0101**

** a
| b = 0111**

** a
& b = 0001**

** a
^ b = 0110**

** -a
& b | a & -b = 0001**

** -a
+ 0x0f = 1100**

** a
<< 2 = 1100**

** b
>> 2 = 0001**

You can watch this same program being created in the following video.

**Java - 4. Bitwise Operators**

**Understanding the Relational operators in Java
programming**

Relational operators determine the relationship between
one operand and another by providing a logical value of type boolean. They are
often used in if` statements for **decision-making** and loops. Relational
operators in the **Java** programming language are no different from comparison
operators in **C#** or other programming languages. The six comparison operators
are:

** ****== equal to**

** != not equal to**

** > greater than**

** >= greater than or equal to**

** < less than**

** <= less than or equal to**

**Relational operators in the Java programming language**

Let's create a simple program and call it
**relationaloperators**. Enter the following code.

package com.tutorial;

public class relationaloperators {

public static void main(String[] args) {

int a = 28;

int b = 5;

boolean c = false;

System.*out*.println();

System.*out*.println(" ***
REALATIONAL OPERATORS ***");

System.*out*.println();

System.*out*.println("a = " + a);

System.*out*.println("b = " + b);

System.*out*.println("c = " + c);

System.*out*.println();

c = a == b;

System.*out*.println("a == b : " + c);

c = a != b;

System.*out*.println("a != b : " + c);

c = a > b;

System.*out*.println("a > b : " + c);

c = a >= b;

System.*out*.println("a >= b : " + c);

c = a < b;

System.*out*.println("a < b : " + c);

c = a <= b;

System.*out*.println("a <= b : " + c);

System.*out*.println();

}

}

As you can see in the given example, we have declared two
variables of type integer; assigned two numerical values, purely so that we
have something to compare. But of course, we used a boolean variable for the
result. A numerical value can also be the entire calculation of a mathematical
task; even in that case the program will perform the calculation first; reduce
it to a single digit and compare. When you execute the above code; the results
will be as follows.

** *** RELATIONAL
OPERATORS *****

**a = 28**

**b = 5**

**c = false**

**a == b : false**

**a != b : true**

**a > b : true**

**a >= b : true**

**a < b : false**

**a <= b : false**

You can watch this same program being created in the following
video.

**Java - 5. Relational Operators**

**Logical Operators in Practice: Effective Decision Making
in Java**

Logical operators operate only on** boolean-type** operands
and produce a boolean result. What you need to know is that logical operators work
similarly to bitwise operators, except that they use boolean values instead of
bits. You will often use logical operators in your projects. Here are the
logical operators in the **Java** programming language:

& AND logical conjunction

! OR logical disjunction

^ OR or XOR logical exclusive disjunction

|| short-circuit disjunction

&& short-circuit conjunction

! NOT logical unary negation

&= assignment with logical conjunction

!= assignment with logical disjunction

^= assignment with logical disjunction

== equal to

!= not equal to

?: ternary conditional operator

**Logical operators in the Java programming language**

Create a new program and call it **logicaloperators**. Enter
the following code.

package com.tutorial;

public class logicaloperators {

public static void main(String[] args) {

boolean a = true;

boolean b = false;

boolean c = false;

System.*out*.println();

System.*out*.println(" *** LOGICAL
OPERATORS *** ");

System.*out*.println();

System.*out*.println("a = " + a);

System.*out*.println("b = " + b);

System.*out*.println("c = " + c);

System.*out*.println();

c = a & b;

System.*out*.println(" a & b :
" + c);

c = a | b;

System.*out*.println(" a | b :
" + c);

c = a ^ b;

System.*out*.println(" a ^ b :
" + c);

c = a || b;

System.*out*.println(" a || b :
" + c);

c = a && b;

System.*out*.println(" a &&
b : " + c);

c = ! a;

System.*out*.println(" ! a :
" + c);

c = a &= b;

System.*out*.println(" a &= b :
" + c);

c = a |= b;

System.*out*.println(" a |= b :
" + c);

c = a ^= b;

System.*out*.println(" a ^= b :
" + c);

c = a == b;

System.*out*.println(" a == b :
" + c);

c = a != b;

System.*out*.println(" a != b :
" + c);

c = a == true ? b == true : b == false;

System.*out*.println(" a == true ?
b == true: b == false : " + c);

System.*out*.println();

}

}

In the mentioned program, you can see, as we said, that
we use boolean variables for logical operators. Notice the ternary conditional
operator. Somewhere it is not counted as a logical operator while somewhere it
is counted depending on the division. One thing is certain, something is more
complex. In the given example it means; that a is true regardless of whether **b**
is **true **or **false**. This operator and other additional operators will become
familiar to you when you use them to simplify the structure and calculations in
your projects. When you execute this code; the result will be.

** *** LOGICAL
OPERATORS *****

**a = true**

**b = false**

**c = false**

** a
& b : false**

** a
| b : true**

** a
^ b : true**

** a
|| b : true**

** a
&& b : false**

** !
a : false**

** a
&= b : false**

** a
|= b : false**

** a
^= b : false**

** a
== b : true**

** a
!= b : false**

** a
== true ? b == true : b == false : true**

You can watch this same program being created in the
following video.

**Java - 6. Logical Operators**

## No comments:

## Post a Comment