# Java Operators | Ternary, Bitwise, Arithmetic, Assignment, Logical

Operators are a very important role in any programming language, it performs logic and other important operation. Java Operators have performed an operation in the program and application like addition, division, etc.

Where Operators in an easy word is a symbol/characters ( +, -, *, / etc.) used between variables (operands) and conditions.

This tutorial you will learn about All types of Java operators with syntax and the latest examples.

## Java Operator Example

Here is a simple example of adding 2 operands in java.

`int x = 30 + 50;`

## Types of Operators in Java

Here are the types of operators in java below:

• Arithmetic Operators
• Relational Operators
• Bitwise Operators
• Logical Operators
• Assignment Operators
• Misc Operators
• Unary Operator
• Ternary Operator

### Operator Precedence

The grouping of terms in an expression that affects how an expression is evaluated in Java Operator Precedence.

### Assignment Operators in Java

Let’s start with a basic one, how to assign the value to variables?

It has done by using Assignment Operators, let’s see an example and code below. In Code where int is a Data type, marks are variables name and 65 is a variable value.

` int marks =  65;`

The Java assignment operators assign ( = )  the value on its right to the variable on its left.

A complete example of Assignment Operators, with print the value in the console.

```package eye.hunts;

public class A {
protected void message() {
//Assign value
int marks =  65;
System.out.println(marks);
}
}```

Output: 65

### Arithmetic Operators in Java

To perform a math (Mathematical) operation in program or application in java, you have to Arithmetic Operator.

Let’s check the example of Java Arithmetic Operator.

```package eye.hunts;

public class A {
protected void message() {
//Assign value
int no1 = 5;
int no2 = 5;

//Arithmetic Operators
System.out.println(no1 - no2); //Subtraction
System.out.println(no1 * no2); //Multiplication
System.out.println(no1 / no2); //Division
System.out.println(no1 % no2); //Remainder
}
}```

Output: 10
0
25
1
0

### Logical Operators in JAVA

Java Logical operators are used to get the logic between variables or values for an operation. Here’s how they work.

Java Operator OR & And Examples.

```package eye.hunts;

public class A {
protected void message() {
//Assign value
Boolean test1 = true;
boolean test2 = false;

//Arithmetic Operators
System.out.println(test1 && test2); // AND
System.out.println(test1 || test2); // OR
System.out.println(!(test1 && test2)); // NOT
}
}```

Output: false
true
true

### Bitwise Operators and Shift Operators in Java

To perform bit shift operators in Java, Bitwise operators are used.

### Relational Operators in Java

To Find a Relationship between two operands uses Relational Operator like operands are equal, greater, etc. And the result is in Boolean: true or false.

### A Unary operator in java

The unary operator performs an operation on only one operand. Mostly using increment or decrement of variables value in Loop statement and if-else block.

Java Unary operator example and use.

```package eye.hunts;

public class A {
protected void message() {
//Assign value
int no1 = 5;
boolean test = true;

//Arithmetic Operators
System.out.println(+no1); // +
System.out.println(-no1); // -
System.out.println(++no1); // ++
System.out.println(--no1); // --
System.out.println(!test); // !
}
}```

Output: 5
-5
6
5
false

#### Ternary Operator in Java

Java Ternary operator is mostly used for replacement for the if-else statement. It a linear statement, and used only a conditional operator which takes three operands.

Here is a Java Ternary Operator Example.

```public class A {
protected void message() {
int a = 7;
int b = 9;
String msg = ((a > b)? "A is Big" : "B is Big ");
System.out.println(msg);
}
}```

Output: B is Big

This is a Tutorial on Java and operators (how its work get to gather) with many examples, Do share your doubt suggestions in the comment section.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java operators Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.
{"ft_lang_label":"__label__en","ft_lang_prob":0.67617536,"math_prob":0.81627893,"size":5866,"snap":"2023-40-2023-50","text_gpt3_token_len":1533,"char_repetition_ratio":0.16291368,"word_repetition_ratio":0.053291537,"special_character_ratio":0.27361065,"punctuation_ratio":0.15067178,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9836813,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-02T22:03:25Z\",\"WARC-Record-ID\":\"<urn:uuid:baa911ce-af3e-4fc1-86a4-18a3b8a6d1d2>\",\"Content-Length\":\"132558\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4b9deee6-a608-4773-a73a-4f13093707cc>\",\"WARC-Concurrent-To\":\"<urn:uuid:7417c0c0-502f-4f97-a387-8b380486dc23>\",\"WARC-IP-Address\":\"172.67.172.128\",\"WARC-Target-URI\":\"https://tutorial.eyehunts.com/java/java-operators-ternary-bitwise-arithmetic-logical/\",\"WARC-Payload-Digest\":\"sha1:KZIARR7SQQBGH6C25ZEIEDY2FWEYQKJ3\",\"WARC-Block-Digest\":\"sha1:JEESY7U26OCVNTM5IYUYCE7KNEEPBJZ3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100452.79_warc_CC-MAIN-20231202203800-20231202233800-00290.warc.gz\"}"}