0% found this document useful (0 votes)
41 views

Class 9 Notes

The document discusses the basic building blocks of the Java language - characters, tokens, and identifiers. It provides details on the Unicode character set used by Java and lists the types of characters that make up Java tokens, including letters, digits, special symbols, and whitespace. It also defines tokens as the smallest meaningful elements to the compiler and categorizes them as keywords, identifiers, constants, operators, and special symbols. Finally, it discusses Java identifiers and the naming rules/conventions for variables in Java programs.

Uploaded by

shanawaz.wipro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Class 9 Notes

The document discusses the basic building blocks of the Java language - characters, tokens, and identifiers. It provides details on the Unicode character set used by Java and lists the types of characters that make up Java tokens, including letters, digits, special symbols, and whitespace. It also defines tokens as the smallest meaningful elements to the compiler and categorizes them as keywords, identifiers, constants, operators, and special symbols. Finally, it discusses Java identifiers and the naming rules/conventions for variables in Java programs.

Uploaded by

shanawaz.wipro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Characterset

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

A constant is a variable whose value cannot change once it has been


assigned. Java doesn't have built-in support for constants.
A = 3.14;

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

What do you understand by the term Keyword?


A word used in a high level language which has a special meaning
attached to it is called

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:

S.No Keyword Usage

Specifies that a class or method will be implemented


1. abstract
later, in a subclass

Assert describes a predicate placed in a java program to


2. assert indicate that the developer thinks that the predicate is
always true at that place.

3. boolean A data type that can hold True and False values only

4. break A control statement for breaking out of loops.


S.No Keyword Usage

A data type that can hold 8-bit data values


5. byte

6. case Used in switch statements to mark blocks of text

7. catch Catches exceptions generated by try statements

A data type that can hold unsigned 16-bit Unicode


8. char
characters

9. class Declares a new class

10. continue Sends control back outside a loop

11. default Specifies the default block of code in a switch statement

12. do Starts a do-while loop

13. double A data type that can hold 64-bit floating-point numbers

14. else Indicates alternative branches in an if statement

A Java keyword is used to declare an enumerated type.


15. enum
Enumerations extend the base class.

Indicates that a class is derived from another class or


16. extends
interface

Indicates that a variable holds a constant value or that a


17. final
method will not be overridden

Indicates a block of code in a try-catch structure that will


18. finally
always be executed
S.No Keyword Usage

A data type that holds a 32-bit floating-point number


19. float

20. for Used to start a for loop

21. if Tests a true/false expression and branches accordingly

22. implements Specifies that a class implements an interface

23. import References other classes

Indicates whether an object is an instance of a specific


24. instanceof
class or implements an interface

25. int A data type that can hold a 32-bit signed integer

26. interface Declares an interface

27. long A data type that holds a 64-bit integer

Specifies that a method is implemented with native


28. native
(platform-specific) code

29. new Creates new objects

This indicates that a reference does not refer to


30. null
anything

31. package Declares a Java package

An access specifier indicating that a method or variable


32. private
may be accessed only in the class it’s declared in

33. protected An access specifier indicating that a method or variable


S.No Keyword Usage

may only be accessed in the class it’s declared in (or a


subclass of the class it’s declared in or other classes in
the same package)

An access specifier used for classes, interfaces,


methods, and variables indicating that an item is
34. public
accessible throughout the application (or where the
class that defines it is accessible)

Sends control and possibly a return value back from a


35. return
called method

36. short A data type that can hold a 16-bit integer

Indicates that a variable or method is a class method


37 static
(rather than being limited to one particular object)

A Java keyword is used to restrict the precision and


38. strictfp rounding of floating-point calculations to ensure
portability.

Refers to a class’s base class (used in a method or


39. super
class constructor)

40. switch A statement that executes code based on a test value

Specifies critical sections or methods in multithreaded


41. synchronized
code

42. this Refers to the current object in a method or constructor

43. throw Creates an exception

44. throws Indicates what exceptions may be thrown by a method


S.No Keyword Usage

Specifies that a variable is not part of an object’s


45. transient
persistent state

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

This indicates that a variable may change


48. volatile
asynchronously

49. while Starts a while loop

Java Identifier
All Java variables must be identified with unique names.

These unique names are called identifiers.

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.

The general rules for naming variables are:

 Names can contain letters, digits, underscores, and dollar signs


 Names must begin with a letter
 Names should start with a lowercase letter and it cannot contain
whitespace
 Names can also begin with $ and _ (but we will not use it in this tutorial)
 Names are case sensitive ("myVar" and "myvar" are different variables)
 Reserved words (like Java keywords, such as int or boolean) cannot be
used as names
data types

Two kinds of data types are:

1. Primitive Datatypes.
2. Non-Primitive Datatypes.

Primitive Data Types


A primitive data type specifies the size and type of variable values, and it has
no additional methods.

There are eight primitive data types in Java:

Data Type Size Description

byte 1 byte Stores whole numbers from -128 to 127

short 2 bytes Stores whole numbers from -32,768 to


32,767

int 4 bytes Stores whole numbers from -2,147,483,648


to 2,147,483,647

long 8 bytes Stores whole numbers from -


9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for
storing 6 to 7 decimal digits

double 8 bytes Stores fractional numbers. Sufficient for


storing 15 decimal digits

boolean 1 bit Stores true or false values

char 2 bytes Stores a single character/letter or ASCII


values

Non-Primitive Data Types


Non-primitive data types are called reference types because they refer to
objects.

The main difference between primitive and non-primitive data types are:

 Primitive types are predefined (already defined) in Java. Non-primitive


types are created by the programmer and is not defined by Java (except
for String).
 Non-primitive types can be used to call methods to perform certain
operations, while primitive types cannot.
 A primitive type has always a value, while non-primitive types can
be null.
 A primitive type starts with a lowercase letter, while non-primitive types
starts with an uppercase letter.
Literals

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:

// Here 100 is a constant/literal.


int x = 100;

Decimal literals(Base 10): In this form, the allowed digits are 0-9.
double d = 123.456;

Octal literals (Base 8)


Hexa-decimal literals (Base 16)
Binary literals: From 1.7 onward,
2. Floating-Point Literals

Floating-point literals are expressed as exponential notations or as decimal fractions.


They can represent either a positive or negative value, but if it’s not specified, the value
defaults to positive. Floating-point literals come in these formats:

 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

Character (Char) literals are expressed as an escape sequence or a character,


enclosed in single quote marks, and always a type of character in Java. Char literals are
sixteen-bit Unicode characters ranging from 0 to 65535. Example: char ch = 077.

4. String Literals

String literals are sequences of characters enclosed between double quote ("") marks.
These characters can be alphanumeric, special characters, blank spaces, etc.

Examples: "John", "2468", "\n", etc.

5. Boolean Literals

Boolean literals have only two values and so are divided into two literals:

 True represents a real boolean value

 False represents a false boolean value

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

These separators are pictorially depicted below as follows:

Symbol Name Purpose

used to contain a list of parameters in method definition and


() Parentheses invocation. also used for defining precedence in expressions in
control statements and surrounding cast types
Symbol Name Purpose

Used to define a block of code, for classes, methods and local


{} Braces scopes Used to contain the value of automatically initialised
array

declares array types and also used when dereferencing array


[] Brackets
values

; Semicolon Terminates statements

Used to separates package names from sub-package and class


, Comma
names and also selects a field or method from an object

separates consecutive identifiers in variable declarations also


. Period
used to chains statements in the test, expression of a for loop

: Colon Used after labels

Java Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

int x = 100 + 50;

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.

Java Operator Precedence

Operator Type Category Precedence

Unary postfix expr++ expr--

prefix ++expr --expr +expr -expr ~ !

Arithmetic multiplicative * / %

additive + -

Shift shift << >> >>>

Relational comparison < > <= >= instanceof

equality == !=

Bitwise bitwise AND &

bitwise exclusive OR ^

bitwise inclusive OR |

Logical logical AND &&

logical OR ||
Ternary ternary ? :

Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

Java Variables

Variables are containers for storing data values.

In Java, there are different types of variables, for example:

 String - stores text, such as "Hello". String values are surrounded by


double quotes
 int - stores integers (whole numbers), without decimals, such as 123 or -
123
 float - stores floating point numbers, with decimals, such as 19.99 or -
19.99
 char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
 boolean - stores values with two states: true or false

Declaring (Creating) Variables


To create a variable, you must specify the type and assign it a value:

Syntax
type variableName = value;

Example
Create a variable called name of type String and assign it the value "John":

String name = "John";

System.out.println(name);
=====================

int x = 5;

int y = 10;

int z = 50;

System.out.println(x + y + z);

How to Initialize a Variable in Java?


During initialization, the data type of the variable must match the type of the
value which is being assigned. The basic syntax to initialize a variable in Java
is as follows:

data_type variable_name = value;

int num = 5;
String message = "Hello, World!";

Declaration vs. Initialization


Let’s start by making sure that we’re on the same page.
Declaration is the process of defining the variable, along with its type and
name.
Here we’re declaring the id variable:
int id;Copy
Initialization, on the other hand, is all about assigning a value:
id = 1;

You might also like