Data Types and Operators  Two category of data types:  Primitive type: value type. Store values.  Object type: reference type. Store addresses.

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

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
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.
Javascript Essentials How do I write it??  Start Homesite  Between the start and end BODY tags type: 
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 7:
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
1 Chapter 2: Elementary Programming Shahriar Hossain.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
LESSON 6 – Arithmetic Operators
Chapter 2 Elementary Programming
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
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.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Data Types Declarations Expressions Data storage C++ Basics.
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Recap……Last Time [Variables, Data Types and Constants]
1.2 Primitive Data Types and Variables
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
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.
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.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2: Basic Elements of C++
Unit 2 Elementary Programming
Lecture 3: Operators, Expressions and Type Conversion
Multiple variables can be created in one declaration
Identifiers - symbolic names
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Elementary Programming
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Unit-2 Objects and Classes
Computers & Programming Languages
Numerical Data Types.
C++ Data Types Data Type
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
elementary programming
Assignment Operators Topics Increment and Decrement Operators
CS2011 Introduction to Programming I Elementary Programming
Java Programming Language
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Presentation transcript:

Data Types and Operators  Two category of data types:  Primitive type: value type. Store values.  Object type: reference type. Store addresses.

Data Types and Operators  Primitive types:  int  double  String (?)  boolean

Data Types and Operators  Operators  Numerical operators: + - * / %  Relational operators: == != > >= < <=  String cancatenation: + Examples: “ABC” + “DEF”  “ABCDEF” “The average is “ + average

Data Types and Operators  Short cuts:  a = a + b;  a += b;  a = a – b;  a -= b;  a = a * b;  a *= b;  a = a / b;  a /= b;  a = a % b;  a %= b;

Data Types and Operators  Increment and decrement  Increment ++: increase … by 1 int a=3; a++  the value of a is 4 ++a  the value of a is 4

Data Types and Operators  Increment and decrement  Decrement -- : decrease … by 1 int a=5; a--  the value of a is 4 --a  the value of a is 4

Data Types and Operators  Increment and decrement  pre and post ++a: preincrement a++: postincrement --a: predecrement a--: postdecrement

Data Types and Operators  Increment and decrement  Stand-alone: no difference int a = 10; ++a and a++  a has a new value a and a--  a has a new value 9.

Data Types and Operators  Increment and decrement  In expression evaluation: different  pre --- increase the value by 1 before evaluating the expression  post --- evaluate the expression before increasing the value by 1

Data Types and Operators  Increment and decrement  Examples: int a=10; int b; b = ++a; /* now a = 11, b=11 */ b = a++; /* now a = 11, b=10 */

Data Types and Operators  Increment and decrement  Examples: int a=10; int b; b = ++a + 4; /* now a = 11, b=15 */ b = a++ + 4; /* now a = 11, b=14 */

Data Types and Operators  char and String types:  Examples: ‘A’, ‘B’,…, ‘a’, ‘b’, ‘$’, ‘*’,… are all constants (literals) of char char a=‘A’; int i = (int)’A’; int j = ‘H’ + ‘e’ + ‘l’ + ‘l’ +’o’;

Data Types and Operators  char and String types:  char is a single char, is an integer type in many languages.  Enclosed in a pair of single quotes.  Its value is the ASCII value.

Data Types and Operators  char and String types:  String contains zero or many chars  Enclosed in a pair of double quotes.  char and String are totally different types. A String has no numerical values.  Operator on String: +

Data Types and Operators  cast:  byte  short  int  long  float  double: precision increases  Assigning a value of high precision to a low precision variable loses precision, and needs cast

Data Types and Operators  cast:  var = (type)expr examples: int a = 10; double d=3.14; d = a; /* ok */ a = d; /* error */ a = (int)d; /* ok with cast */

Programming Problems 1. Write a program that reads the radius (double) of a circle from the console, computes the area of the circle and displays the result on the console. 2. Write a program to calculate and display the total restaurant bill after reading the amount of food and drink, tax rate, and tip percentage from the console. The display should itemize different items.

Programming Problems 3. Write a program reads an integer between 0 and 999 and calculates the sum of the digits. 4. Write a program to calculate the net pay after reading gloss pay, fed and state tax rates from the console. Programs 3 and homework, send the source as an to as an attachment.