Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

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

JAVA OPERATORS GENERAL

Chapter 4. Operations on Data

Chapter 3: Operators, Expressions and Type Conversion

Operators. Java operators are classified into three categories:

Unit 3. Operators. School of Science and Technology INTRODUCTION

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

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

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

LECTURE 3 C++ Basics Part 2

SECTION II: LANGUAGE BASICS

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Lecture 3 Tao Wang 1

Operators & Expressions

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

Prof. Navrati Saxena TA: Rochak Sachan

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

CT 229. Java Syntax 26/09/2006 CT229

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

The Arithmetic Operators

COMP Primitive and Class Types. Yi Hong May 14, 2015

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

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

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

Operators in C. Staff Incharge: S.Sasirekha

Programming in C++ 5. Integral data types

DEPARTMENT OF MATHS, MJ COLLEGE

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Fundamentals of Programming

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

UNIT- 3 Introduction to C++

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Chapter 4: Basic C Operators

Fundamental of Programming (C)

Lecture 3. More About C

Basics of Java Programming

Expression and Operator

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

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

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

14:332:231 DIGITAL LOGIC DESIGN. Hardware Description Languages

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

BASIC ELEMENTS OF A COMPUTER PROGRAM

Information Science 1

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

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

More Programming Constructs -- Introduction

Chapter 2: Basic Elements of C++

Computer Programming CS F111

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

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

Fundamentals of Programming CS-110. Lecture 3

Chapter 2: Using Data

Programming Lecture 3

Basics of Programming

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Computer System and programming in C

CMPT 125: Lecture 3 Data and Expressions

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

CSC 1214: Object-Oriented Programming

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

Chapter 2, Part III Arithmetic Operators and Decision Making

Arithmetic and Bitwise Operations on Binary Data

C/C++ Programming Lecture 7 Name:

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Full file at

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Quick Reference Guide

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

The Design of C: A Rational Reconstruction"

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

Basic Arithmetic (adding and subtracting)

Objectives. In this chapter, you will:

Operators and Expressions

ISA 563 : Fundamentals of Systems Programming

Here n is a variable name. The value of that variable is 176.

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

ECEN 468 Advanced Logic Design

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

4. Inputting data or messages to a function is called passing data to the function.

Number representations

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

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

Expressions and Precedence. Last updated 12/10/18

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

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Transcription:

Course Name: Advanced Java

Lecture 2 Topics to be covered Variables Operators

Variables -Introduction A variables can be considered as a name given to the location in memory where values are stored. One syntax of variable declaration datatype variablename; Do you imagine that the variable can change its value yes, that is why it is called as variable. Is the following variable name is legal: GrandTotal? Suggest good name for it?

Variable Names and Keywords Use only the characters 'a' through 'z', 'A' through 'Z', '0' through '9', character '_', and character '$'. A name can t contain space character. Do not start with a digit. A name can be of any length. Upper and lower case count as different characters. So SUM and Sum are different names. A name can not be a reserved word. A name must not already be in use in this part of the program. A reserved word is a word which has a predefined meaning in Java. For example int, double, true, and import are reserved words.

Expressions An expression is a combination of constants (like 100), operators ( like +), variables(section of memory) and parentheses ( like ( and ) ) used to calculate a value. x = 1; y = 100 + x; This is the stuff that you know from algebra, like: (32 - y) / (x + 5) There are rules for this, but best rule is that an expression must look OK as algebra. Based on what you know about algebra, what is the value of 12-4/2 + 2? Spaces don t much matter. JAVA-NLP Lecture series

Operators--An Introduction Operators Operators are used to compute and compare values and test multiple conditions. You can connect two values or variables in a logical expression using operators. Operators also depict a relationship between two values or variables. An expression without operators is similar to a sentence without conjunctions.

Java provides the following types of operators Arithmetic Assignment Unary Comparison Shift Bit-wise Logical Conditional New

Arithmetic Operators Arithmetic operators are used to write expressions that produce numerical output. Assume integer variable A holds 10 and variable B holds 20 then Operator Description Example + Addition - Adds values on either side of the operator A + B will give 30 - Subtraction - Subtracts right hand operand from left A - B will give -10 hand operand * Multiplication - Multiplies values on either side of the operator A * B will give 200 / Division - Divides left hand operand by right hand operand B / A will give 2 % Modulus - Divides left hand operand by right hand operand and returns remainder B % A will give 0

Arithmetic Operators What is the value of 12 + 3 An arithmetic operator is a symbol that asks for doing some arithmetic. Operator Precedence + * / % middle + - Meaning Unary minus Unary Plus Multiplication Division Modulus highest highest middle middle addition Subtraction low low JAVA-NLP Lecture series

Assignment Operators These operators are used to assign values to a variable. If a variable is not assigned a value, the variable is initialized with the default value. Operator Description Example = Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assigne value of A + B into C += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C+A -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C-A *= Multiply AND assignment operator, It multiplies right operand with C *= A is equivalent to C = the left operand and assign the result to left operand C*A /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C/A %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C =C%A

Unary Operators Unary operators use only a single operand, such as x++. In this case, x is the single operand and the unary operator is +. Assume integer variable A holds 10 and variable B holds 20 then Operator Description Example ++ Increment - Increase the value of B++ gives 21 operand by 1 -- Decrement - Decrease the value B-- gives 19 of operand by 1

Comparison Operators You can compare the values stored in two variables using the comparison operators. Assume variable A holds 10 and variable B holds 20 then: Operator Description Example == Checks if the value of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (A!= B) is true. > Checks if the value of left operand is greater than the (A > B) is not true. value of right operand, if yes then condition becomes true. < Checks if the value of left operand is less than the value of (A < B) is true. right operand, if yes then condition becomes true. >= Checks if the value of left operand is greater than or equal (A >= B) is not true. to the value of right operand, if yes then condition becomes true. <= Checks if the value of left operand is less than or equal to (A <= B) is true. the value of right operand, if yes then condition becomes true.

Shift Operators In memory, data is stored in binary format. A bit can have a value of one or zero. Eight bits form a byte. Decimal Binary Equivalent 0 00000000 1 00000001 2 00000010 3 00000011 4 00000100 5 00000101 6 00000110 7 00000111 8 00001000

Cont. Shift operators work on bits of data. You use the shift operators to move the bit pattern to the left or right. Operator Description Assume integer variable A holdsexample 60 and << Binary Left Shift Operator. The left A << 2 will give 240 which variable B holds 13 then operands value is moved left by the is 1111 0000 number of bits specified by the right operand. >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 1111 >>> Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. A >>>2 will give 15 which is 0000 1111

Signed Left Shift ("<<") The signed left shift ("<<") operator shifts a bit (or bits) to the left by the distance specified in the right operand. In this case, the leftmost digit is shifted at the end of the register, and a new 0 is shifted into the rightmost position. No matter, the number is positive or negative; In both of case the leading bit position is always filled with a zero.

Signed Right Shift (">>") The signed right shift (">>") operator shifts a bit (or bits) to the right by the distance specified in the right operand and fills the left most bit by the sign bit. In this case the rightmost bit (or bits) is shifted out, and a new 0 is filled with the sign bit into the highhigh-order bits to the left position if the left operand is positive; otherwise 1, if the left operand is negative. This technique is known as sign extension.

Unsigned Right Shift (">>>") The unsigned right shift (">>>") operator behave like the signed right shift operator. i.e. it shifts a bit (or bits) to the right. But unlike ">>" operator, this operator always shifts zeros into the leftmost position by the distance specified in the right operand. So the result of applying the >>>operator is always positive. For example, the expression "14>>>2"; shifts all bits of the number 14 to the right placing a zero to the left for each blank place Thus the value 1110 becomes 0011 or 3 in decimal. An unsigned shift operation of a negative number generally returns the result in a positive number, because any unsigned right shift operation replaces the leading sign bit with a zero which indicates a positive number

Bit--wise operators Bit You use bit-wise operators to evaluate complex conditions, such as depicting logical circuits. When you use a bit-wise operator, the operands are first converted into their binary equivalents. Assume integer variable A holds 60 and variable B holds 13 then Operator Description Example & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100 Binary OR Operator copies a bit if it exists in eather operand. (A B) will give 61 which is 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -60 which is 1100 0011

Logical Operators You use logical operators to combine the results of Boolean expressions. Assume boolean variables A holds true and variable B holds false then Operator Description Example && Called Logical AND operator. If both the operands are non zero then then condition becomes true. (A && B) is false. Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. (A B) is true.! Called Logical NOT Operator. Use to!(a && B) is true. reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

Conditional Operators Conditional operators enable you to write statements that have a conditional or a selection construct. Operator Description Example (condition)?val1:val2 Evaluates to val1 if the X=(y>z)?y:z condition returns true and val2 if the condition X is assigned the value returns false. of y if y is greater than z, else x is assigned the value of z.

New Operator When you create an instance of a class, which is called an object, you need to allocate memory. To allocate memory for an object, there are two ways: Declare the object and then allocate memory using new operator. <class_name><object_name>; <object_name>= new <class_name>(); Declare the object and simultaneously allocate memory using the new operator. <class_name><object_name>=new <class_name>();