Vous êtes sur la page 1sur 4

Java Identifiers

Identifiers are names we give to our variables, constants, classes, and methods.

Identifiers must comply with these rules:

• The first character of an identifier must be a letter, an underscore(_), or a dollar sign($).


• The rest of the characters in the identifier can be a letter, underscore, dollar sign, or digit. Note
that spaces are NOT allowed in identifiers.
• Identifiers are case-sensitive. This means that age and Age are different identifiers.
• Identifiers cannot match any of Java's reserved words.

Although not required, we strongly recommend that you name your identifiers using Java
naming conventions.

Examples:

These identifiers are valid:

MyClass
$amount
totalGrades;
TotalGrades;
TAX_RATE
one

These identifiers are NOT valid:

My Class // spaces are not allowed


numberOf*s // the asterisk cannot be used
final // final is a reserved word
1Way // identifiers cannot start with a digit

In this example you will learn what is identifier and primitive data types of a identifier. This
tutorials will teach you how to use these identifier in you java programming. Identifier is is a
simple variable name which is defined as the value container. The type of value stored by
identifier is defined by the special java keyword is termed as primitive data type.

In the given example there are some identifiers have been used like byteident, shortident,
intident, longident, charident, stringident, floatident, doubleident. And there are some primitive
data types of used identifiers have been also used in the program like byte, short, int, long, float,
double, char and String.

All the data type has it's own capacity to keep the maximum value. Which have been mentioned
below :

Primitive Data Types


Keywo
Description Size/Format
rd

Integers

8-bit two's
byte Byte-length integer
complement

16-bit two's
short Short integer
complement

32-bit two's
int Integer
complement

64-bit two's
long Long integer
complement

Real numbers

Single-precision floating
float 32-bit IEEE 754
point

Double-precision floating
double 64-bit IEEE 754
point

Other types

16-bit Unicode
char A single character
character

A boolean value (true or


boolean true or false
false)

Code of the Program :

public class identifierandpdatatype{


public static void main(String[] args){
byte byteident = 3;
short shortident=100;
int intident = 10;
long longident = 40000;
char charident = 'a';
String stringident = "chandan";
float floatident = 12.0045f;
double doubleident = 2333333.000000000033343343434f;
System.out.println(byteident + " is the value of identifire

named 'byteident' which primitive data type is byte.");


System.out.println(shortident + " is the value of

identifire named 'shortident' which primitive data type is short.");


System.out.println(intident + " is the value

of identifire named 'intident' which primitive data type is int.");


System.out.println(longident + " is the value

of identifire named 'longident' which primitive data type is long.");


System.out.println(charident + " is the value

of identifire named 'charident' which primitive data type is char.");


System.out.println(stringident + " is the value

of identifire named 'stringident' which primitive data type is string.");


System.out.println(floatident + " is the value

of identifire named 'floatident' which primitive data type is float.");


System.out.println(doubleident + " is the value

of identifire named 'doubleident' which primitive data type is double.");


}
}

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
• x
• i
• _myvariable
• $myvariable
• _9pins
• andros
• ανδρος
• OReilly
• This_is_an_insanely_long_variable_name_that_just_keeps_going_and_going_
and_going_and_well_you_get_the_idea_The_line_breaks_arent_really_part_o
f_the_variable_name_Its_just_that_this_variable_name_is_so_ridiculously
_long_that_it_won't_fit_on_the_page_I_cant_imagine_why_you_would_need_s
uch_a_long_variable_name_but_if_you_do_you_can_have_it

The following are not legal variable names:

• My Variable // Contains a space


• 9pins // Begins with a digit
• a+c // The plus sign is not an alphanumeric character
• testing1-2-3 // The hyphen is not an alphanumeric character
• O'Reilly // Apostrophe is not an alphanumeric character
• OReilly_&_Associates // ampersand is not an alphanumeric character

Tip: How to Begin a Variable Name with a Number

If you want to begin a variable name with a digit, prefix the name you'd like to have (e.g. 8ball)
with an underscore, e.g. _8ball. You can also use the underscore to act like a space in long
variable names.

A snippet of Java code with keywords highlighted in blue and bold font

In the Java programming language, a keyword is one of 50 reserved words[1] which have a
predefined meaning in the language; because of this, programmers cannot use keywords as
names for variables, methods, classes, or as any other identifier[2]. Due to their special functions
in the language, most integrated development environments for Java use syntax highlighting to
display keywords in a different color for easy identification.

Vous aimerez peut-être aussi