Variables, Constants and Built-in Data Types Chapter 2: Java Fundamentals.

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
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.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
String Escape Sequences
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
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.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
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.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
Chapter 2 Elementary Programming
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Primitive Variables.
Copyright Curt Hill Variables What are they? Why do we need them?
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Chapter 2. Variable and Data type
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.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
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.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Fundamentals 2.
Chapter # 2 Part 2 Programs And data
Topic 2 Elementary Programming
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Escape Sequences What if we wanted to print the quote character?
IDENTIFIERS CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Escape Sequences Some Java escape sequences: See Roses.java (page 68)
Fundamentals 2.
Chapter 2: Java Fundamentals
Storing Information Each memory cell stores a set number of bits (usually 8 bits, or one byte) (byte addressable)
Chapter # 2 Part 2 Programs And data
Chapter 2: Java Fundamentals
Primitive Types and Expressions
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Primitive Data Types and Operations
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Presentation transcript:

Variables, Constants and Built-in Data Types Chapter 2: Java Fundamentals

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Objectives Discovering what is a variable Discovering what is a data type Learning about the basic data types Constants and variables identifiers Get acquainted with how to select proper types for numerical data. Write arithmetic expressions in Java.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 3 Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in the computer's memory: space memory. A space memory has three characteristics Identifier Data Type State KeyboardScreen Processing input data output data

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 4 State of the Space Memory The state of the space memory is the current value (data) stored in the space memory. The state of the space memory: May be changed. –In this case the space memory is called variable. Cannot be changed. –In this case the space memory is called constant.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 5 Space Memory Identifier Identifier is a sequence of characters that denotes the name of the space memory to be used. This name is unique within a program. Identifier Rules –It cannot begin with a digit (0 – 9). –It may contain the letters a to z, A to Z, the digits 0 to 9, and the underscore symbol, _. –No spaces or punctuation, except the underscore symbol, _, are allowed.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 6 Constants: All uppercase, separating words within a multiword identifier with the underscore symbol, _. Variables All lowercase. Capitalizing the first letter of each word in a multiword identifier, except for the first word. Identifier Conventions in Java

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 7 Identifiers are Case-Sensitive Identifiers in Java are case-sensitive. Thus, the identifiers myNumber and mynumber, are seen as two different identifiers by the compiler.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 8 Data Type The data type defines what kinds of values a space memory is allowed to store. All values stored in the same space memory should be of the same data type. All constants and variables used in a Java program must be defined prior to their use in the program.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 9 Java built-in Data Types

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 10 Primitive Data Types Type Size (bits) RangeDescription booleantrue, falseStores a value that is either true or false. char16Stores a single 16-bit Unicode character. byte8-128 to +127Stores an integer. short to Stores an integer. int32 bits-2,147,483,648 to +2,147,483,647 Stores an integer. long64 bits-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 Stores an integer. float32 bitsaccurate to 8 significant digitsStores a single-precision floating point number. double64 bitsaccurate to 16 significant digitsStores a double-precision floating point number.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 11 Variable/Constant Declaration When the declaration is made, memory space is allocated to store the values of the declared variable or constant. The declaration of a variable means allocating a space memory which state (value) may change. The declaration of a constant means allocating a space memory which state (value) cannot change.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 12 Constant Declaration final dataType constIdentifier = literal | expression; final double PI = ; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060; final int MAX = 1024; final int MIN = 128; final int AVG = (MAX + MIN) / 2; These are constants, also called named constant. The reserved word final is used to declare constants. These are called literals. This is called expression.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 13 Variable Declaration A variable may be declared: –With initial value. –Without initial value. Variable declaration with initial value; dataType variableIdentifier = literal | expression; double avg = 0.0; int i = 1; int x =5, y = 7, z = (x+y)*3; Variable declaration without initial value; dataType variableIdentifier; double avg; int i;