bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Similar documents
JAVA OPERATORS GENERAL

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Selenium Class 9 - Java Operators

Prof. Navrati Saxena TA: Rochak Sachan

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 3: Operators, Expressions and Type Conversion

Operators in java Operator operands.

Operators. Java operators are classified into three categories:

Answer1. Features of Java

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Operators Questions

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

SECTION II: LANGUAGE BASICS

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Java Programming Language. 0 A history

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CSC 1214: Object-Oriented Programming

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Operators and Expressions

CT 229. Java Syntax 26/09/2006 CT229

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

More Programming Constructs -- Introduction

Java Simple Data Types

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Infix to Postfix Conversion

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Expressions and Precedence. Last updated 12/10/18

Programming Basics. Digital Urban Visualization. People as Flows. ia

Department of Computer Science

A flow chart is a graphical or symbolic representation of a process.

Computers Programming Course 6. Iulian Năstac

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Key Java Simple Data Types

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CT 229 Java Syntax Continued

Building Java Programs

CS1150 Principles of Computer Science Boolean, Selection Statements

Java Simple Data Types

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Operators and Expressions:

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Building Java Programs

Chapter 3 Selection Statements

Operators & Expressions

1. Short circuit AND (&&) = if first condition is false then the second is never evaluated

Chapter 3. Selections

Operators in C. Staff Incharge: S.Sasirekha

Operators. 3 Two-Minute Drill CERTIFICATION OBJECTIVES Q&A. Using Operators. Self Test

AP COMPUTER SCIENCE A

Logical and Bitwise Expressions

Zheng-Liang Lu Java Programming 45 / 79

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Computer Components. Software{ User Programs. Operating System. Hardware

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Informatics Ingeniería en Electrónica y Automática Industrial

4. Number Representations

Module - 2 Overview of Java History of Java Technology?

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Operators. 3 Two-Minute Drill CERTIFICATION OBJECTIVES Q&A. Using Operators. Self Test

Introduction to Programming (Java) 2/12

Information Science 1

A variable is a name that represents a value. For

The Java language has a wide variety of modifiers, including the following:

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Arithmetic Operators. Portability: Printing Numbers

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

coe318 Lab 2 ComplexNumber objects

More types, Methods, Conditionals. ARCS Lab.

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Expression and Operator

Lecture 3 Operators MIT AITI

Java Review. Java Program Structure // comments about the class public class MyProgram { Variables

Expressions and Casting

Computational Expression

JAVA Programming Fundamentals

3. Java - Language Constructs I

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

VARIABLES & ASSIGNMENTS

Java Basic Programming Constructs

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

CS Programming I: Branches

COMP6700/2140 Operators, Expressions, Statements

3 Operators and Assignments

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Computer Components. Software{ User Programs. Operating System. Hardware

b. Suppose you enter input from the console, when you run the program. What is the output?

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Visual Programming. Lecture 2: More types, Methods, Conditionals

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Lecture Set 4: More About Methods and More About Operators

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

DM503 Programming B. Peter Schneider-Kamp.

Transcription:

Operators in java Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc. There are many types of operators in java which are given below: Unary Operator, Arithmetic Operator, Shift Operator, Relational Operator, Bitwise Operator, Logical Operator, Ternary Operator and Assignment Operator. Java Operator Precedence Operator Type Category Precedence Unary postfix expr++ expr-- prefix ++expr --expr +expr -expr ~! Arithmetic multiplicative * / % additive + - Shift shift << >> >>> Relational comparison < > <= >= instanceof equality ==!= Bitwise bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Java Unary Operator The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.: o o o incrementing/decrementing a value by one negating an expression inverting the value of a boolean Java Unary Operator Example: ++ and int x=10; System.out.println(x++);//10 (11) System.out.println(++x);//12 System.out.println(x--);//12 (11) System.out.println(--x);//10 10 12 12 10 Java Unary Operator Example 2: ++ and -- int b=10; System.out.println(a++ + ++a);//10+12=22 System.out.println(b++ + b++);//10+11=21 22 21

Java Unary Operator Example: ~ and! int b=-10; boolean c=; boolean d=; System.out.println(~a);//-11 (minus of total positive value which starts from 0) System.out.println(~b);//9 (positive of total minus, positive starts from 0) System.out.println(!c);// (opposite of boolean value) System.out.println(!d);// -11 9 Java Arithmetic Operators Java arithmetic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations. Java Arithmetic Operator Example System.out.println(a+b);//1 System.out.println(a-b);// System.out.println(a*b);//0 System.out.println(a/b);//2 System.out.println(a%b);//0 1 0 2 0

Java Arithmetic Operator Example: Expression System.out.println(10*10/+3-1*4/2); 21 Java Left Shift Operator The Java left shift operator << is used to shift all of the bits in a value to the left side of a specified number of times. Java Left Shift Operator Example System.out.println(10<<2);//10*2^2=10*4=40 System.out.println(10<<3);//10*2^3=10*8=80 System.out.println(20<<2);//20*2^2=20*4=80 System.out.println(1<<4);//1*2^4=1*16=240 40 80 80 240 Java Right Shift Operator The Java right shift operator >> is used to move left operands value to right by the number of bits specified by the right operand. Java Right Shift Operator Example System.out.println(10>>2);//10/2^2=10/4=2 System.out.println(20>>2);//20/2^2=20/4= System.out.println(20>>3);//20/2^3=20/8=2 2 2

Java Shift Operator Example: >> vs >>> //For positive number, >> and >>> works same System.out.println(20>>2); System.out.println(20>>>2); //For negative number, >>> changes parity bit (MSB) to 0 System.out.println(-20>>2); System.out.println(-20>>>2); - 1073741819 Java AND Operator Example: Logical && and Bitwise & The logical && operator doesn't check second condition if first condition is. It checks second condition only if first one is. The bitwise & operator always checks both conditions whether first condition is or. int c=20; System.out.println(a<b&&a<c);// && = System.out.println(a<b&a<c);// & = Java AND Operator Example: Logical && vs Bitwise & int c=20; System.out.println(a<b&&a++<c);// && = //10 because second condition is not checked System.out.println(a<b&a++<c);// && = //11 because second condition is checked

10 11 Java OR Operator Example: Logical and Bitwise The logical operator doesn't check second condition if first condition is. It checks second condition only if first one is. The bitwise operator always checks both conditions whether first condition is or. int c=20; System.out.println(a>b a<c);// = System.out.println(a>b a<c);// = // vs System.out.println(a>b a++<c);// = //10 because second condition is not checked System.out.println(a>b a++<c);// = //11 because second condition is checked 10 11

Java Ternary Operator Java Ternary operator is used as one liner replacement for if-then-else statement and used a lot in java programming. it is the only conditional operator which takes three operands. Java Ternary Operator Example int a=2; int min=(a<b)?a:b; System.out.println(min); 2 Another Example: int min=(a<b)?a:b; System.out.println(min); Java Assignment Operator Java assignment operator is one of the most common operator. It is used to assign the value on its right to the operand on its left. Java Assignment Operator Example class OperatorExample{ int b=20; a+=4;//a=a+4 (a=10+4) b-=4;//b=b-4 (b=20-4) System.out.println(b); 14 16

Java Assignment Operator Example class OperatorExample{ public static void main(string[] args){ a+=3;//10+3 a-=4;//13-4 a*=2;//9*2 a/=2;//18/2 13 9 18 9 Java Assignment Operator Example: Adding short class OperatorExample{ short a=10; short b=10; //a+=b;//a=a+b internally so fine a=a+b;//compile time error because 10+10=20 now int Compile time error After type cast: short a=10; short b=10; a=(short)(a+b);//20 which is int now converted to short 20