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

Week 2 Java

The document discusses Java keywords, identifiers, and data types. It provides rules for naming Java identifiers, such as only allowing alphanumeric characters and underscores, identifiers cannot start with digits, and identifiers are case-sensitive. It also describes the different types of variables in Java, including local variables declared in methods, instance variables declared in classes, and static variables declared with the static keyword. Finally, it covers the two types of data types in Java - primitive types like integer and float, and non-primitive types like classes and arrays.

Uploaded by

Angelina Lumbre
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Week 2 Java

The document discusses Java keywords, identifiers, and data types. It provides rules for naming Java identifiers, such as only allowing alphanumeric characters and underscores, identifiers cannot start with digits, and identifiers are case-sensitive. It also describes the different types of variables in Java, including local variables declared in methods, instance variables declared in classes, and static variables declared with the static keyword. Finally, it covers the two types of data types in Java - primitive types like integer and float, and non-primitive types like classes and arrays.

Uploaded by

Angelina Lumbre
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

JAVA KEYWORD, JAVA

IDENTIFIER AND DATA TYPES


JAVA
IDENTIFIERS
IDENTIFIERS are used for identification purposes. In
Java, an identifier can be a class name, method
name, variable name, or label. For example :
In the above java code, we have 5 identifiers
namely :
RULES FOR NAMING AN IDENTIFIER
There are certain rules for defining a valid
java identifier. These rules must be
followed, otherwise we get compile-time
error. These rules are also valid for other
languages like C,C++.
RULES FOR NAMING AN IDENTIFIER
The only allowed characters for identifiers
are all alphanumeric characters([A-Z],[a-
z],[0-9]), ‘$‘(dollar sign) and ‘_‘
(underscore).
For example “SMSHS@” is not a valid java
identifier as it contain ‘@’ special
character.
RULES FOR NAMING AN IDENTIFIER
Identifiers should not start with digits
([0-9]).
For example “342563SMSHS” is a not a
valid java identifier.
RULES FOR NAMING AN IDENTIFIER
Java identifiers are case-sensitive.
RULES FOR NAMING AN IDENTIFIER
There is no limit on the length of the
identifier but it is advisable to use an
optimum length of 4 – 15 letters only.
RULES FOR NAMING AN IDENTIFIER
Reserved Words can’t be used as an
identifier. For example “int while = 20;” is
an invalid statement as while is a
reserved word. There are 53 reserved
words in Java.
RULES FOR NAMING AN IDENTIFIER

Whitespaces are not allowed.


VALID IDENTIFIERS
scorelevel
highestScore
number1
convertToString
INVALID IDENTIFIERS
class
float
1number
highest Score
@pple
JAVA
KEYWORDS
JAVA KEYWORDS/RESERVED WORDS
Any programming language reserves some words to represent
functionalities defined by that language. These words are called
reserved words.They can be briefly categorised into two parts :
keywords(50) and literals(3). Keywords define functionalities and
literals define a value. Identifiers are used by symbol tables in
various analyzing phases(like lexical, syntax, semantic) of a
compiler architecture.
JAVA KEYWORDS
Example 1 Example 2 Example 3
VARIABLE IN JAVA
Variable in Java is a data container that stores the data values
during Java program execution. Every variable is assigned data
type which designates the type and quantity of value it can hold.
Variable is a memory location name of the data.
The Java variables have mainly three types :
Local, Instance and Static.
VARIABLE IN JAVA
In order to use a variable in a program you need
to perform
2 steps:
1. variable declaration
2. variable initialization
VARIABLE DECLARATION
To declare a variable, you must specify the
data type and the give a unique variable name
SYNTAX
data type variable = value;

int num1 = 19
VARIABLE INITIALIZATION
To initialize a variable, you must assign it a
valid value.
TYPES OF
VARIABLES
LOCAL VARIABLE
A variable declared inside the body of the method is
called local variable. You can use this variable only
within that method and the other methods in the class
aren't even aware that the variable exists.A local
variable cannot be defined with "static" keyword.
INSTANCE VARIABLE
A variable declared inside the class but outside the
body of the method, is called instance variable. It is not
declared as static.It is called instance variable because
its value is instance specific and is not shared among
instances.
STATIC VARIABLE
A variable which is declared as static is called static
variable. It cannot be local. You can create a single
copy of static variable and share among all the
instances of the class. Memory allocation for static
variable happens only once when the class is loaded in
the memory.
JAVA DATA
TYPES
Data Types in Java are defined as
specifiers that allocate different sizes
and types of values that can be stored
in the variable or an identifier. Java
has a rich set of data types. Data
types in Java can be divided into two
parts :
1. Primitive Data Types :- which include
integer, character, boolean, and float
2. Non-primitive Data Types :- which
include classes, arrays and
interfaces.

You might also like