You are on page 1of 4

Chapter-2

Introduction to a Class
Token: The smallest individual unit in a program is known as token.
Example: keywords, identifiers, punctuation marks, literals (constants), operators.
Whitespace: Whitespace is a space between two tokens. There should be at least one whitespace character between
two tokens. Tab space (\t) and new line (\n) are also referred as whitespace.
Keywords: Keywords are the words that convey special meaning to the language compiler. They are used for
special purpose. They should be written in lowercase only.
Example: int, void, static, etc.,
Identifiers: Identifiers are the names given to various data items in a program such as variables, classes,
functions/methods, objects, arrays.
Rules for naming an identifier:
1.
2.
3.
4.
5.
6.

Should always start with either an alphabet or dollar sign $ or underscore sign _.
Should not start with a number, but numbers can be present in between the name or at the end of the name.
Keywords should not be used for naming the identifiers, but keywords can be prefixed or suffixed with the
identifier names.
No blank space characters allowed.
Identifier can be written either in uppercase alphabets or in lowercase alphabets or including both.
The length (number of characters) depends upon the operating system used.

Comments: Comments are used to enhance the readability of the program. It is the remark given by the
programmer. Comments will not be executed by the compiler. The different styles of expressing comments are:
1.
2.

3.

Single line comment: It is preceded by //. We can write comment in one line only. If you want to continue
writing the comment in the next line, then every line should start with // Example: // this is a comment.
Multiline comment: It is preceded by /*.. and the comment ends with */. We can write multiple
line comments.
Example: /* this program is used to input the total purchase of a customer
and calculates the discount amount and the amount to be paid by the
customer. */
Documentation comment: This type of comment is used to produce an HTML file that documents your
program. It starts with /** and ends with */
Separators: Separators are the few characters that are used as punctuators in a program.
1. ( )Parenthesis:
1. Used to declare input parameters.
Eg: public void input (int a, int b)

2. Used to group expressions.


Eg: (a+b)*c/d
2. { } Curly braces:
Used to start and end the class/method.

Eg: class abc


{
public void input (int a, int b)
{
Statement (s);
}}
3. [ ] Brackets:
1. Used to declare array type. Eg: int a [ ];
4. ; Semicolon:
Used to end the java statement.
Eg: int a; a=10;
System.out.println (a);
a=a+5;
5. ,Comma:
1. Used to separate the variables of same data type Eg: int a,b,c;
2. Used to separate input parameters in a function header.
Eg: public void input (int a, char b, String s)
6 . Dot/period:
1. Used to separate the object and function
Eg: System.out.println(Hello); out is object and println() is a function
2. Used to separate the class name and function.
Eg: Math.sqrt (25 ); Mathis a class name and sqrt() is a function
Constants or Literals: Literals (often referred as constants) are the data items that are fixed data values. The
different types of constants are:
1.
2.

3.

4.
5.

Integer constants: These are the whole numbers without any fractional part. Eg: 50, 457, -20.
Floating-point constant: Floating point constant are also called as real constants. These constants are
numbers having fractional parts.
Data type
Eg: -50.5, 50.2
Character constants: A character constant is one character enclosed in single quotes. Java allows you to
have certain non-graphic characters in character constants. Non-graphic characters or escape sequence
characters are the characters that cannot be typed directly from keyboard. Eg: backspace, tab, carriage
Reference
return (enter key) etc.,Primitive
These Non-graphic characters can be represented
by a backslash (\) followed by one
or more characters. \nnew line or line feed, \ttab, \ double quotes etc., The following are some
examples of character constants a, %, \t, 8
String constant: Multiple characters enclosed within double quotes are treated as string constants. Eg:
abc\t NOTE:
\t is one character.
Numeric
Non-Numeric
Classes
Arrays
Boolean constant: These constants are represented by two values true and false. Eg: true, false

Data type: It is defined as the set of possible values that a variable can hold. Java data types are classified as
Boolean
boolean
1. Primitive data type or intrinsic data type or basic data type or fundamental data type.
double
2. Reference data type or composite data type or user defined data type.

Integer

Floating point

Character

byte
short
int
long

float
double

char

Data type

Size in
bits

Size in
bytes

byte

8 bits

1 byte

short

16 bits

2 bytes

int
long

32 bits
64 bits

4 bytes
8 bytes

float

32 bits

4 bytes

double

64 bits

8 bytes

char

16 bits
8 bits,
but uses
1 bit

2 bytes

-128 to +127
or
7
-2 to 27-1
-32768 to
+32767
or
15
-2 to 215-1
-231 to 231-1
-263 to 263-1
-3.4 x 10-38
to3.4 x 1038
-1.7 x 10-308
to 1.7 x 10308
0 to 65536

1 byte

true or false

boolean

Range

The default data type for integer constants is int.


The default data type for floating point constant is double.
Variable: variable represent named storage locations whose values can be manipulated during program run. A
variable holds a data value of a particular data type.
Variable declaration: It is the data type followed by the variable name.

Syntax: datatype variable;


Example: int age; float amount;
Initialization of variable: Assigning values or storing values into variables is called as initialization.
Syntax: datatype variable;
variable=value;
[or] datatype variable=value;
Example: int x; x=10; or int x=10;
Dynamic Initialization of variable: Initializing the variable during the execution of the program is known as
Dynamic Initialization.
Example: int x=10, y=20;
Int z=x+y;
Operator: It is a symbol or character used to perform a specific operation on operands(variables) or values.
Operators are categorized as follows:
Arithmetic Operators: These operators are used to perform arithmetic operations on two or more operands. The
result of these operators will be Integer or Floating point.
Sl.no
1
2
3
4
5

Symbol
+
*
/
%

Description
Addition
Subtraction
Multiplication
Division
Modulo

Example:
int x=10,y=5;
System.out.println (x+y);
System.out.println (x-y);
System.out.println (x*y);
System.out.println (x/y); // gives Quotient as output after dividing 2 numbers
System.out.println (x%y); // gives remainder as output after dividing 2 numbers

You might also like