You are on page 1of 8

JAVA IDENTIFIERS

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

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

Java Reserved Words


A reserved word in Java is a special word that gets recognized by the compiler.
When the compiler sees a reserved word, it is prompted to do a unique task.
Reserved words cannot be used for any other purpose in Java, besides their unique
task. Below is a list of the words and their purpose. You may not understand all of
the terms listed below, but you should familiarize yourself with them before you
start programming.
1. Boolean used for declaring a type Boolean
2. break a reserved word in java that will immediately break out of the loop
3. byte - used for declaring a type byte
4. catch used to handle exceptions
5. char - used for declaring a type char
6. class used for creating a new
class 7. do used to declare
a do-while loop 8.
double used for declaring a type double
9. else contains a block of code to do if the if-statement is
false 10. final a reserved word
in java that declares a value in java to be a constant 11.float -
used for declaring a type float
12.for used to declare a for loop
13.if used to make an if statement
14.int - used for declaring a type int
15.long - used for declaring a type long
16. new used for
declaring a new Object, Array, or Array List 17.
private used to declare something as private, or not able to be accessed
18.return a word in java used to return a value in a method
19.short - used for declaring a type short
20.static used to declare
something as static, or shared 21.switch
used to declare a switch statement
22.throw used to avoid handling the program crashing
23.try contains text that will be caught and handled if the program crashes
24. void declares that a method does not return any
value 25.while used to declare a while loop

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.

Java Integer Literals

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.

Java Floating-point Literals

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

double dou = 89.0D; //OK


double doub = 89.0d; //OK
double doubl = 89.0; //OK, by default floating point literal is double
Java Boolean Literals

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.

Java Character Literals


Character literals are enclosed in single quotes when they are used for example, 'a' or
'A'. Whereas string literals are enclosed in double quotes for example, "Coffea
arabica".
char charLit = 'a';
String strLit = "String Literal";
Java String Literals

A string literal consists of zero or more characters enclosed in double quotes.


Characters may be represented by escape sequences. A string literal is always of
type String and a reference to an instance of class String. A string literal always
refers to the same instance of class String. This is because string literals
are interned so as to share unique instances, using the method String.intern. It is a
compile-time error for a line terminator to appear after the opening " and before the
closing matching ". A long string literal can always be broken up into shorter pieces
and written as an expression using the string concatenation operator +.

Along with 8 built-in primitive data types; Java provides a rich set of readymade
classes for general data structures and problems.

Primitive Data Types:


There are eight primitive data types supported by Java. Primitive data types are
predefined by the
language and named by a keyword. Let us now look into detail about the eight primitive
data
types.
byte:
Byte data type is an 8-bit signed two's complement integer.
Minimum value is -128 27
Maximum value is 127 inclusive27 1
Default value is 0
Byte data type is used to save space in large arrays, mainly in place of integers,
since a byte
is four times smaller than an int.
Example: byte a = 100 , byte b = -50
short:
Short data type is a 16-bit signed two's complement integer.
Minimum value is -32,768 215
Maximum value is 32,767 inclusive 215 1
Short data type can also be used to save memory as byte data type. A short is 2
times
smaller than an int
Default value is 0.
Example: short s = 10000, short r = -20000
int:
Int data type is a 32-bit signed two's complement integer.
Minimum value is - 2,147,483,648.231
Maximum value is 2,147,483,647inclusive.231 1
Int is generally used as the default data type for integral values unless there is a
concern
about memory.
The default value is 0.
Example: int a = 100000, int b = -200000
long:
Long data type is a 64-bit signed two's complement integer.
Minimum value is -9,223,372,036,854,775,808.263
Maximum value is 9,223,372,036,854,775,807 inclusive. 263 1
This type is used when a wider range than int is needed.
Default value is 0L.
Example: long a = 100000L, long b = -200000L
float:
Float data type is a single-precision 32-bit IEEE 754 floating point.
Float is mainly used to save memory in large arrays of floating point numbers.
Default value is 0.0f.
Float data type is never used for precise values such as currency.
Example: float f1 = 234.5f
double:
double data type is a double-precision 64-bit IEEE 754 floating point.
This data type is generally used as the default data type for decimal values,
generally the
default choice.
Double data type should never be used for precise values such as currency.
Default value is 0.0d.
Example: double d1 = 123.4

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

Declaring variables for other data types is exactly the same:


byte nextInStream;
short hour;
long totalNumberOfStars;
float reactionTime;
double itemPrice;

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;

the compiler will throw an error:


variable numberOfDays might not have been initialized
To initialize a variable we use an assignment statement. An assignment statement
follows the same pattern as an equation in mathematics (e.g., 2 + 2 = 4). There is a left
side of the equation, a right side and an equals sign (i.e., "=") in the middle. To give a
variable a value, the left side is the name of the variable and the right side is the value:
int numberOfDays;
numberOfDays = 7;
In the above example, numberOfDays has been declared with a data type of int and has
been giving an initial value of 7. We can now add ten to the value of numberOfDays
because it has been initialized:

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:

//declare the variable and give it a value all in one statement


int numberOfDays = 7;

You might also like