School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.

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

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 09 – Programming with Java Datatypes and Flow Control.
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.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Introduction to Computers and Programming Lecture 7:
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
String Escape Sequences
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.
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.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Lecture #5 Introduction to C++
OOP with Java, David J. Barnes Adding Sequential Behavior1 Adding Behavior Previous class definitions have passively maintained attributes. Active behavior.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Introduction to Java Java Translation Program Structure
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.
Copyright © – Curt Hill Types What they do.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Final Jeopardy Fundamen tal Java Numerical Data type Boolean Expressi on If/THEN/ WHILE Miscellan eous
COMP Primitive and Class Types Yi Hong May 14, 2015.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
1.2 Primitive Data Types and Variables
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 06 – Java Datatypes Webpage:
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
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 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 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.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Multiple variables can be created in one declaration
Computer Science 3 Hobart College
Type Conversion, Constants, and the String Object
Chapter 2.
Introduction to Programming in Java
5 Variables, Data Types.
Computers & Programming Languages
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Introduction to Primitive Data types
The System.exit() Method
Java Programming Review 1
Primitive Types and Expressions
Unit 3: Variables in Java
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Variables and Constants
Introduction to Primitive Data types
Presentation transcript:

School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making

2 Variables Symbolic representation of data of a specific type Symbolic representation of data of a specific type Variables are named by an identifier. Variables are named by an identifier. Type must be declared before a variable can be used Type must be declared before a variable can be used e.g. int a e.g. int a Values can be assigned to a variable Values can be assigned to a variable Java assignment is = Java assignment is = eg a = b; eg a = b; Variables can be modified during a programs execution (usually by assignment). Variables can be modified during a programs execution (usually by assignment). 3.3

3 Major Java Variable Types Numeric Numeric byte - 8 bits signed integer (ie from -128 to 127) byte - 8 bits signed integer (ie from -128 to 127) short - 16 bits signed integer (ie from ) short - 16 bits signed integer (ie from ) int - 32 bits signed integer int - 32 bits signed integer long - 64 bits signed integer long - 64 bits signed integer float - 32 bit floating point float - 32 bit floating point double - 64 bit floating point (double precision) double - 64 bit floating point (double precision) Character Character char - single character char - single character String - text (any length) bounded by double quotes String - text (any length) bounded by double quotes Boolean Boolean boolean - true or false boolean - true or false User Defined User Defined

4 Converting Types Types often need to be converted. Types often need to be converted. Examples: Examples: Integer  Real Integer  Real Real  Integer Real  Integer String  Real eg "3.14"  3.14 String  Real eg "3.14"  3.14 Real  String Real  String

5 Converting Types (2) Library methods Library methods Casting a = (int) b This forces b to be treated as an int. Casting a = (int) b This forces b to be treated as an int. Syntactically any type can be cast into any other type. Syntactically any type can be cast into any other type. Be careful that it makes sense! Be careful that it makes sense! If it does not, then the compiler might generate an error! If it does not, then the compiler might generate an error! Information that does not exist in the target type is lost. Information that does not exist in the target type is lost.

6 Decisions Bank Account Bank Account Withdrawal - subtract sum from balance. Withdrawal - subtract sum from balance. Deposit - add sum to balance. Deposit - add sum to balance. A decision is required if deposit then add sum to balance else subtract sum from balance A decision is required if deposit then add sum to balance else subtract sum from balance This is one example of a control structure This is one example of a control structure

7 If Statement This statement requires a boolean expression as part of its code. This statement requires a boolean expression as part of its code. For example compare numeric variables a and b. For example compare numeric variables a and b. if (a > b) {......} if (a > b | b == 0) {......} if (a > b) { } else {......}

8 Relational Operators > greater than > greater than < less than < less than == is equal to == is equal to != is not equal to != is not equal to >= greater or equal to >= greater or equal to <= less or equal to <= less or equal to | or | or & and & and