Class 9 Notes
Class 9 Notes
Characters are the smallest units (elements) of Java language that are used to
write Java tokens. These characters are defined by the Unicode character set.
Java language uses the character sets as the building block to form the basic
elements such as identifiers, variables, array, etc in the program. These are as
follows:
Letters: Both lowercase (a, b, c, d, e, etc.) and uppercase (A, B, C, D, E,
etc.) letters.
Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
Special symbols: _, (, ), {, }, [, ], +, -, *, /, %, !, &, |, ~, ^, <, =, >, $, #, ?,
Comma (,), Dot (.), Colon (:), Semi-colon (;), Single quote (‘), Double
quote (“), Back slash (\).
White space: Space, Tab, New line.
Java Tokens
A token is the smallest element of a program that is meaningful to the compiler.
Tokens can be classified as follows:
1. Keywords
2. Identifiers
3. Constants
4. Special Symbols
5. Operators
constant
The above statement declares the float variable "A" as a constant with a value
of 3.14. We cannot change the value of "A" at any point in time in the program.
Later if we try to do that by using a statement like "A=5.25", Java will throw
errors at compile time itself. It is not mandatory that we need to assign values
of constants during initialization itself.
Keyword
1. Keyword ✓
Keywords are reserved words that have a special meaning for the Java
compiler. Java compiler reserves these words for its own use so Keywords
cannot be used as identifiers. For example, void, public, class, int, etc.
Java has a set of keywords that are reserved words that cannot be used as
variables, methods, classes, or any other identifiers:
3. boolean A data type that can hold True and False values only
13. double A data type that can hold 64-bit floating-point numbers
25. int A data type that can hold a 32-bit signed integer
46. try Starts a block of code that will be tested for exceptions
47. void Specifies that a method does not have a return value
Java Identifier
All Java variables must be identified with unique names.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, total
Volume).
Identifiers in Java are a sequence of characters to identify something in a program. They are
names given to a class, variable, package, method, or interface and allow the programmer to
refer to the specific item from any place in the program.
1. Primitive Datatypes.
2. Non-Primitive Datatypes.
The main difference between primitive and non-primitive data types are:
Literal: Any constant value which can be assigned to the variable is called
literal/constant
Literals in Java are a synthetic representation of boolean, character, numeric, or string data.
They are a means of expressing particular values within a program. They are constant values
that directly appear in a program and can be assigned now to a variable. For example, here
is an integer variable named ‘’/count assigned as an integer value in this statement:
Decimal literals(Base 10): In this form, the allowed digits are 0-9.
double d = 123.456;
Floating: Floating format single precision (4 bytes) end with an “f” or “F.” Example: 4f.
Floating format double precision (8 bytes) end with a “d” or “D.”
Example: 3.14d.
Decimal: This format uses 0 through 9 and can have either a suffix or an exponent. Example:
99638.440.
Decimal in Exponent form: The exponent form may use an optional sign, such as a "-," and
an exponent indicator, such as "e" or "E." Example: 456.5f.
3. Char Literals
4. String Literals
String literals are sequences of characters enclosed between double quote ("") marks.
These characters can be alphanumeric, special characters, blank spaces, etc.
5. Boolean Literals
Boolean literals have only two values and so are divided into two literals:
So, Boolean literals represent the logical value of either true or false. These values
aren't case-sensitive and are equally valid if rendered in uppercase or lowercase mode.
Boolean literals can also use the values of “0” and “1.”
Examples:
boolean b = true;
boolean d = false;
6. Null Literals
Null literals represent a null value and refer to no object. Nulls are typically used as a
marker to indicate that a reference type object isn’t available. They often describe an
uninitialized state in the program. It is a mistake to try to dereference a null value.
Example: Patient age = NULL;
Remember, not everyone divides literals in Java into these six types. Alternative
classifications split literals into as few as four types (Integer, Character, Boolean, and
String) or as many as ten (Integer, Real, Backslash, Character, String, Floating-Point,
Boolean, NULL, Class, and Invalid).
Separators In Java
In Java, separators are characters that separate different parts of a code statement or
expression. They play an important role in defining the language's syntax and help to
organize and structure code in a readable and understandable way.
Java has several types of separators, each with a specific use. The most commonly used
separators include the semicolon (;), comma (,), dot (.), and colon (:).
Understanding and properly using separators is important in writing clear and efficient
Java code. It allows for a consistent and organized structure, making it easier for other
developers to understand and work with the code.
int a = 10, b = 5
Java Operators
In the example below, we use the + operator to add together two values:
Operators in Java
Operator in Java is a symbol that is used to perform operations. For example: +, -, *, /
etc.
There are many types of operators in Java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Arithmetic multiplicative * / %
additive + -
equality == !=
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary ternary ? :
Java Variables
Syntax
type variableName = value;
Example
Create a variable called name of type String and assign it the value "John":
System.out.println(name);
=====================
int x = 5;
int y = 10;
int z = 50;
System.out.println(x + y + z);
int num = 5;
String message = "Hello, World!";