COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Types and Arithmetic Operators
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
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.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
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.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Expressions, Data Conversion, and Input
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.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
LESSON 6 – Arithmetic Operators
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Mathematical Calculations in Java Mrs. G. Chapman.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
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.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Mathematical Calculations in Java Mrs. C. Furman.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 2. Program Construction in Java. 01 Java basics.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Expressions.
Chapter 2 Basic Computation
Chapter 7: Expressions and Assignment Statements
Expressions.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 7: Expressions and Assignment Statements
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Escape Sequences What if we wanted to print the quote character?
Data Types and Expressions
Chapter 2 Variables.
Data Types and Expressions
Data Types and Expressions
Building Java Programs
Presentation transcript:

COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University

Literals Literals are VALUES in a program int i = 10; char c = ‘A’; String s = “Hello”; In the above program, 10, ‘A’, and “Hello” are literals

datatype or type of a literal When Java sees a value, it DECIDES on a type for the literal 1234  Java will decide that this is an int type ‘a’  Java will decide that this is a char type true  Java will decide that this is a boolean type 2.3  Java will decide that this is a double type “Hi”  Java will decide that this is a String type The values will always be taken to be one of these types

Values must follow rules to be recognized int OK  1234   Not OK  1,234 (comma not allowed)  $1234 ($ not allowed) long int literal ending with l or L  12345l or 12345L double OK   1.234e25 (=1.234*10 25 )  E-24 Not OK  1,  1.23-e25 float double literals ending with f or F  0.23f or 0.23F

Values must follow rules to be recognized char single quote around a character  ‘a’, ‘A’, Not OK  ‘AB’ boolean only two valid value  true  false Not OK  TRUE  False  Yes  No  True  FALSE

String literals A String literal needs double quotes to surround them OK  “hello all”  “1234 hello ”  “a”  “a\””’

Arithmetic Operators These work with all primitive types except boolean + additive operator - subtraction operator * multiplication operator / division operator % remainder operator e.g., 5%2 = 1; 10%4 = 2; 10%6 =4 Result type depends on the type of the operand

Relational Operators Result type is boolean. Operands can be any primitive data type except boolean.

Boolean Operators Operands must be boolean. Result is of type boolean AND && OR (||) Reverse (!)

Assignment Operators “=“ is simple assignment operator It is different from “==“ which is a relational operator

++ Unary Operator ++i (pre increment) result value is new value side effect: 1 is added to value of I int i = 10; int k = ++i; // results: k=11 and i=11 i++ (post increment) result value is old value of I side effect: 1 is added to value of I int i = 10; int k = i++; // results: k=10 and i=11

Expressions Expressions are like clauses in sentences in English Like operators, expressions will typically have a result, the result will have a type, and there may be a side effect Simple expressions single operator and operands.  5/3, 5%3 method called  s.substring(0, 9)

Compound Expression Compound expressions are built from simple expressions (i >= 3) && (j/3 != 0) && (k < 5) i = j = 3 expression 1 expression 2 expressions

Precedence The order in which operations are done in an expression is defined by precedence of operators To avoid errors/confusing, using brackets ”()”

Associativity If we have operators with same precedence, which operation will be done first? This is known as “associativity” Associativity can be Right to Left (= operator) Left to Right (+ operator)

Storage per Type (in bytes)

Overflow Overflow occurs when the storage for a variable cannot hold the result int oneThousand = 1000; int oneMillion = 1000 * oneThousand; int oneBillion = 1000 * oneMillion; System.out.println(3 * oneBillion); will print out why? the result (3 billion) overflows int capacity maximum value for an int is +2,147,483,647 Use a long instead of an int (or use a double)

Data Conversion Converting one data type into another Widening conversion: no problem more space is available in the new type no data loss Narrowing conversion: problematic Less space is available in the new type data loss possible

Conversions Assignment for widening conversions int count; short pastCount; count = pastCount; //allowed and conversion done Promotion double result, sum; int count; sum = 24.32; count = 4; result = sum/count; // count promoted to double Casting: used for narrowing conversions double money = int handOver = (int) money; //thows away fraction

Special Conversion to String If one of the operands to the + operator is a String, the other operator is converted to a String and the two string are concatenated “hello” + 1  “Hello1” 1 + “hello”  “1Hello” This works for all types