© 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill.

Slides:



Advertisements
Similar presentations
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Advertisements

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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 3. Operators SPARCS JAVA Study.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 3, page 1 Sun Certified Java 1.4 Programmer Chapter 3 Notes Gary Lance
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
3: Controlling Program Flow Using Java operators Mathematical operators Relational operators Logical operators –Primitive type: ALL (the same with C) –String:
Primitive Variables.
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.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
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.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
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.....
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
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.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Programming Principles Operators and Expressions.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
August 6, Operators. August 6, Arithmetic Operators.
Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CompSci 230 S Programming Techniques
Chapter 7: Expressions and Assignment Statements
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Java operatoriai
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
OPERATORS (2) CSC 111.
Chapter 2: Basic Elements of Java
Operators August 6, 2009.
Expressions and Assignment
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Presentation transcript:

© 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill

© 2001 by Ashby M. Woolf Revision 3 Assignment Operator (=) lvalue = rvalue; Take the value of the rvalue and store it in the lvalue. The rvalue is any constant, variable or expression. The lvalue is named variable. w = 10; x = w; z = (x - 2)/(2 + 2);

© 2001 by Ashby M. Woolf Revision 3 Mathematical Operators Addition + Subtraction - Multiplication * Division/ Modulus%

© 2001 by Ashby M. Woolf Revision 3 Simple Arithmetic public class Example { public static void main(String[] args) { int j, k, p, q, r, s, t; j = 5; k = 2; p = j + k; q = j - k; r = j * k; s = j / k; t = j % k; System.out.println("p = " + p); System.out.println("q = " + q); System.out.println("r = " + r); System.out.println("s = " + s); System.out.println("t = " + t); } > java Example p = 7 q = 3 r = 10 s = 2 t = 1 >

© 2001 by Ashby M. Woolf Revision 3 Shorthand Operators +=, -=, *=, /=, %= Common Shorthand a = a + b;a += b; a = a - b;a -= b; a = a * b;a *= b; a = a / b;a /= b; a = a % b;a %= b;

© 2001 by Ashby M. Woolf Revision 3 Shorthand Operators public class Example { public static void main(String[] args) { int j, p, q, r, s, t; j = 5; p = 1; q = 2; r = 3; s = 4; t = 5; p += j; q -= j; r *= j; s /= j; t %= j; System.out.println("p = " + p); System.out.println("q = " + q); System.out.println("r = " + r); System.out.println("s = " + s); System.out.println("t = " + t); } > java Example p = 6 q = -3 r = 15 s = 0 t = 0 >

© 2001 by Ashby M. Woolf Revision 3 Shorthand Increment and Decrement ++ and -- Common Shorthand a = a + 1;a++; or ++a; a = a - 1;a--; or --a;

© 2001 by Ashby M. Woolf Revision 3 Increment and Decrement > java example p = 6 q = 6 j = 7 r = 6 s = 6 > public class Example { public static void main(String[] args) { int j, p, q, r, s; j = 5; p = ++j; // j = j + 1; p = j; System.out.println("p = " + p); q = j++; // q = j; j = j + 1; System.out.println("q = " + q); System.out.println("j = " + j); r = --j; // j = j -1; r = j; System.out.println("r = " + r); s = j--; // s = j; j = j - 1; System.out.println("s = " + s); }

© 2001 by Ashby M. Woolf Revision 3 Arithmetic Operators A Change of Topic Casting long 64 bit l int 32 bit i

© 2001 by Ashby M. Woolf Revision 3 long 64 bit int 32 bit l i int i; i = (int)l; l = (long)i; Moving Between Buckets "Casting" long l; l = i; long 64 bit int 32 bit l i Widening Narrowing

© 2001 by Ashby M. Woolf Revision 3 DeclarationSize and TypeNumber Range byte b;// 8 bit integer to short s;// 16 bit integer to char c;// 16 bit Unicode 0 to int i;// 32 bit integer to long l;// 64 bit integer to float f;// 32 bit floating pointIEEE754 Standard double d;// 64 bit floating pointIEEE754 Standard The Primitive Numbers

© 2001 by Ashby M. Woolf Revision 3 Casting the Primitives b = (byte)s; b = (byte)c; b = (byte)i; b = (byte)l; b = (byte)f; b = (byte)d; i = (int)b; i = (int)s; i = (int)c; i = (int)l; i = (int)f; i = (int)d; l = (long)b; l = (long)s; l = (long)c; l = (long)i; l = (long)f; l = (long)d; s = (short)b; s = (short)c; s = (short)i; s = (short)l; s = (short)f; s = (short)d; 2's Complement c = (char)b; c = (char)s; c = (char)i; c = (char)l; c = (char)f; c = (char)d; Positive Only f = (float)b; f = (float)s; f = (float)c; f = (float)i; f = (float)l; f = (float)d; d = (double)b; d = (double)s; d = (double)c; d = (double)i; d = (double)l; d = (double)f; Floating Point b = (byte)s; b = (byte)c; b = (byte)i; b = (byte)l; b = (byte)f; b = (byte)d; i = b; i = s; i = c; i = (int)l; i = (int)f; i = (int)d; l = b; l = s; l = c; l = i; l = (long)f; l = (long)d; s = b; s = (short)c; s = (short)i; s = (short)l; s = (short)f; s = (short)d; 2's Complement c = (char)b; c = (char)s; c = (char)i; c = (char)l; c = (char)f; c = (char)d; Positive Only f = b; f = s; f = c; f = i; f = l; f = (float)d; d = b; d = s; d = c; d = i; d = l; d = f; Floating Point

© 2001 by Ashby M. Woolf Revision 3 Acceptable Implicit Casts char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit Illegal b = l; l = f; c = s; OK l = b; i = c; f = l

© 2001 by Ashby M. Woolf Revision 3 char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit Automatic Promotion with Arithmetic Illegal Casts s = s + b; s = s + s; OK s = (short)(s + b); s = (short)(s + s); Arithmetic is never done in 8 or 16 bit containers.

© 2001 by Ashby M. Woolf Revision 3 char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit Arithmetic Promotion with Mixed Primitives Illegal Casts i = i + l; f = f + d; l = l + f; OK i = (int)(i + l); d = (double)(f + d); l = (long)(l + f); Arithmetic is done in the "widest" type.

© 2001 by Ashby M. Woolf Revision 3 Implicit Casts in Method Calls char 16 bit double 64 bit float 32 bit long 64 bit int 32 bit short 16 bit byte 8 bit Illegal i = st.indexOf(f); OK i = st.indexOf(c); i = st.indexOf(b); For: String st; and, public int indexOf(int ch);

© 2001 by Ashby M. Woolf Revision 3 Casting A Change of Topic The Logical and Relational Operators

© 2001 by Ashby M. Woolf Revision 3 Relational Operators > = <= == != Primitives Greater Than > Less Than < Greater Than or Equal >= Less Than or Equal<= Primitives or Object References Equal (Equivalent)== Not Equal != The Result is Always true or false

© 2001 by Ashby M. Woolf Revision 3 Relational Operator Examples public class Example { public static void main(String[] args) { int p =2; int q = 2; int r = 3; Integer i = new Integer(10); Integer j = new Integer(10); System.out.println("p < r " + (p < r)); System.out.println("p > r " + (p > r)); System.out.println("p == q " + (p == q)); System.out.println("p != q " + (p != q)); System.out.println("i == j " + (i == j)); System.out.println("i != j " + (i != j)); } > java Example p < r true p > r false p == q true p != q false i == j false i != j true >

© 2001 by Ashby M. Woolf Revision 3 Logical Operators (boolean) && || ! Logical AND && Logical OR || Logical NOT !

© 2001 by Ashby M. Woolf Revision 3 Logical (&&) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("f && f " + (f && f)); System.out.println("f && t " + (f && t)); System.out.println("t && f " + (t && f)); System.out.println("t && t " + (t && t)); } > java Example f && f false f && t false t && f false t && t true >

© 2001 by Ashby M. Woolf Revision 3 Logical (||) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("f || f " + (f || f)); System.out.println("f || t " + (f || t)); System.out.println("t || f " + (t || f)); System.out.println("t || t " + (t || t)); } > java Example f || f false f || t true t || f true t || t true >

© 2001 by Ashby M. Woolf Revision 3 Logical (!) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("!f " + !f); System.out.println("!t " + !t); } > java Example !f true !t false >

© 2001 by Ashby M. Woolf Revision 3 Logical Operator Examples Short Circuiting with && public class Example { public static void main(String[] args) { boolean b; int j, k; j = 0; k = 0; b = ( j++ == k ) && ( j == ++k ); System.out.println("b, j, k " + b + ", " + j + ", " + k); j = 0; k = 0; b = ( j++ != k ) && ( j == ++k ); System.out.println("b, j, k " + b + ", " + j + ", " + k); } > java Example b, j, k true 1, 1 > java Example b, j, k true 1, 1 b, j, k false 1, 0 >

© 2001 by Ashby M. Woolf Revision 3 Logical Operator Examples Short Circuiting with || public class Example { public static void main(String[] args) { boolean b; int j, k; j = 0; k = 0; b = ( j++ == k ) || ( j == ++k ); System.out.println("b, j, k " + b + ", " + j + ", " + k); j = 0; k = 0; b = ( j++ != k ) || ( j == ++k ); System.out.println("b, j, k " + b + ", " + j + ", " + k); } > java Example b, j, k true 1, 0 > java Example b, j, k true 1, 0 b, j, k true 1, 1 >

© 2001 by Ashby M. Woolf Revision 3 The Logical and Relational Operators A Change of Topic Manipulating the Bits

© 2001 by Ashby M. Woolf Revision 3 Logical Operators (Bit Level) & | ^ ~ AND & OR | XOR ^ NOT ~

© 2001 by Ashby M. Woolf Revision 3 Twos Complement Numbers Base 10 A byte of binary

© 2001 by Ashby M. Woolf Revision 3 Adding Twos Complements Base 10Binary Base 10Binary

© 2001 by Ashby M. Woolf Revision 3 Logical Operators (Bit Level) & | ^ ~ int a = 10; // = 10 int b = 12; // = 12 a b a & b a b a | b a b a ^ b a ~a & AND | OR ^ XOR ~ NOT

© 2001 by Ashby M. Woolf Revision 3 Logical (bit) Operator Examples public class Example { public static void main(String[] args) { int a = 10; // = 10 int b = 12; // = 12 int and, or, xor, na; and = a & b; // = 8 or = a | b; // = 14 xor = a ^ b; // = 6 na = ~a; // = -11 System.out.println("and " + and); System.out.println("or " + or); System.out.println("xor " + xor); System.out.println("na " + na); } > java Example and 8 or 14 xor 6 na -11 >

© 2001 by Ashby M. Woolf Revision 3 Shift Operators (Bit Level) > >>> Shift Left <<Fill with Zeros Shift Right >>Based on Sign Shift Right >>>Fill with Zeros

© 2001 by Ashby M. Woolf Revision 3 Shift Operators > int a = 3; // = 3 int b = -4; // = -4 a a << b b << << Left >> Right a a >> b b >>

© 2001 by Ashby M. Woolf Revision 3 Shift Operator >>> int a = 3; // = 3 int b = -4; // = -4 >>> Right 0 a a >>> b b >>> big

© 2001 by Ashby M. Woolf Revision 3 Shift Operator Examples public class Example { public static void main(String[] args) { int a = 3; // = 3 int b = -4; // = -4 System.out.println("a<<2 = " + (a<<2)); System.out.println("b<<2 = " + (b<<2)); System.out.println("a>>2 = " + (a>>2)); System.out.println("b>>2 = " + (b>>2)); System.out.println("a>>>2 = " + (a>>>2)); System.out.println("b>>>2 = " + (b>>>2)); } > java Example a<<2 = 12 b<<2 = -16 a>>2 = 0 b>>2 = -1 a>>>2 = 0 b>>>2 = >

© 2001 by Ashby M. Woolf Revision 3 Shift Operator >>> and Automatic Arithmetic Promotion byte a = 3; // = 3 byte b = -4; // = -4 byte c; c = (byte) a >>> 2 c = (byte) b >>> 2 >>> Right Fill 0 a a >>> c = (byte) b b >>> c = (byte) Much to big for byte

© 2001 by Ashby M. Woolf Revision 3 Which Operators Operate On What Operators && || ! Unary * / % > = <= == != > >>> & | ^ ~ = op= etc. floatdouble Floating Point charbyteshortintlong Automatic Promotion Except Automatic Promotion Except Automatic Promotion Except Automatic Promotion Except Automatic Promotion Except Automatic Promotion Except Automatic Promotion Automatic Promotion Automatic Promotion Integral boolean Logical Any Object + with String Only Reference Only Not Content

© 2001 by Ashby M. Woolf Revision 3 Assignment Operator (=) and Classes Date x = new Date(); Date y = new Date(); x = y;

© 2001 by Ashby M. Woolf Revision 3 Assignment Operator (=) and Classes Date x = new Date(); Date y = new Date(); x = y;

© 2001 by Ashby M. Woolf Revision 3 Manipulating the Bits A Change of Topic Some Odds and Ends

© 2001 by Ashby M. Woolf Revision 3 Ternary Operator ? : If true this expression is evaluated and becomes the value entire expression. Any expression that evaluates to a boolean value. If false this expression is evaluated and becomes the value entire expression. boolean_expression ? expression_1 : expression_2

© 2001 by Ashby M. Woolf Revision 3 Ternary ( ? : ) Operator Examples public class Example { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println("t?true:false "+(t ? true : false )); System.out.println("t?1:2 "+(t ? 1 : 2 )); System.out.println("f?true:false "+(f ? true : false )); System.out.println("f?1:2 "+(f ? 1 : 2 )); } > java Example t?true:false true t?1:2 1 f?true:false false f?1:2 2 >

© 2001 by Ashby M. Woolf Revision 3 String (+) Operator String Concatenation "Now is " + "the time." "Now is the time."

© 2001 by Ashby M. Woolf Revision 3 String (+) Operator Automatic Conversion to a String If either expression_1 If either expression_1 or expression_2 evaluates to a string the other will be converted to a string if needed. The result will be their concatenation. expression_1 + expression_2

© 2001 by Ashby M. Woolf Revision 3 String (+) Operator Automatic Conversion with Primitives "The number is " + 4 "The number is " + "4" "The number is 4"

© 2001 by Ashby M. Woolf Revision 3 String (+) Operator Automatic Conversion with Objects "Today is " + new Date() "Today is " + "Wed 27 22:12;26 CST 2000" "Today is Wed 27 22:12;26 CST 2000" "Today is " + new Date().toString()

© 2001 by Ashby M. Woolf Revision 3 Operator Precedence ! ~ () * / % + - > >>> > = <= instanceof == != & | ^ && || ?: = (and += etc.) Unary Arithmetic Shift Comparison Logical Bit Boolean Ternary Assignment

© 2001 by Ashby M. Woolf Revision 3 Passing Classes to Methods class Letter { char c; } public class PassObject { static void f(Letter y) { y.c = 'z'; } public static void main(String[] args) { Letter x = new Letter(); x.c = 'a'; System.out.println("1: x.c: " + x.c); // Prints 1: x.c: a f(x); System.out.println("2: x.c: " + x.c); // Prints 2: x.c: z }

© 2001 by Ashby M. Woolf Revision 3 Passing Primitives to Methods class Letter { char c; } public class PassPrimitive { static void f(char y) { y = 'z'; } public static void main(String[] args) { Letter x = new Letter(); x.c = 'a'; System.out.println("1: x.c: " + x.c); // Prints 1: x.c: a f(x.c); System.out.println("2: x.c: " + x.c); // Prints 2: x.c: a }

© 2001 by Ashby M. Woolf Revision 3 End of Content