Académique Documents
Professionnel Documents
Culture Documents
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)
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
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