August 6, 20091 Operators. August 6, 20092 Arithmetic Operators.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Compunet Corporation1 Programming with Visual Basic.NET Arithmetic, Logical & Bitwise Operators Week # 3 Tariq Ibn Aziz.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Binary numbers and arithmetic. ADDITION Addition (decimal)
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
© 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
MPL Week #02  Primitive Data Types  Comments  The for Statement  The if Statement  The while and do while Statements  The switch Statement  The.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
EVENT DRIVEN PROGRAMMING WITH JAVA 10/28/2015CSE Dept.1.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 3, page 1 Sun Certified Java 1.4 Programmer Chapter 3 Notes Gary Lance
August 6, 2009 Data Types, Variables, and Arrays.
3: Controlling Program Flow Using Java operators Mathematical operators Relational operators Logical operators –Primitive type: ALL (the same with C) –String:
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
COMP Primitive and Class Types Yi Hong May 14, 2015.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Sahar Mosleh California State University San MarcosPage 1 Data Types and Operators.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
Programming in java Lecture-3 (a). Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Software Technology - I Tools to do operations.....
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Programming Principles Operators and Expressions.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CompSci 230 S Programming Techniques
Chapter 7: Expressions and Assignment Statements
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Sachin Malhotra Saurabh Choudhary
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Java operatoriai
Operators and Expressions
Escape Sequences What if we wanted to print the quote character?
Operators August 6, 2009.
Comp Org & Assembly Lang
OOP With Java/ course1 Sundus Abid-Almuttalib
Names of variables, functions, classes
Presentation transcript:

August 6, Operators

August 6, Arithmetic Operators

August 6, class BasicMath { public static void main(String args[]) { // arithmetic using integers System.out.println("Integer Arithmetic"); int a = 1 + 1; int b = a * 3; int c = b / 4; int d = c - a; int e = -d; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); System.out.println("e = " + e); // arithmetic using doubles

System.out.println("\nFloating Point Arithmetic"); double da = 1 + 1; double db = da * 3; double dc = db / 4; double dd = dc - a; double de = -dd; System.out.println("da = " + da); System.out.println("db = " + db); System.out.println("dc = " + dc); System.out.println("dd = " + dd); System.out.println("de = " + de); } August 6, 20094

5 The modulus operator, %, returns the remainder of a division operation It can be applied to floating-point types as well as integer types This differs from C/C++, in which the % can only be applied to integer types

August 6, class Modulus { public static void main(String args[]) { int x = 42; double y = 42.25; System.out.println("x mod 10 = " + x % 10); System.out.println("y mod 10 = " + y % 10); } When you run this program you will get the following output: x mod 10 = 2 y mod 10 = 2.25

August 6, Arithmetic Assignment Operators a = a + 4; a += 4; Any statement of the form var = var op expression; can be rewritten as var op= expression;

August 6, The Bitwise Operators

August 6, 20099

10

August 6, class BitLogic { public static void main(String args[]) { String binary[ ] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111” }; int a = 3; // or 0011 in binary int b = 6; // or 0110 in binary int c = a | b; int d = a & b; int e = a ^ b; int f = (~a & b) | (a & ~b); int g = ~a & 0x0f; System.out.println(" a = " + binary[a]); System.out.println(" b = " + binary[b]); System.out.println(" a|b = " + binary[c]); System.out.println(" a&b = " + binary[d]);

System.out.println(" a^b = " + binary[e]); System.out.println("~a&b|a&~b = " + binary[f]); System.out.println(" ~a = " + binary[g]); } } Output of the program : a = 0011 b = 0110 a | b = 0111 a & b= 0010 a ^ b = 0101 ~a&B | a&~b = 0101 ~a = 1100 August 6,

August 6, The Left Shift It has this general form: value << num If you left-shift a byte value, that value will first be promoted to int and then shifted This means that you must discard the top three bytes of the result if what you want is the result of a shifted byte value The easiest way to do this is to simply cast the result back into a byte

August 6, class ByteShift { public static void main(String args[]) { byte a = 64, b; int i; i = a << 2; b = (byte) (a << 2); System.out.println("Original value of a: " + a); System.out.println("i and b: " + i + " " + b); } Output: Original value of a: 64 i and b: 256 0

August 6, The Right Shift The right shift operator, >>, shifts all of the bits in a value to the right a specified number of times value >> num int a = 32; a = a >> 2; // a now contains 8 int a = 35; a = a >> 2; // a still contains 8

August 6, –8 >> –4 Sign extension :- when you are shifting right, the left most bits exposed by the right shift are filled in with the contents of the top bit. If you shift -1, the result always remains -1. In the program, the right shifted value is masked by ANDing it with 0x0f to discard the sign extended bits so that the value can be used an index in to the array of hexadecimal characters.

August 6, // Masking sign extension. class HexByte { static public void main(String args[]) { char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f‘ }; byte b = (byte) 0xf1; System.out.println ("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]); } } // output: b = oxf1

August 6, The Unsigned Right Shift Unsigned, shift-right operator, >>>, which always shifts zeros into the high-order bit int a = -1; a = a >>> 24; Here is the same operation in binary form to further illustrate what is happening: –1 in binary as an int >>> in binary as an int

August 6, // Unsigned shifting a byte value. class ByteUShift { static public void main(String args[]) { char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f’ }; byte b = (byte) 0xf1; byte c = (byte) (b >> 4); byte d = (byte) (b >>> 4); byte e = (byte) ((b & 0xff) >> 4); System.out.println( “b = 0x“ + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]); System.out.println( " b >> 4 = 0x“ + hex[(c >> 4) & 0x0f] + hex[c & 0x0f]); System.out.println( " b >>> 4 = 0x“ + hex[(d >> 4) & 0x0f] + hex[d & 0x0f]);

August 6, System.out.println("(b & 0xff) >> 4 = 0x“ + hex[(e >> 4) & 0x0f] + hex[e & 0x0f]); } Output: b = 0xf1 b >> 4 = 0xff b >>> 4 = 0xff (b & 0xff) >> 4 = 0x0f

August 6, Bitwise Operator Assignments a = a >> 4; a >>= 4; a = a | b; a |= b;

August 6, Relational Operators

August 6, int a = 4; int b = 1; boolean c = a < b; int done; //... if(!done)... // Valid in C/C++ if(done)... // but not in Java. In Java, these statements must be written like this: if(done == 0))... // This is Java-style. if(done != 0)...

August 6, In Java, true and false are nonnumeric values which do not relate to zero or nonzero

August 6, Boolean Logical Operators

August 6,

August 6, Short-Circuit Logical Operators Java will not bother to evaluate the right-hand operand when the outcome of the expression can be determined by the left operand alone Example 1: if (denom != 0 && num / denom > 10) Example 2: if(c==1 & e++ < 100) d = 100;

August 6, The Assignment Operator var = expression; int x, y, z; x = y = z = 100; // set x, y, and z to 100

August 6, The ? Operator expression1 ? expression2 : expression3 Example: ratio = denom == 0 ? 0 : num / denom;

August 6, Operator Precedence

August 6, Example: a | 4 + c >> b & 7 (a | (((4 + c) >> b) & 7))

August 6, Parentheses (redundant or not) do not degrade the performance of your program Therefore, adding parentheses to reduce ambiguity does not negatively affect your program