3- variables and identifiers
TRANSCRIPT
Muhammad Haris - Lecturer GIS Center PUCIT
Variable
The variable is the basic unit of storage in a Java program.
A variable is defined by the combination of a type, an identifier, and an optional initializer.
int marks = 50; String name = “Haris” ;
Muhammad Haris - Lecturer GIS Center PUCIT
Data type
Java defines eight simple (or primitive) types of data: byte, short, int, long, char, float, double, and boolean.
These can be put in four groups: Integers: This group includes byte, short, int, and long, which
are for whole valued signed numbers. Floating-point numbers: This group includes float and double,
which represent numbers with fractional precision (decimal numbers).
Characters: This group includes char, which represents symbols in a characterset, like letters and numbers
Boolean: This group includes boolean, which is a special type for representing true/false values.
Muhammad Haris - Lecturer GIS Center PUCIT
Integer
Muhammad Haris - Lecturer GIS Center PUCIT
Floating point number
Muhammad Haris - Lecturer GIS Center PUCIT
Characters
the data type used to store characters is char.
Java char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative chars.
Muhammad Haris - Lecturer GIS Center PUCIT
Demonstration of Char
Muhammad Haris - Lecturer GIS Center PUCIT
Boolean
It can have only one of two possible values, true or false
Muhammad Haris - Lecturer GIS Center PUCIT
Identifier
An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($).
An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit.
An identifier cannot be a reserved word. An identifier cannot be true, false, or
null. An identifier can be of any length.
Muhammad Haris - Lecturer GIS Center PUCIT
Declaring Variables
int x; // Declare x to be an // integer variable;
double radius; // Declare radius to // be a double variable;
char a; // Declare a to be a // character variable;
Muhammad Haris - Lecturer GIS Center PUCIT
Assignment Statements
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;
Muhammad Haris - Lecturer GIS Center PUCIT
Declaring and Initializingin One Step
int x = 1;
double d = 1.4;
float f = 1.4;
Muhammad Haris - Lecturer GIS Center PUCIT
Constants
final datatype CONSTANTNAME = VALUE;
final double PI = 3.14159;
final int SIZE = 3;
Muhammad Haris - Lecturer GIS Center PUCIT
Scope of variable
Muhammad Haris - Lecturer GIS Center PUCIT
Muhammad Haris - Lecturer GIS Center PUCIT
Practical
Demonstration
Muhammad Haris - Lecturer GIS Center PUCIT
Type Conversion and Casting
Converting data from one type to another Automatic type conversion take place if;
The two types are compatible. The destination type is larger than the source
type.
Muhammad Haris - Lecturer GIS Center PUCIT
Type Conversion and Casting
To create a conversion between two incompatible types, you must use a cast.
A cast is simply an explicit type conversion. It has this general form:
(target-type) value
Muhammad Haris - Lecturer GIS Center PUCIT
Conversion Example
Muhammad Haris - Lecturer GIS Center PUCIT
Output of previous example
Muhammad Haris - Lecturer GIS Center PUCIT
Consider the following statements:
byte i = 100;
long k = i*3+4;
double d = i*3.1+k/2;
int x = k; //(Wrong)
long k = x; //(fine,implicit casting)
Muhammad Haris - Lecturer GIS Center PUCIT
Description Escape Sequence Unicode
Backspace \b \u0008
Tab \t \u0009
Linefeed \n \u000a
Carriage return \r \u000d
Escape Characters
Muhammad Haris - Lecturer GIS Center PUCIT
int i = 'a'; // Same as int i = (int)'a';
char c = 97; // Same as char c = (char)97;
Muhammad Haris - Lecturer GIS Center PUCIT
Input Dialog Box
Sample Program
Muhammad Haris - Lecturer GIS Center PUCIT
String inputYear = JOptionPane.showInputDialog( null, “Prompt Message”, “Dialog Title”, JOptionPane.QUESTION_MESSAGE);
Muhammad Haris - Lecturer GIS Center PUCIT
The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns “123”. To obtain the input as a number, you have to convert a string into a number.
To convert a string into an int value, you can use the static parseInt method in the Integer class as follows:
int intValue = Integer.parseInt(intString);
where intString is a numeric string such as “123”.
Muhammad Haris - Lecturer GIS Center PUCIT
To convert a string into a double value, you can use the static parseDouble method in the Double class as follows:
double doubleValue=Double.parseDouble(doubleString);
where doubleString is a numeric string such as “123.45”.
Muhammad Haris - Lecturer GIS Center PUCIT
JOptionPane.showMessageDialog(null, "Your Message");
Muhammad Haris - Lecturer GIS Center PUCIT
Write a “CPA Calculation Java Program"
using Input from User
Muhammad Haris - Lecturer GIS Center PUCIT
References
Concepts
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
http://www.javatutorialhub.com/wiki/Variables
http://www.java-made-easy.com/java-variables.html
Exercises
http://www.functionx.com/java/Lesson02.htm