Академический Документы
Профессиональный Документы
Культура Документы
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.
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.
Integer
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.
Demonstration of Char
Boolean
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;
char a;
Assignment Statements
x = 1; // Assign 1 to x;
radius = 1.0;
a = 'A';
Constants
final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3;
Scope of variable
Practical Demonstration
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.
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
Conversion Example
Escape Characters
Description Backspace Tab Linefeed Escape Sequence
\b \t \n
Unicode
\u0008 \u0009 \u000a \u000d
Carriage return \r
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:
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);
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