(
Java Tokens
A token is the smallest element of a program that is meaningful to the compiler. Tokens supported in
Java include keywords, variables, constants, special characters, operations etc.
Tokens are the various Java program elements which are identified by the compiler.
Created by
Page 1 of 13
(
//Print Hello
public class Hello
{
public static void main(String args[])
{
System.out.println(“Hello Java”);
}
}
The source code contains tokens such as public, class, Hello, {, public, static, void, main, (, String, [],
args, {, System, out, println, (, "Hello Java", }, }. The resulting tokens· are compiled into Java
bytecodes that is capable of being run from within an interpreted java environment. Token are useful
for compiler to detect errors. When tokens are not arranged in a particular sequence, the compiler
generates an error message.
Tokens are the smallest unit of Program There is Five Types of Tokens
1) Reserve Word or Keywords
2) Identifier
3) Literals
4) Operators
5) Separators
Java Keywords
Keywords are an essential part of a language definition.
Java language has reserved words as keyword.
Keywords have specific meaning in Java we cannot use them as names for variables, classes,
methods and so … on.
Created by
Page 2 of 13
(
All keywords are to be written in lower case letters.
Java is case sensitive.
Java Literals - What is literal in Java? Type of literal
By literal we mean any number, text, or other information that represents a value. For instance
int month = 10;
In the above statement the literal is an integer value i.e 10. The literal is 10 because it directly
represents the integer value.
Constant Vs Literals
A Literal is a value that is expressed itself. Ex – 5,”hello”,’#’ etc.
A Constant is a data type that substitute a literal.
ex – final double pi=3.14;
Here pi is a constant but 3.14 is a literal
final keyword:
In Java final keyword can be used to define a constant.
ex – final int x = 10;
x = 20; //error , x cannot be modified
const vs final
In Java, const (as well as goto) is reserved word and is not allowed to be used by programmer in coding.
**We also cannot define a variable having name const and goto as they are reserved word (not keyword).
Number Literals
Number literals is a sequence of digits and a suffix as L. To represent the type as long integer we use L
as a suffix. We can specify the integers either in decimal, hexadecimal or octal format. To indicate
a decimal format put the left most digit as nonzero. Similarly put the characters as oxto the left of at
least one hexadecimal digit to indicate hexadecimal format. Also we can indicate the octal format by
a zero digit followed by the digits 0 to 7. Lets tweak the table below.
659L
Decimal integer literal of type long integer
0x4a
Hexadecimal integer literal of type integer
057L
Octal integer literal of type long integer
Character Literals
Created by
Page 3 of 13
(
We can specify a character literal as a single printable character in a pair of single quote characters such
as 'a', '#', and '3' etc.
Escape Meaning
\n
New line
\t
Tab
\b
Backspace
\r
Carriage return
\f
Formfeed
\\
Backslash
\'
Single quotation mark
\"
Double quotation mark
\d
Octal
\xd
Hexadecimal
\ud
Unicode character
It is very interesting to know that if we want to specify a single quote, a backslash, or a nonprintable
character as a character literal use an escape sequence. An escape sequence uses a special syntax to
represents a character. The syntax begins with a single backslash character.
Boolean Literals
The values true and false are also treated as literals in Java programming. When we assign a value to a
boolean variable, we can only use these two values. Unlike C, we can't presume that the value of 1 is
equivalent to true and 0 is equivalent to false in Java. We have to use the values true and false to
represent a Boolean value. Like
boolean chosen = true;
Remember that the literal true is not represented by the quotation marks around it. The Java compiler
will take it as a string of characters, if its in quotation marks.
Floating-point literals
Floating-point numbers are like real numbers in mathematics, for example, 4.13179, -0.000001. Java
has two kinds of floating-point numbers: float and double. The default type when you write a floatingpoint literal is double.
Type
Size
Range
Created by
Precision
Page 4 of 13
(
name
bytes
bits
(Approximate)
(in decimal digits)
float
4
32
+/- 3.4 * 1038
6-7
double
8
64
+/- 1.8 * 10308
15
A floating-point literal can be denoted as a decimal point, a fraction part, an exponent (represented by E
or e) and as an integer. We also add a suffix to the floating point literal as D, d, F or f. The type of a
floating-point literal defaults to double-precision floating-point.
The following floating-point literals represent double-precision floating-point and floating-point values.
6.5E+32 (or 6.5E32)
Double-precision floating-point literal
7D
Double-precision floating-point literal
.01f
Floating-point literal
String Literals
The string of characters is represented as String literals in Java. In Java a string is not a basic data type,
rather it is an object. These strings are not stored in arrays as in C language. There are few methods
provided in Java to combine strings, modify strings and to know whether to strings have the same
value.
We represent string literals as
String myString = "How are you?";
The above example shows how to represent a string. It consists of a series of characters inside double
quotation marks.
Lets see some more examples of string literals:
"" // the empty string
"\"" // a string containing "
"This is a string" // a string containing 16 characters
"This is a " + // actually a string-valued constant expression,
"two-line string" // formed from two string literals
Strings can include the character escape codes as well, as shown here:
String example = "Your Name, \"Sumit\"";
System.out.println("Thankingyou,\nRichards\n");
Null Literals
The final literal that we can use in Java programming is a Null literal. We specify the Null literal in the
source code as 'null'. To reduce the number of references to an object, use null literal. The type of the
null literal is always null. We typically assign null literals to object reference variables. For instance
s = null;
Created by
Page 5 of 13
(
In this example an object is referenced by s. We reduce the number of references to an object by
assigning null to s. Now, as in this example the object is no longer referenced so it will be available for
the garbage collection i.e. the compiler will destroy it and the free memory will be allocated to the other
object. Well, we will later learn about garbage collection.
Separators :
Separators are symbols used to indicate where groups of code are divided and arranged.
Parentheses ( ) : Used to enclosed parameters in method definition.
Braces { }: Define a block of code for classes methods and local scope.
Automatically initialize values to array.
Brackets [ ] : Used to declare array types.
Semicolon (;) : Used to separate statement.
Comma (,) : Used to separate identifiers in a variable deceleration.
Variables and Data Types in Java
Variable
Variable is name of reserved area allocated in memory. In other words, it is a name of memory
location. It is a combination of "vary + able" that means its value can be changed.
1.
int data=50;//Here data is variable
Types of Variable
There are three types of variables in java:
Created by
Page 6 of 13
(
o
local variable
o
instance variable
o
static variable
1) Local Variable
A variable which is declared inside the method is called local variable.
2) Instance Variable
A variable which is declared inside the class but outside the method, is called instance variable. It is not
declared as static.
3) Static variable or Class variable
A variable that is declared as static is called static variable. It cannot be local. Static variable in Java is
variable which belongs to the class and initialized only once at the start of the execution.
It is a variable which belongs to the class and not to object(instance)
Static variables are initialized only once, at the start of the execution. These variables will be
initialized first, before the initialization of any instance variables
A single copy to be shared by all instances of the class
A static variable can be accessed directly by the class name and doesn’t need any object
Syntax :
<class-name>.<variable-name>
Example to understand the types of variables in java
1. class A
2. {
3. int data=50;
4. static int m=100;
5. void method()
//instance variable
//static variable
Created by
Page 7 of 13
(
6. {
7.
int n=90;
8. }
9. }//end of class
//local variable
Data Types in Java
Data types represent the different values to be stored in the variable. In java, there are two types of data
types:
o
Primitive data types
o
Non-primitive or Referenced data types
Created by
Page 8 of 13
(
Data Type
Default Value
Default size
boolean
false
1 bit
char
'\u0000'
2 byte
(it is a Unicode value
denoting null or 0)
byte
0
1 byte
short
0
2 byte
int
0
4 byte
long
0L
8 byte
Created by
Page 9 of 13
(
float
0.0f
4 byte
double
0.0d
8 byte
The following table lists all Java primitive data types, their storage requirements in bytes and the
numeric range they support.
Table 1: List of Java's primitive data types
Type
Size in Bytes
Range
byte
1 byte
-128 to 127
short
2 bytes
-32,768 to 32,767
int
4 bytes
-2,147,483,648 to 2,147,483, 647
long
8 bytes
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float
4 bytes
approximately ±3.40282347E+38F (6-7 significant decimal digits) Java
implements IEEE 754 standard
double
8 bytes
Approximately ±1.79769313486231570E+308
(15 significant decimal digits)
char
2 byte
0 to 65,536 (unsigned)
boolean
not precisely true or false
defined*
*boolean represents one bit of information, but its "size" isn't something that's precisely
defined.
Primitive Data Types
There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the
language and named by a keyword. Let us now look into the eight primitive data types in detail.
byte
Byte data type is an 8-bit signed two's complement integer
Minimum value is -128 (-2^7)
Maximum value is 127 (inclusive)(2^7 -1)
Default value is 0
Created by
Page 10 of 13
(
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 integer.
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 (-2^15)
Maximum value is 32,767 (inclusive) (2^15 -1)
Short data type can also be used to save memory as byte data type. A short is 2 times smaller
than an integer
Default value is 0.
Example: short s = 10000, short r = -20000
Int data type is a 32-bit signed two's complement integer.
Minimum value is - 2,147,483,648 (-2^31)
Maximum value is 2,147,483,647(inclusive) (2^31 -1)
Integer 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
int
long
Long data type is a 64-bit signed two's complement integer
Minimum value is -9,223,372,036,854,775,808(-2^63)
Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -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 floating point
Float is mainly used to save memory in large arrays of floating point numbers
Default value is 0.0f
Created by
Page 11 of 13
(
Float data type is never used for precise values
Example: float f1 = 234.5f;
double
double data type is a double-precision 64-bit 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
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' (or 0)
Maximum value is '\uffff' (or 65,535 inclusive)
Char data type is used to store any character
Example: char letterA = 'A'
Non Primitive or Reference Data types
Reference variables are created using defined constructors of the classes. They are used to
access objects. These variables are declared to be of a specific type that cannot be changed. For
example, Employee, Puppy, etc.
Created by
Page 12 of 13
(
Class objects and various type of array variables come under reference datatype.
Default value of any reference variable is null.
A reference variable can be used to refer any object of the declared type or any compatible
type.
Example: Animal animal = new Animal("giraffe");
Created by
Page 13 of 13
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )