DECLARING AND USING
IDENTIFIERS, VARIABLES AND
CONSTANT
Chapter 3.1:
 Name of things such as variables,
constants, name of classes and
methods, that appears in programs.
 Example:
 Variable: num1
 Constant: PIE
 Method: countNum()
Identifiers
Java’s Rules for Identifiers
 Can be made of ONLY letters, digits, (_)
and $.
 Cannot start with a digit.
 Spaces are not permitted inside identifiers.
 Cannot use reserved words.
 e.g: int,public, throws,return
 An identifier cannot be true, false, or
null.
 They are case sensitive.
 An identifier can be of any length.
Variables
 A variable is a name for a location in memory that
holds a value.
 Is used to store data input, data output, or
intermediate data
 Representing data of a certain type
 Content may change during program execution.
 Must be declared before it can be used.
 May not be automatically initialized.
 If new value is assigned, old one is destroyed.
 Value can only be changed by an assignment
statement or an input (read) statement.
Java Keywords
 abstract
 assert
 boolean
 break
 byte
 case
 catch
 char
 class
 continue
 default
 do
 super
 switch
 synchronized
 this
 throws
 transient
 try
 void
 volatile
 while
• double
• else
• enum
• extends
• for
• final
• finally
• float
• if
• implements
• import
• instanceof
• int
• interface
• long
• native
• new
• package
• private
• protected
• public
• return
• short
• static
• strictfp*
Declaring Variables
Syntax: datatype variableName
Example:
int i,j; //Declare i and j to
//be an integer variable;
double sale; //Declare sale to
// be a double variable;
char first; // Declare first to be a
// character variable;
String str; // Declare str to be a
//String variable;
Declaring Variables
 A variable declaration specifies the variable's
name and the type of information that it will hold
int total;
int count, temp, result;
Multiple variables can be created in one declaration
data type variable name
Variable Initialization
 A variable can be given an initial value in the
declaration
int sum = 0;
int base = 32, max = 149;
 When a variable is referenced in a program, its
current value is used
 See PianoKeys.java
Variable Initialization
//********************************************************************
// PianoKeys.java Author: Lewis/Loftus
//
// Demonstrates the declaration, initialization, and use of an
// integer variable.
//********************************************************************
public class PianoKeys
{
//-----------------------------------------------------------------
// Prints the number of keys on a piano.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int keys = 88;
System.out.println ("A piano has " + keys + " keys.");
}
}
Variable Initialization
//********************************************************************
// PianoKeys.java Author: Lewis/Loftus
//
// Demonstrates the declaration, initialization, and use of an
// integer variable.
//********************************************************************
public class PianoKeys
{
//-----------------------------------------------------------------
// Prints the number of keys on a piano.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int keys = 88;
System.out.println ("A piano has " + keys + " keys.");
}
}
Output
A piano has 88 keys.

Chapter 3.1

  • 1.
    DECLARING AND USING IDENTIFIERS,VARIABLES AND CONSTANT Chapter 3.1:
  • 2.
     Name ofthings such as variables, constants, name of classes and methods, that appears in programs.  Example:  Variable: num1  Constant: PIE  Method: countNum() Identifiers
  • 3.
    Java’s Rules forIdentifiers  Can be made of ONLY letters, digits, (_) and $.  Cannot start with a digit.  Spaces are not permitted inside identifiers.  Cannot use reserved words.  e.g: int,public, throws,return  An identifier cannot be true, false, or null.  They are case sensitive.  An identifier can be of any length.
  • 4.
    Variables  A variableis a name for a location in memory that holds a value.  Is used to store data input, data output, or intermediate data  Representing data of a certain type  Content may change during program execution.  Must be declared before it can be used.  May not be automatically initialized.  If new value is assigned, old one is destroyed.  Value can only be changed by an assignment statement or an input (read) statement.
  • 5.
    Java Keywords  abstract assert  boolean  break  byte  case  catch  char  class  continue  default  do  super  switch  synchronized  this  throws  transient  try  void  volatile  while • double • else • enum • extends • for • final • finally • float • if • implements • import • instanceof • int • interface • long • native • new • package • private • protected • public • return • short • static • strictfp*
  • 6.
    Declaring Variables Syntax: datatypevariableName Example: int i,j; //Declare i and j to //be an integer variable; double sale; //Declare sale to // be a double variable; char first; // Declare first to be a // character variable; String str; // Declare str to be a //String variable;
  • 7.
    Declaring Variables  Avariable declaration specifies the variable's name and the type of information that it will hold int total; int count, temp, result; Multiple variables can be created in one declaration data type variable name
  • 8.
    Variable Initialization  Avariable can be given an initial value in the declaration int sum = 0; int base = 32, max = 149;  When a variable is referenced in a program, its current value is used  See PianoKeys.java
  • 9.
    Variable Initialization //******************************************************************** // PianoKeys.javaAuthor: Lewis/Loftus // // Demonstrates the declaration, initialization, and use of an // integer variable. //******************************************************************** public class PianoKeys { //----------------------------------------------------------------- // Prints the number of keys on a piano. //----------------------------------------------------------------- public static void main (String[] args) { int keys = 88; System.out.println ("A piano has " + keys + " keys."); } }
  • 10.
    Variable Initialization //******************************************************************** // PianoKeys.javaAuthor: Lewis/Loftus // // Demonstrates the declaration, initialization, and use of an // integer variable. //******************************************************************** public class PianoKeys { //----------------------------------------------------------------- // Prints the number of keys on a piano. //----------------------------------------------------------------- public static void main (String[] args) { int keys = 88; System.out.println ("A piano has " + keys + " keys."); } } Output A piano has 88 keys.