April 6, 1998CS102-02Lecture 2-1 Java Operators CS 102-02 Lecture 2-1 Being a Smooth Operator.

Slides:



Advertisements
Similar presentations
Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and
Advertisements

ISBN Chapter 7 Expressions and Assignment Statements.
Types and Arithmetic Operators
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Python November 14, Unit 7. Python Hello world, in class.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
ISBN Chapter 7 Expressions and Assignment Statements.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
C H A P T E R S E V E N Expressions and Assignment Statements.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
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.
Building Java Programs Primitive Data and Definite Loops.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
ISBN Chapter 7 Expressions and Assignment Statements.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
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.
Primitive Variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
What are Operators? Some useful operators to get you started.
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.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Expressions and Assignment Statements
CompSci 230 S Programming Techniques
G. Pullaiah College of Engineering and Technology
CSE 220 – C Programming Expressions.
Chapter 12 Variables and Operators
Expressions and Assignment Statements
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive Data, Variables, Loops (Maybe)
Expressions and Assignment Statements
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Operators and Expressions
Operators and Expressions
Expressions and Assignment Statements
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Expressions and Assignment Statements
Introduction to Java, and DrJava part 1
Chapter-3 Operators.
Introduction to Java, and DrJava
Expressions and Assignment
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 2 Programming Basics.
SSEA Computer Science: Track A
Primitive Types and Expressions
Introduction to Java, and DrJava part 1
OPERATORS in C Programming
Problem 1 Given n, calculate 2n
OPERATORS in C Programming
Presentation transcript:

April 6, 1998CS102-02Lecture 2-1 Java Operators CS Lecture 2-1 Being a Smooth Operator

April 6, 1998CS102-02Lecture 2-1 Operating with Java Most programming languages have operators –Operators are short-hand symbols for actions = Assign right to left + Add two numbers (or concatenate two strings) –Operators in Java have fixed meaning No operator overloading Can’t say: List = List + Item;// Add item to list

April 6, 1998CS102-02Lecture 2-1 Kinds of Operators

April 6, 1998CS102-02Lecture 2-1 Operator Precedence Usually things go left-to-right, but there are precedence rules Nutshell reading lists operators by precedence Override precedence with ()’s

April 6, 1998CS102-02Lecture 2-1 Arithmetic Operators The usual suspects: plus, minus, blah, blah, blah Modulo/remainder operator

April 6, 1998CS102-02Lecture 2-1 Modulo Operator Modulo (or remainder) operator: what’s left over after division 7%3 = 1 198%3 = ?? 6.0%4.0 = 2 Is it odd or even? Looping with clock arithmetic –Appointment at 5pm everyday –Baking 217 cakes: step 3 of 7 same as 24 of 28

April 6, 1998CS102-02Lecture 2-1 Short-Hand Operators Increment and decrement: ++ and -- –Often need to add or subtract 1 –Pre: Add (subtract) first –Post: Add (subtract) afterwards Compiler can sometimes optimize

April 6, 1998CS102-02Lecture 2-1 Testing Out Short-Hand Suppose we start with: X = 7; Y = 9; What’s the difference between: X++; ++X;

April 6, 1998CS102-02Lecture 2-1 Are You My Type? What’s the type of a result? ExpressionResult type int * int int float * float?? int * float?? int / int?? Conversion & promotion

April 6, 1998CS102-02Lecture 2-1 Assignment Operators Change the value on the left to the value of the expression on the right If you want to:Try: Assign 8 to YY = 8; Add 1 to YY++; Assign Y+10 to YX += 10;

April 6, 1998CS102-02Lecture 2-1 Works for Strings Too Strings are “added” (concatenated) with + What is Name after the third line? Name = “Simpson”; First = “Lisa”; Name += First; What’s the result here? Age = 11; Message = “He’s “ + Age + “ years old.”;

April 6, 1998CS102-02Lecture 2-1 Conditional Operator Instead of If..Then..Else, use ?: Takes three arguments in the form: Boolean condition ? If-true : If-false If (Simpson == “Lisa”) { Message = “She’s our favorite!”; } else { Message= “Doh!”; } System.out.println(Message); is the same as…

April 6, 1998CS102-02Lecture 2-1 Using the Conditional Operator System.out.println(Simpson==“Lisa” ? ”She’s our favorite” :“Doh!”); (The above should be on one line in a real program)

April 6, 1998CS102-02Lecture 2-1 And, But and Or will get you pretty far.. Logical operators combine simple expressions to form complex ones Boolean logic

April 6, 1998CS102-02Lecture 2-1 Boolean Types True or false are real values in Java Some languages just use 0 and not 0 if (y = 7) then … In Java result of a comparison is Boolean 8 != 9?? 8 != 8??

April 6, 1998CS102-02Lecture 2-1 Logical Operators in Java Translating logic into Java AND&& OR|| XOR^ NOT!

April 6, 1998CS102-02Lecture 2-1 Boolean Expressions De Morgan’s Laws with Expressions One & Two One OR Two == One AND Two One AND Two == One OR Two Some handy relations One XOR One == False One OR One == True

April 6, 1998CS102-02Lecture 2-1 Short-Circuit Remember: False AND Anything == False True OR Anything == True Sometimes compiler can short-circuit and skip evaluation of second expression What if there are side effects?

April 6, 1998CS102-02Lecture 2-1 Sideline on Side Effects Side effects are results of expression evaluation other than the expression’s value Examples X++; –Output: System.out.println(“Howdy!”);

April 6, 1998CS102-02Lecture 2-1 Short-Circuiting Side Effects Short-circuiting could prevent a side effect How do you force the compiler to evaluate a second expression?

April 6, 1998CS102-02Lecture 2-1 No Short-Circuit Here Guarantee that the second expression is evaluated AND& OR| XOR^ (Why is ^ listed here?)

April 6, 1998CS102-02Lecture 2-1 Relational Operators Determine the relationship between values Equality & inequality Less than, greater than

April 6, 1998CS102-02Lecture 2-1 (In)Equality Equality is different from assignment == != = Most keyboards just have = –Use == for equality –And != for inequality

April 6, 1998CS102-02Lecture 2-1 Bitwise Operators Computers are binary creatures: everything’s on or off For example, computers can’t store decimal numbers so

April 6, 1998CS102-02Lecture 2-1 Binary Arithmetic Everything’s in powers of two Turn 78 into:

April 6, 1998CS102-02Lecture 2-1 Accentuate the positive Computers don’t know about negative numbers Use the first (leftmost) bit as a sign bit: 1 if negative:-5 is if positive:+5 is

April 6, 1998CS102-02Lecture 2-1 Bitwise is Binary Work with the bits inside the values Only good for integral values (integer numbers, bytes and characters)

April 6, 1998CS102-02Lecture 2-1 And Shift Your Bits ‘Round and ‘Round Bitwise AND of 78 and 34

April 6, 1998CS102-02Lecture 2-1 Why Bother with Bitwise? Use numbers not for themselves but for their internal representations Example: A tic-tac-toe grid might have 0’s for O’s and 1’s for X’s Just need 9 bits to do the whole table and only 27 bits for 3-D tic-tac-toe

April 6, 1998CS102-02Lecture 2-1 That’s It for Operators Operators are key to building large expressions in Java Know operator precedence (or at least where to look it up) Next time: Use operators to build expressions for control structures