3- variables and identifiers

29
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” ;

Upload: asfaw-kelbessa

Post on 16-Apr-2015

33 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 3- Variables and Identifiers

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

Page 2: 3- Variables and Identifiers

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.

Page 3: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Integer

Page 4: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Floating point number

Page 5: 3- Variables and Identifiers

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.

Page 6: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Demonstration of Char

Page 7: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Boolean

It can have only one of two possible values, true or false

Page 8: 3- Variables and Identifiers

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.

Page 9: 3- Variables and Identifiers

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;

Page 10: 3- Variables and Identifiers

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;

Page 11: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Declaring and Initializingin One Step

int x = 1;

double d = 1.4;

float f = 1.4;

Page 12: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Constants

final datatype CONSTANTNAME = VALUE;

final double PI = 3.14159;

final int SIZE = 3;

Page 13: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Scope of variable

Page 14: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Page 15: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Practical

Demonstration

Page 16: 3- Variables and Identifiers

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.

Page 17: 3- Variables and Identifiers

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

Page 18: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Conversion Example

Page 19: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Output of previous example

Page 20: 3- Variables and Identifiers

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)

Page 21: 3- Variables and Identifiers

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

Page 22: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

int i = 'a'; // Same as int i = (int)'a';

char c = 97; // Same as char c = (char)97;

Page 23: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Input Dialog Box

Sample Program

Page 24: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

String inputYear = JOptionPane.showInputDialog( null, “Prompt Message”, “Dialog Title”, JOptionPane.QUESTION_MESSAGE);

Page 25: 3- Variables and Identifiers

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

Page 26: 3- Variables and Identifiers

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

Page 27: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

JOptionPane.showMessageDialog(null, "Your Message");

Page 28: 3- Variables and Identifiers

Muhammad Haris - Lecturer GIS Center PUCIT

Write a “CPA Calculation Java Program"

using Input from User

Page 29: 3- Variables and Identifiers

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