Lecture 2: Data Types, Variables, Operators, and Expressions

Slides:



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

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
JAVA PROGRAMMING PART II.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
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.
Chapter 2: Java Fundamentals
1 Variables. 2 Receipt example What's bad about the following code? public class Receipt { public static void main(String[] args) { // Calculate total.
Building Java Programs Primitive Data and Definite Loops.
1 Primitive data types, expressions, and variables.
1 Java Basics: Data Types, Variables, and Loops “ If debugging is the process of removing software bugs, then programming must be the process of putting.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Topic 4 Expressions and variables Based on slides bu Marty Stepp and Stuart Reges from "Once a person has understood.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chapter 4 Assignment Statement
Lecture 4: Expressions and Variables
Building Java Programs
Lecture 2: Operations and Data Types
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Variables and Arithmetic Operators in JavaScript
University of Central Florida COP 3330 Object Oriented Programming
Primitive Data, Variables, Loops (Maybe)
Chapter 3 Assignment Statement
Building Java Programs Chapter 2
Starting JavaProgramming
Topic 4 Expressions and variables
null, true, and false are also reserved.
Introduction to Java Programming
An overview of Java, Data types and variables
Building Java Programs
Chapter 1: Computer Systems
Building Java Programs
Topic 4 Expressions and variables
JavaScript Reserved Words
Building Java Programs
CISC124 TA names and s have been added to the course web site.
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Focus of the Course Object-Oriented Software Development
Module 2 - Part 1 Variables, Assignment, and Data Types
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Chap 2. Identifiers, Keywords, and Types
Agenda Types and identifiers Practice Assignment Keywords in Java
Building Java Programs
Building Java Programs
Topic 4 Expressions and variables
Building Java Programs
Building Java Programs
Presentation transcript:

Lecture 2: Data Types, Variables, Operators, and Expressions Dr. Kyung Eun Park Summer 2017

Data Types as Data Categories Data Categorization: Integer Real number Character String Boolean Data Types: Determine the operators’ behavior / (division) for integer numbers or for real numbers

Primitive Data Types Primitive Types: Java supports 8 simple types for numbers, text, etc.: byte, short, int, long, float, double, char, & boolean Commonly Used Primitive Types in Java int: 42, -3, 0, 926394 double: 3.1, -0.25, 19.83425 char: 'a', 'X', '?', '\n' boolean: true, false Special (Object) Type: String: "Tuesday", "Java", "Hello World!"

Numeric Operators Numeric Binary Operators: Numeric Unary Operator: Combines two values (operands: left and right) 3*4, 9/2, 68%4, etc. + addition - subtraction (or negation) * multiplication / division % modulus (a.k.a. remainder) Numeric Unary Operator: -5, +6

Keywords (Reserved Words) keyword: An identifier that you cannot use because it already has a reserved meaning in Java. abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized

Expressions Simple Expression: Numeric expressions: 1+1, 3*4, (1+5)*50, 10/3, 9%3 String expressions: "Hello World!" Numeric expression plus ('+') String value String concatenation (2*6)+(4*4)+10 "Hello " + "World!" "Summer " + 2017 "Interstate " + 9 + 5 20 + 20 + "Class"

Program I: Numeric Data Types public class DataTypes { public static void main(String[] args) { System.out.println("5*8/6 = " + 5*8/6); System.out.println("(2*6)+(4*4)+10 = " + (2*6)+(4*4)+10); System.out.println("6 / 2 + 7 / 3 = " + 6 / 2 + 7 / 3); System.out.println("6 * 7 % 4 = " + 6 * 7 % 4 ); System.out.println("22 + 4 * 2 = " + 22 + 4 * 2); }  Wrong answer! Fix this using () : Higher the evaluation precedence

Evaluation Precedence Precedence: Order in which operators are evaluated. Generally operators evaluate left-to-right. 1 - 2 - 3 is (1 - 2) - 3 which is -4 But * / % have a higher level of precedence than + - 1 + 3 * 4 is 13 6 + 8 / 2 * 3 6 + 4 * 3 6 + 12 is 18 Parentheses can force a certain order of evaluation: (1 + 3) * 4 is 16

Precedence Examples 1 * 2 + 3 * 5 % 4 \_/ | 2 + 3 * 5 % 4 \_/ | 2 + 3 * 5 % 4 \_/ | 2 + 15 % 4 \___/ | 2 + 3 \________/ | 5 1 + 8 % 3 * 2 - 9 \_/ | 1 + 2 * 2 - 9 \___/ | 1 + 4 - 9 \______/ | 5 - 9 \_________/ | -4

Homework I What values result from the following expressions? 9 / 5 695 % 20 7 + 6 * 5 7 * 6 + 5 248 % 100 / 5 6 * 3 - 9 / 4 (5 - 7) * 4 6 + (18 % (17 - 12))

Program II: Evaluation public class Evaluation { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println("Hello " + "World!"); System.out.println("Summer " + 2017); System.out.println("Interstate " + 9 + 5); System.out.println(20 + 20 + " Class"); System.out.println("Class " + 20 + 20); }

Homework II What String result from the following expressions? "hello" + 42 is ______________ 1 + "abc" + 2 is ______________ "abc" + 1 + 2 is ______________ 1 + 2 + "abc" is ______________ "abc" + 9 * 3 is ______________ "1" + 1 is ______________ 4 - 1 + "abc" is ______________

Real Number Examples: 6.022 , -42.0 , 2.143e17 Placing .0 or . after an integer makes it a double The operators + - * / % () all still work with double. / produces an exact answer: 15.0 / 2.0 is 7.5 Precedence is the same: () before * / % before + -

Real Number Examples 2.0 * 2.4 + 2.25 * 4.0 / 2.0 \___/ | 4.8 + 2.25 * 4.0 / 2.0 \___/ | 4.8 + 9.0 / 2.0 \_____/ | 4.8 + 4.5 \____________/ | 9.3

Where to store values? A Named Container for Rewritable Values: variable!!! Variables are used to hold different types of values: Integer number: int container (variable) Real number: double or real container (variable) Character symbol: char container (variable) Boolean logical value: boolean container (variable) String text: String container (object)

Variable Declaration Variable declaration: Syntax Sets aside memory for storing a value Variables must be declared before they can be used Syntax type name; The name is an identifier. An identifier name must start with a letter or _ (underscore) or $ Construct using letters [A-Za-z], digits [0-9], and ‘_’ and ‘$’ symbols int x; double taxRate;

Assignment Assignment: Stores a value into a variable. Syntax name = expression; int x; x = 3; x = x + 3; double taxRate; taxRate = 0.06; x 3 x 6 taxRate 0.06

Declaration/initialization A variable can be declared/initialized in one statement. Syntax type name = expression; int x = 3; double taxRate = 0.06;

Printing a Variable’s Value Using '+', attach a variable’s value to the existing String object double grade = (95.1 + 71.9 + 82.6) / 3.0; System.out.println("Your grade was " + grade); int students = 11 + 17 + 4 + 19 + 14; System.out.println("There are " + students + " students in the course.");

Program III: Receipt public class Receipt { public static void main(String[] args) { // Calculate total owed, assuming 6% tax / 15% tip int subtotal = 38 + 40 + 30; double tax = subtotal * .06; double tip = subtotal * .15; double total = subtotal + tax + tip; System.out.println("Subtotal: " + subtotal); System.out.println("Tax: " + tax); System.out.println("Tip: " + tip); System.out.println("Total: " + total); }