Primitive Types and Expressions

Slides:



Advertisements
Similar presentations
Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types and Arithmetic Operators
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
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.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Intro to CS – Honors I Programming Basics GEORGIOS PORTOKALIDIS
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Lesson 3 – Primitive Data Types Objective: Students will investigate the definition and usage of objects and primitive data types in Java in order to practice.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Primitive Types, Strings, and Console I/O Chapter 2.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 2 Variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
1 2. Program Construction in Java. 01 Java basics.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Primitive Types Four integer types: Two floating-point types:
Chapter 2 Variables.
Chapter 2 Basic Computation
Chapter 2: Introduction to C++
Computing and Statistical Data Analysis Lecture 2
BASIC ELEMENTS OF A COMPUTER PROGRAM
ITEC113 Algorithms and Programming Techniques
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
Chapter 2: Introduction to C++
Chapter 2 Basic Computation
2.1 Parts of a C++ Program.
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Fundamentals 2.
Chapter 2: Java Fundamentals
Expressions and Assignment
elementary programming
Chapter 2: Introduction to C++.
Unit 3: Variables in Java
Chapter 2 Variables.
Presentation transcript:

Primitive Types and Expressions

Variables Named containers used to store data such as numbers and letters. The number, letter, or other data item in a variable is called its value. The values held by variables can be changed any number of times while the program is being run. Example: numberOfChildren = 6;

Primitive Variable Types In order to run a program information must be given that tells the computer how much space is required to store each variable and how the variable is to be represented. This information can be determined if the type of the variable is known. Primitive types include int (for integers), char (for characters), and double (for real numbers)

Declaring Variables In Java the type of each variable must be declared before it is used. A declaration statement is used for this purpose. It has the form: Type Variable_1, Variable_2, . . . ; Examples: int styleNumber, numberOfChecks; char answer; double amount, interestRate;

Java Identifiers The technical term for a name in a programming language, like the name of a variable, is an identifier. An identifier can contain letters, digits, and the underscore character (_), but cannot start with a digit. (No spaces, or other characters like dots (.) can be used. There is no limit to the length of an identifier.

Java Identifiers (cont’d) Java is case sensitive, so the names mystuff, myStuff, and MyStuff are all different. By convention identifiers for primitive types begin with lowercase letters. Also, by convention, in multiword identifiers, the first letter of each word after the first is capitalized, e.g., theTimeOfDay, hotCar, totalNumberOfEggs, etc.

Java Identifiers (cont’d) Some names which are “keywords” in Java like “public”, “class”, “static” cannot be used as identifiers for variables. Some names like “main” or “println” are not reserved keywords but they have predefined meanings. Although technically you can use these names and thereby change their meaning, it is not usually a good idea to do so.

Primitive Types Type Name Kind of Value Memory Used byte integer short 2 bytes Int 4 bytes long 8 bytes float floating-point double char single character boolean true or false 1 bit

Assignment Statements The most direct way to give a variable a value is to use an assignment statement. For example, it answer is a variable of type int and you want to assign it the value of 42, you can write answer = 42; The equal sign, =, is called the assignment operator. It doesn’t really mean equal in the mathematical sense.

Examples of Assignment Statements amount = 3.99; firstInitial = ‘B’; score = numberOfCards + handicap; eggsPerBasket = eggsPerBasket – 2; The statement means: compute the value of the expression to the right of the equal sign and replace the currently stored value of the variable to the left of the equal sign with this new value.

Specialized Assignment Operators The simple assignment operator (=) can be combined with an arithmetic operator like +, -, or * (times). For example amount += 5; means amount = amount + 5; and amount *= 25; means amount = amount*25

Simple Screen Output System.out is an object and println is a method of this object that sends output to the screen. System.out.println(amount + “ is the cost.”); outputs the value of the variable amount (to the screen) followed by the phrase “ is the cost.” Notice that the plus sign here means to append the phrase to the amount. It is not an arithmetic operator.

Simple Input In order to do keyboard input we must put the following statement at the beginning of the file: import java.util.*; This line tells the Java compiler where to find the definition of the Scanner class that we use for keyboard input. The following line sets things up so that data can be entered form the keyboard: Scanner keyboard = new Scanner(System.in);

Simple Input (cont’d) The statement: eggsPerBasket = keyboard.nextInt(); reads one whole number from the keyboard. When entering numbers at the keyboard, the user must either place numbers on separate lines or else separate multiple numbers by one or more spaces.

Constants Character constants are written by placing a single quote mark before and after the character, e.g., ‘B’, ‘#’, ‘z’, etc. Integer constants are written as integers are normally written. They may be preceded with a + or - sign, but they cannot contain a decimal point. Examples are: 77, -5, or +98, but not 4. or 88.0

Constants (cont’d) Numbers with decimal points are called floating point numbers and can be written as a series of digits followed by a decimal point and followed by another series of digits, e.g. 3.24159, .0001, 5., or 0.1234 They can also be written in scientific notation using the letter e to denote the exponent, e.g. 8.654e2 (which is the same as 865.4

Assignment Compatibilities You cannot store a value of one type into a variable of another type unless it is first converted to the same type as that of the variable. Sometimes this conversion is performed automatically by Java. The conversion is automatic as long as the variable receiving the value is further down the following list than the value is: byteshortintlongfloatdouble

Type Casting This is a way of converting a value of one type to another type. The code double distance; distance = 9.0 int points; points = distance; would result in an error

Type Casting (cont’d) However, the code double distance; distance = 9.0 int points; points = (int)distance; would be OK. In this case distance is converted to an integer before it is placed in points. However, the value is truncated not rounded.

Arithmetic Operators In Java you can form arithmetic expressions involving addition (+), subtraction (-), multiplication (*), and division (/). These operators can be used will all integer and floating point types of variables. The result of an expression involving mixed type is the type of the variable farthest to the right on the list: byteshortintlongfloatdouble

The % Operator In Java the result of 9.0/2 is 4.5 but the result of 9/2 is 4 The % operator can be used to capture the remainder of the integer division operation. That is, the result of 9%2 is 1

Parentheses and Precedence Rules Parentheses can be used to group items in arithmetic expressions to indicate the order in which the operations are to be performed. For example, (cost + tax) * discount cost + (tax * discount)

Parentheses and Precedence Rules (cont’d) Without parentheses, precedence rules apply. Highest Precedence First: the unary operators: +, -, ++, --, and ! Second: the binary arithmetic operators: *, /, and % Third: the binary arithmetic operators: + and – Lowest Precedence

Increment and Decrement Operators Increment operator (++) count++; is the same as count = count + 1; Decrement operator (--) count--; is the same as count = count – 1;