Identifiers Are The Names of Variables, Methods, Classes, Packages and Interfaces
Identifiers Are The Names of Variables, Methods, Classes, Packages and Interfaces
Identifiers are the names of variables, methods, classes, packages and interfaces.
Unlike literals they are not the things themselves, just ways of referring to them. In
the HelloWorld program, HelloWorld, String, args, main and println are identifiers.
Identifiers must be composed of letters, numbers, the underscore _ and the dollar
sign $. Identifiers may only begin with a letter, the underscore or a dollar sign.
Each variable has a name by which it is identified in the program. It's a good idea to
give your variables mnemonic names that are closely related to the values they hold.
Variable names can include any alphabetic character or digit and the underscore _.
The main restriction on the names you can give your variables is that they cannot
contain any white space. You cannot begin a variable name with a number. It is
important to note that as in C but not as in Fortran or Basic, all variable names are
case-sensitive. MyVariable is not the same as myVariable. There is no limit to the
length of a Java variable name. The following are legal variable names:
MyVariable
myvariable
MYVARIABLE
_myvariable
$myvariable
_9pins
andros
OReilly
This_is_an_insanely_long_variable_name_that_just_keeps_going_and_going_a
nd_going_and_well_you_get_the_idea_The_line_breaks_arent_really_part_of_
the_variable_name_Its_just_that_this_variable_name_is_so_ridiculously_lo
ng_that_it_won't_fit_on_the_page_I_cant_imagine_why_you_would_need_such_
a_long_variable_name_but_if_you_do_you_can_have_it
Java Literals
Java literals are fixed or constant values in a program's source code. You can use any
primitive type value as a literal e.g. boolean int, char and float. However, string is not
a primitive type in Java yet we can use strings as literals. Later in this article string
literals are discussed.
Integer literals in Java can be used in three flavors in a Java program those
are decimal, octal (base 8), and hexadecimal (base 16).
Octal literals are specified in Java program by a leading zero e.g., 07 or 06 are valid
octal values, while 09 or 08 are not because octal range lies between 0-7. If you try 08
as an octal literal for an experiment it will result into the error "The literal 08 of type
int is out of range".
A hexadecimal literal starts with a leading zero and x (0x) or zero and X (0X). For
example, 0xff or 0XFF are valid hexadecimal literals.
Decimal integer literals neither start with a leading zero nor a 0x or 0X. Following
piece of code shows all three variants of integer literals.
int octLit = 0400; //octal equivalent of decimal 256
int hexLit = 0x100; //hexadecimal equivalent of decimal 256
int decLit = 256; // decimal 256
Integer literals create an int value, which in Java is a 32-bit integer value. Since Java
is strongly typed, you might be wondering how it is possible to assign an integer
literal to one of Java's other integer types, such as byte or long, without causing a type
mismatch error. Fortunately, such situations are easily handled. When a literal value is
assigned to a byte or short variable, no error is generated if the literal value is within
the range of the target type. Also, an integer literal can always be assigned to
a long variable. However, to specify a long literal, you will need to explicitly tell the
compiler that the literal value is of type long. You do this by appending an upper or
lower-case letter L to the literal. For
example, 0x7ffffffffffffffL or 9223372036854775807L is the largest long.
Floating-point literals in Java default to double precision. To specify a float literal, you
must append an F or f to the constant. You can also explicitly specify a double literal
by appending a D or d. For example,
float f = 89.0; // Type mismatch: cannot convert from double to float
float ff = 89.0f; //OK
Boolean literals have only two possible values those are true and false.
boolean boolFalse = false;
boolean boolTrue = true;
Words true, false, and null might seem like keywords, but they are actually literals;
you cannot use them as identifiers in your programs.
Along with 8 built-in primitive data types; Java provides a rich set of readymade
classes for general data structures and problems.
boolean:
boolean data type represents one bit of information.
There are only two possible values: true and false.
This data type is used for simple flags that track true/false conditions.
Default value is false.
Example: boolean one = true
char:
char data type is a single 16-bit Unicode character.
Minimum value is '\u0000' or0.
Maximum value is '\uffff' or65, 535inclusive.
Char data type is used to store any character.
Example: char letterA ='A'
Declaring Variables
To declare a variable in Java, all that is needed is the data type followed by the variable
name:
int numberOfDays;
In the above example, a variable called "numberOfDays" has been declared with a data
type of int. Notice how the line ends with a semi-colon. The semi-colon tells the Java
compiler that the declaration is complete.
Now that it has been declared, numberOfDays can only ever hold values that match the
definition of the data type (i.e., for an int data type the value can only be a whole
number between -2,147,483,648 to 2,147,483,647).
Initializing Variables
Before a variable can be used it must be given an initial value. This is called initializing
the variable. If we try to use a variable without first giving it a value:
int numberOfDays;
//try and add 10 to the value of numberOfDays
numberOfDays = numberOfDays + 10;
int numberOfDays;
numberOfDays = 7;
numberOfDays = numberOfDays + 10;
System.out.println(numberOfDays);
Typically, the initializing of a variable is done at the same time as its declaration: