Identifiers, Variables and the JDK - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Identifiers, Variables and the JDK

Description:

Every language has a set of identifiers which represent some ... Alphanumeric. Characters and Strings. Logical. Boolean that is True or False. Abstract ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 23
Provided by: Pet126
Category:

less

Transcript and Presenter's Notes

Title: Identifiers, Variables and the JDK


1
Identifiers, Variables and the JDK
  • Introduction to Software Development
  • Week 1 Lecture 2

2
Objectives
  • In this lecture, we will
  • Introduce identifiers and variables
  • Compare assignment and initialisation
  • Use variables in a Java application
  • Discuss downloading the Java Development Kit

3
Identifiers
  • These are the human readable words or characters
    used in programs
  • Every language has a set of identifiers which
    represent some predefined aspect of the language
  • These are called keywords
  • For example the following are keywords in Java
  • class
  • public
  • static
  • void
  • In Java identifiers are case sensitive

4
Identifiers
  • Most languages have rules and recommendations
    about what is or is not valid identifier form
  • Rules as to which characters are allowed at the
    start of an identifier
  • Limitations on the length of the identifier
  • How you should represent different entities such
    as variable types
  • Spaces are not permitted in identifier names in
    most programming languages
  • User specified identifiers should be meaningful

5
Identifiers in Java
  • In Java user specified identifiers
  • May be any length
  • Must start with a letter, underscore (_), or a
    sign
  • The remaining characters that make up the
    identifier may be most characters other than
    those used as operators such as - / lt gt
  • Cannot contain spaces or tabs
  • Cannot be keywords
  • The following conventions are used in naming
    identifiers in Java
  • The first letter should be lowercase
  • Multiple words are concurrent, not separated with
    underscores
  • Each word after the first should be capitalised

6
Variables and Constants
  • Variables and constants are used to represent
    information and data in programs
  • A variable is a value in a program which may
    change during the lifetime of the program
  • A constant is a value in a program which does not
    change during the lifetime of the program or
    during the execution of a particular algorithm

7
Data types
  • Most programming languages allow the type of data
    being processed to be specified
  • Four main data type categories are
  • Numeric
  • Integer and Real
  • Alphanumeric
  • Characters and Strings
  • Logical
  • Boolean that is True or False
  • Abstract
  • Stack
  • Every data type has a set of symbols which can be
    applied to variables or constants of that type

8
Variables in Java
  • A variable in Java can be declared as follows
  • int num1
  • The declaration involves two parts a data type
    and an identifier
  • In this example the data type is int
  • The variable is associated with enough memory to
    hold a value of type integer
  • Java supports several data types these will be
    discussed in detail next week
  • The identifier is num1
  • In Java variables can be declared almost anywhere
  • They must be declared before they are used

9
Using Variables
  • // Addition Java Application
  • public class Add
  • public static void main (String args)
  • System.out.println ("This program adds three
    numbers")
  • int num1, num2, num3
  • // input has not been covered yet so the values
    of the
  • // numbers are set directly in the program
  • num1 10
  • num2 20
  • num3 30

10
Using Variables (continued)
  • // calculate the sum of the three numbers
  • int sum
  • sum num1 num2 num3
  • // output the results
  • System.out.println ("The sum is " sum)

11
Assignment and Initialisation
  • The operation of storing some data in a variable
    is referred to as assignment
  • int num1 num1 10
  • The variable num1 is assigned the value 10
  • What value is in the variable num1 initially?
  • It is good practice to assign an initial value to
    a variable
  • The initial value can be given to the variable as
    it is declared using initialisation
  • int num1 10
  • The symbol is used for two distinct operations
  • Assignment and Initialisation

12
Operators
  • The symbol seen earlier is an example of an
    operator
  • It is a binary operator
  • The operation is carried out on two operands
  • In the case num1 num2 the two operands are
    num1 and num2
  • Java has many operators including - /
  • The minus operator can be used as a binary
    operator or a unary operator
  • num1 num2 a binary use
  • - num2 a unary use
  • Operators will be discussed in detail next week

13
Working with Real Numbers
  • Another data type provided by Java is double
  • A variable of type double can hold a real number
  • double dNum
  • dNum 17.5
  • An integer can be assigned to a double variable
  • dNum 10
  • int num1 7
  • dNum num1
  • The integer is promoted to a double
  • A double CANNOT be assigned to an integer
    variable without loss of accuracy and so fails to
    compile

14
Code Fragment
  • int num1 5, num2 7, num3 20
  • double avg
  • avg (num1 num2 num3) / 3.0
  • Note the use of 3.0 rather than 3
  • What is the difference?
  • The literal 3.0 is a real number but the literal
    3 is an integer
  • If the two operands of the division operator /
    are integers then integer division takes place
  • If the two operands are doubles then real
    arithmetic is used

15
Java Development Kit
  • All the software required to develop Java
    applications is available from Sun
  • Visit the Sun Java website
  • http//java.sun.com
  • http//java.sun.com/j2se/1.5.0/download.jsp
  • In this module the Java Development Kit is used
  • Java 1.5
  • Versions are available for Windows and Linux

16
NetBeans
  • NetBeans is an integrated Java development
    environment
  • The latest version can be downloaded from Sun
    along with JDK 1.5

17
Installing
  • Installation is straightforward
  • Once the download is complete execute the
    installation file and follow the instructions
  • The default location for the files created during
    installation will be a folder called Java
  • The folder should be in the program files
    directory
  • In order to make using the JDK as simple as
    possible it is useful to set a couple of
    environment variables

18
Environment Variables
  • First launch Settings Control Panel System
  • Then select Environment Variables on the Advanced
    tab

19
Environment Variables
  • The following screen is displayed
  • Three System Variables can be added or edited if
    they already exist
  • PATH
  • JAVA_HOME
  • CLASSPATH

20
Environment Variables
  • PATH
  • Add an entry to the Path identifying where to
    find the bin folder of JDK1.5
  • JAVA_HOME
  • Set this to the JDK1.5 folder
  • CLASSPATH
  • Add . and the JDK1.5 bin folder

21
Editors
  • Notepad is a simple editor to use but it provides
    no support for Java
  • Many other editors are available that make
    entering Java applications easier providing
    facilities such as
  • Highlighting keywords
  • Automatic indenting
  • There are numerous websites where free editors
    can be found and downloaded
  • For example
  • http//java.about.com

22
Summary
  • In this lecture we have
  • Introduced identifiers and variables
  • Compared assignment and initialisation
  • Used variables in a Java application
  • Discussed downloading the Java Development Kit
Write a Comment
User Comments (0)
About PowerShow.com