What will each of the following lines print? System.out.println("number" + 6 + 4 + 5); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
Advertisements

Types and Arithmetic Operators
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CMT Programming Software Applications
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
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.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
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.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Chapter 2 Variables.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
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.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Doing math In java.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
© 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.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
2.5 Another Java Application: Adding Integers
Arithmetic operations & assignment statement
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Examples of Primitive Values
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Building Java Programs
Chapter 2 Programming Basics.
Chapter 2 Variables.
Arithmetic Expressions & Data Conversions
Building Java Programs
Presentation transcript:

What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println( ); 15 System.out.println(6 + "number" ); 6number54 System.out.println( "number); 15number

Chapter 2 Variables, Constants, Assignments, Expressions

data – How are they different? Johnny

Memory Storage In order to reserve space in memory for the variable, the computer has to know what type of data it will be. Declare = to tell what type of data the variable will hold and its name Initialize = assign the variable a value using the = sign

Primitive Data types Primitive Data Types: Simple data types the type of data that is stored in a variable: whole number int decimal number double true or false value ( 1,0) boolean Open up DataTypes in Dr. Java

String data type String is a class. It is an object variable. We can create String objects. String holds words enclosed in quotation marks. String name = “Sally”;

Review of Declaring Variables int sum; typeidentifier This statement declares sum. It reserves a space in memory large enough to store an int.

Assignments Statements We are initializing sum to 120. We use the assignment operator ( = ) to give a value to a variable. sum = 120; identifer assignment value operator

Create Variables What type of data should be used in the following? (double, int, char, boolean, or a class) 1. Your age: 2. Your bank account balance: 3. Your middle name: 4. Your height: 5. The speed limit:

Arithmetic Operators int and double operators: + addition -subtraction * multiplication /division % modulus or remainder

Arithmetic Expressions Expressions: combinations of operators and operands to perform calculations / 3 * 8 =

2 Types of Division 1. integer division – when both operands are of type integer, we truncate the fractional part. result = 7 / 3; 2. floating point division – if one or both operands are floating point numbers, then the fractional part is kept. double dresult = 7.0 / 3.0; result 2 dresult …

Integer and Double division Try this in the interactions pane 6/4 6.0 / 4 13 / / 2

Modulus Operator % % (mod) remainder operator – returns the remainder when you divide 2 integers or doubles. Example: int result = 7 % 3; Remainder result 1

Order of Operations Java has to follow the order of operations when computing an expression As a reminder if operators are on the same level, we work left to right. Assignments work right to left. Parenthesis Multiplication, Division, Modulus (L to R) Addition, Subtraction, String Concatenation (L to R) Assignment operator

Operator Precedence Expressions are solved according to an order of operations. PEMDAS otherwise they are solved left to right. You should always use parenthesis to assure correctness. * / % first from left to right + - next left to right / 3 % 2; ( ) / 3 % 2; 3 * / 2);

Website Go to the internet Click on expression in java

Operator Precedence a + (b-c) * d – e a % (b * c) * d * e

int a = 6, b = 10, c = 9; double w = 12.9, y = 3.2; 1.a + b * c_______________________ 2.a % b / y_______________________ 3.a - b – c_______________________ 4.a - b / c_______________________ 5.a / b_______________________ 6.b / a_______________________ * 9 6 % 10 / – 10 – 9 6 – 10 / 9 6 / / ,

int a = 6, b = 10, c = 9; double w = 12.9, y = 3.2; 1.w/y_______________________ 2.y/w_______________________ 3.a + w / b _______________________ / / /

Assignment Problets website.