IDENTIFIERS CSC 111.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

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,
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.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
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.
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.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Variables, Constants and Built-in Data Types Chapter 2: Java Fundamentals.
String Escape Sequences
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
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 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
INTRODUCTION TO JAVA CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
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
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
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.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
CPS120: Introduction to Computer Science Variables and Constants.
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.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
© 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.
Bill Tucker Austin Community College COSC 1315
Chapter # 2 Part 2 Programs And data
Egyptian Language School Computer Department
Chapter 2 Variables.
Chapter 2 Basic Computation
Java Variables and Types
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
Variables and Primative Types
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
OUTPUT STATEMENTS GC 201.
INPUT STATEMENTS GC 201.
Escape Sequences What if we wanted to print the quote character?
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Fundamentals 2.
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter # 2 Part 2 Programs And data
elementary programming
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
Unit 3: Variables in Java
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

IDENTIFIERS CSC 111

Outline 1. Identifiers 2. Memory Space 3. Data Types 4.Data state 5. Declaration 5.1 Constants 5.2 Variables 6. Examples

In our course, reserved words are written in light blue 1. IDENTIFIERS Identifiers are names of things such as: Methods: a set of processing operations Classes Rules for identifiers’ names include: May consist only of: Letters (a – z or A – Z), Digits (0 – 9), Underscore (_), Dollar sign ($) Should NOT begin with a digit Not a reserved word: These are some words used in the Java language. They are interpreted by the compiler to do a specific thing. Examples of reserved words include: public, class, void, etc… In our course, reserved words are written in light blue Identifier names are case sensitive: number, Number, NUMBER represent three different identifiers.

EXAMPLES OF IDENTIFIERS’ NAMES The following identifiers’ names are valid: First payRate $Amount employee_salary _Update The following identifiers’ names are NOT valid (illegal): mid-term - is an illegal character add salary space is an illegal character one+two + is an illegal character 2nd must NOT begin with a digit public Reserved word

Remember when we said that input need to be saved Programs and Data Remember when we said that input need to be saved Keyboard Screen Processing input data output 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 : name for that space Data Type : Specifies how much space to store in memory State : is it variable ? or Constant

1-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. 

Identifier Conventions in Java 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.

2.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.

Java built-in Data Types

PRIMITIVE DATA TYPES (1) - Integers Java categorizes integer data into the following primitive types: Type Size Min. Value Max. Value byte 8 bits - 128 = - 27 + 127 = 27 -1 short 16 bits - 32,768 = - 215 + 32,767 = 215 - 1 int 32 bits - 2,147,483,648 = - 231 +2,147,483,648 = 231 - 1 long 64 bits -9,223,372,036,854,775,808 = - 263 + 9,223,372,036,854,775,808 = 263 - 1 All above types store numbers with no decimal point: integers. Positive integers do not require a + sign in front of them No commas are allowed within integers Note that larger size implies lower minimum and higher maximum Words in blue are reserved words

4. DATA TYPES PRIMITIVE DATA TYPES (2) – Decimals Java categorizes decimal (or real) data into the following primitive types: Type Size Min. Value Max. Value Number of Significant bits float 32 bits - 3.4e+38 + 3.4e+38 Up to 7 after the decimal point (Single precision) double 64 bits - 1.7e+308 + 1.7e+308 Up to 15 after the decimal point (Double precision) All above types store numbers with decimal point: floating-point. In Java, real numbers are represented using the floating-point notation: Number Scientific Notation Floating-point Notation 4387 4.387 * 103 4.387e+3 438791 4.38791 * 105 4.38791e+5 0.0005 5.0 * 10-4 5.0e-4 0.0000265 2.65 * 10-5 2.65e-5

PRIMITIVE DATA TYPES (3) – Character Size Min. Value Max. Value Description char 16 bits 65,535 = 216 - 1 Stores the Unicode of single characters Any key on the keyboard is represented by a char data type Values of char type are enclosed between single quotes such as: ‘A’ ‘a’ ‘%’ ‘$’ ‘*’ ‘&’ ‘7’ ‘ ’ The following values can NOT be represented by a char type: ‘Abc’ ‘>=’ ‘Fatma’  these are rather string types (non-primitive). Note that the space may be represented as a char type Java uses the Unicode coding system to represent characters in memory. Each character has a unique code. There are 65,535 unique codes. For example, the value 65 corresponds to the letter ‘A’; the value 97 represents ‘a’; and so on. The table in the next slide represents all letters codes. In our course, space character is represented by the letter ‘~’

4. DATA TYPES

PRIMITIVE DATA TYPES (4) – Boolean Size Min. Value Max. Value Description boolean 1 bit 1 Stores either true or false. The boolean data type manipulates logical expressions that evaluate to either true or false.

Example of DATA TYPES Different programs manipulate different data An employee payroll program paycheck manipulates data such as: Number of hours (a fraction number) Pay rate (a fraction number) Marital status (a character) Number of dependents (a whole number) A Registrar system in a university manipulates data such as: GPA (a fraction number) Semester’s load (a whole number) Different data types manipulate different operations Numeric data are added, subtracted, multiplied, etc… Character data are sorted, concatenated, etc…

5. DECLARATION Declaration allocates appropriate memory space to identifiers based on their types. Any identifier must be declared before being used in the program. 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.

5.1 Constant Declaration final dataType constIdentifier = literal | expression; final double PI = 3.14159; 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; The reserved word final is used to declare constants. These are constants, also called named constant. These are called literals. This is called expression.

5.2 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;

5.2 VARIABLES DECLARATION In Java, double is the default type of a floating-point number. When using float literals, the number should be written as shown below; otherwise, the compiler would give an error message (syntax error) : Example 5 float x=5.33f; float length=12.33f, width= 6.333f, radius=0.3f;

6. EXAMPLES – PROGRAM 1 Program Output: // This example illustrates data declaration & manipulation // program to calculate area of circule // import necessary libraries public class dataManipulation { static final double PI = 3.14159;//constant declaration public static void main (String[] args) // Declaration section: to declare needed variables double radius= 2.5, area; // Input section: to enter values of used variables // Processing section: processing statements area = PI * radius * radius; // Output section: display program output System.out.println (“The area of the circle of radius “ + radius + “ is “ + area); } // end main } // end class 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Program Output: The area of the circle of radius 2.5 is 19.6349375 1

Display the value of a variable 6. EXAMPLES – PROGRAM 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // This example illustrates data declaration & manipulation // import necessary libraries public class dataManipulation { public static void main (String[] args) // Declaration section: to declare needed variables int num1= 10, num2 = num1 - 1; double sale = 0.02 * num1; char first; // Input section: to enter values of used variables // Processing section: processing statements first = ‘D’; // Output section: display program output System.out.println (“num1= “ + num1);//line output 1 System.out.println (“num2= “ + num2);//line output 2 System.out.println (“sale= “ + sale); //line output 3 System.out.println (“first= “ + first); //line output 4 } // end main } // end class Print statement either display a text as it is inside double quotation “ “ OR Display the value of a variable num1= 10 num2= 9 sale= 0.2 first= D 1 2 3 4

6. EXAMPLE IMPORTANT NOTES ANY VARIABLE MUST BE DECLARED BEFORE BEING USED VARIABLES ON THE RIGHT HAND SIDE OF AN EQUATION SHOULD ALREADY HAVE VALUES ALSO, VARIABLES THAT ARE TO BE PRINTED SHOULD ALREADY HAVE VALUES VARIABLES GET VALUES EITHER BY: 1) INITIALIZATION, 2) CALCULATION, 3) INPUT FROM THE USER

Self-Check Exercises (1) Which of the following identifiers are illegal? Explain why: God Father &currency final 901 4ever Write a program that converts from ⁰C to ⁰F. Write a program that adds two numbers. Write a program that calculates the average of three numbers. W2.2 Identifiers

Self-Check Exercises (2) Detect the errors in the following program: public class FindError { static final CENTIMETERS_PER_INCH = 2.54; public static void main (String[] args) double inches; cm = CENTIMETERS_per_INCH * inches; System.out.println (“There are “ + cm + “cm in “ + inches + “inches”); } 1 2 3 4 5 6 7 8 9 10 11 W2.2 Identifiers