You are on page 1of 32

Using Data

Instructor: Mr. Neil A. Basabe, MIT


Rules for defining Java Identifiers

1. The only allowed characters for identifiers are all alphanumeric


characters([A-Z],[a-z],[0-9]), ‘$‘(dollar sign) and ‘_‘ (underscore).For
example “geek@” is not a valid java identifier as it contain ‘@’ special
character.

2. Identifiers should not start with digits([0-9]). For example “123geeks”


is a not a valid java identifier.

3. Java identifiers are case-sensitive.

4. There is no limit on the length of the identifier but it is advisable to


use an optimum length of 4 – 15 letters only.

5. Reserved Words can’t be used as an identifier. For example “int


while = 20;” is an invalid statement as while is a reserved word. There
are 53 reserved words in Java.
Upper Camel case or Pascal Case
- each word or abbreviation begins with a capital letter
- use for classes and interfaces

Camel Case
- the first word is in small letters, next word or abbreviation
begins with a capital letter
- use for methods, variables, objects

Upper Case
- use for constants

Lower Case
- use for packages
Declaring and Using Constants
and Variables
• Constant
Data type
– Cannot be changed while A type of data that can be
program is running stored
• Literal constant How much memory an item
– Value taken literally at each use occupies
What types of operations
• Numeric constant can be performed on data
– As opposed to a literal constant
• Unnamed constant Primitive type
A simple data type
– No identifier is associated with it
• Variable Reference types
– A named memory location More complex data types
– Used to store a value
– Can hold only one value at a time
– Its value can change
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Java Basics
Declaring Named Constants
A named constant:
• Should not change during program execution
• Has a data type, name, and value
• Has a data type preceded by the keyword final
• Can be assigned a value only once
• Conventionally is given identifiers using all uppercase
letters

Reasons for using named constants:


• Make programs easier to read and understand
• Enable you to change a value at one location within a
program
• Reduce typographical errors
• Stand out as separate from variables
• Eliminates magic numbers
Java Basics
Programming with objects and classes
Programming with objects and classes
Class Variables, Constants, and Methods

An instance variable is tied to a specific instance of the


class; it is not shared among objects of the same class.
For example, suppose that you create the following
objects:
Circle circle1 = new Circle();
Circle circle2 = new Circle();

The radius in circle1 is independent of the radius in


circle2. Changes made to circle1’s radius do not affect
circle2’s radius, and vice versa.
If you want all the instances of a class to share data,
use class variables. Class variables store values for the
variables in a common memory location. Because of this
common location, all objects of the same class are
affected if one object changes the value of a class
variable.

To declare a class variable, put the modifier static in


the variable declaration. Suppose you want to track the
number of objects of the Circle class created, you can
define the class variable as follows:

static int numOfObjects;


To declare a class constant, add the final keyword in the
declaration. For example:
public final static double PI = 3.1416;

Instance methods belong to instances and can only be applied


after the instances are created. They are called by the following:
objectName.methodName();

The methods defined in the Circle class are instance methods.


Java supports class methods as well as class variables. To define a
class method, put the modifier static in the method declaration as
follows:
static returnValueType staticMethod();

Examples of static methods are the readDouble() and the readInt()


in the class MyInput, and all the methods in the Math class.
Class methods are called by one of the following syntaxes:

ClassName.methodName();
objectName.methodName();

For example, MyInput.readInt() is a call that reads an integer from the


keyboard. MyInput is a class, not an object.
Instance variables can be accessed from instance methods in a class, but
not from a class method in a class.

class Foo{
int i=5;
static void p(){
int j=1;//wrong
}
}
The Scope of Variables

You use an instance variable or a class variable to


describe the property of an object. These variables are
referred to as global variables because they can be
accessed by all the methods in the class. A variable
declared in a method is referred to as a local variable,
since it is only used inside a method locally.
The scope of the variable is the part of the program
where the variable can be referenced. The scope of
instance and class variables is the entire class,
regardless of where the variables are declared. The
instance and class variables can be declared anywhere in
the class.
Example:
class Circle{
double findArea(){
return radius*radius*Math.PI;
}

double radius = 1;
}
Note: The data properties and methods are the members of
the class. There is no order among them. Therefore, they
can be declared in any order in a class.
The scope of a local variable starts from its declaration and
continues to the end of the block that contains the variable. A local
variable must be declared before it can be used.
You can declare a variable only once as a class member (instance
or class variable), but you can declare the same variable in a method
multiple times in different non-nesting blocks.

class Foo{
int x=0;int y=0;//instance variable
Foo(){}
void p(){
int x=1;//local variable
System.out.println(“x = ”+x);
System.out.println(“y = ”+y);
}
}
Using the Scanner Class to Accept Keyboard Input
• System.in object
• Standard input device
• Normally the keyboard
• Access using the Scanner class
• Scanner object
• Breaks input into units called tokens
Pitfall: Using nextLine() Following One of the Other Scanner Input Methods

There is a problem when using one numeric Scanner class retrieval method or
next()method before using the nextLine()method
Keyboard buffer
Location in memory that stores all keystrokes, including Enter
To avoid issues, add an extra nextLine()method call to retrieve the abandoned
Enter key character after numeric or next() inputs
Using the JOptionPane Class to Accept GUI Input
Dialog boxes used to accept user input:
Input dialog box
Confirm dialog box

Input dialog box


Asks a question
Provides a text field in which the user can enter a response
showInputDialog() method
Six overloaded versions
Returns a String representing a user’s response
Prompt
A message requesting user input
showInputDialog()
One version requires four arguments:
Parent component
Message
Title
Type of dialog box
Convert String to int or double
Use methods from the built-in Java classes Integer and Double

Type-wrapper classes
Each primitive type has a corresponding class contained in the java.lang
package
Include methods to process primitive type values
Integer.parseInt()
Double.parseDouble()
Using Confirm Dialog Boxes
Confirm dialog box
Displays the options Yes, No, and Cancel
showConfirmDialog() method in JOptionPane class
Four overloaded versions are available
Returns integer containing either:
JOptionPane.YES_OPTION
JOptionPane.NO_OPTION
JOptionPane.CANCEL_OPTION
You can create a confirm dialog box with five arguments:
Parent component
Prompt message
Title
Integer that indicates which option button to show
Integer that describes the kind of dialog box

You might also like