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” ;
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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:
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: