Session 2
Session 2
Scenario:
Sam
Identifying the Building Blocks of a Java Program (Contd.)
Scenario (Contd.):
Classes
Data types
A Java program
comprises
Class members
Packages
Defining a Class
A class defines:
The characteristics and behavior of an object.
The member variables and methods of objects that share common
characteristics.
The following code snippet shows how to declare a class:
class <ClassName>
{
//Declaration of member variables
//Declaration of methods
}
Defining a Class (Contd.)
There are certain rules that should be followed to name Java
classes.
Some of these rules are:
The name of a class should not contain any embedded space or
symbol, such as ?, !, #, @, %, &, {}, [], :, ;, “, and /.
A class name must be unique.
A class name must begin with a letter, an underscore (_), or the dollar
symbol ($). Or, it must begin with an alphabet that can be followed by
a sequence of letters or digits (0 to 9), ‘$’, or ‘_’.
A class name should not consist of a keyword.
Defining a Class (Contd.)
Keywords:
Are the reserved words with a special meaning for a language, which
express the language features.
Cannot be used to name variables or classes.
Should be written in lowercase only.
Defining a Class (Contd.)
The following table lists the Java keywords.
abstract boolean break byte
case catch char class
const continue default do
double else extends final
finally float for goto
if implements import instanceof
int interface long native
new package private protected
public return short static
strictfp super switch synchronized
this throw throws transient
try void volatile while
enum assert
Defining a Class (Contd.)
The following naming conventions should be followed for naming a
Java file:
A file name must be unique.
A file name cannot be a keyword.
If a class is specified as public, the file name and class name should
be the same.
If a file contains multiple classes, only one class can be declared as
public. The file name should be the same as the class name that is
declared public in the file.
If a file contains multiple classes that are not declared public, any file
name can be specified for the file.
Identifying Data Types
While working with an application, you need to store and
manipulate varying data.
To handle such varying data in an application, Java supports
various data types.
There are eight primitive data types in Java, which are further
grouped into the following categories:
Floating Characte
Integer point
Can store type Can store
r type Can store
integer decimal a single
values. numbers. character,
such as a
symbol,
letter, and
Boolean Can store number.
type only the
values,
true and
false.
Identifying Data Types (Contd.)
In order to use the primitive data types as objects, Java provides
wrapper classes.
A wrapper class acts like an object wrapper and encapsulates the
primitive data types within the class.
The various wrapper classes, such as Boolean, Character,
Integer, Short, Long, Double, and Float, are provided by the
java.lang package.
Identifying Class Members
In Java, a class can contain the following members:
Method
s
Variabl
es
Objects
Inner
classes
Identifying Class Members (Contd.)
A variable:
Is used essentially as a container for the storage of varying kinds of
data.
Represents a name that refers to a memory location where some value
is stored.
Needs to be declared before it is accessed.
The following code snippet shows how to declare a variable:
<type> <variablename>; // Single variable of given
type.
<type><variable1name,variable2name.....variable_n_n
ame> // Multiple variables of given type.
Identifying Class Members (Contd.)
The following code snippet shows how to assign values to a
variable:
<type> <variablename>=<value>; // During
declaration.
<variablename>=<value> // After declaration.
The following code snippet can be used to assign a value to a
variable at the time of its declaration:
int num1=5;
A literal:
Is a value that is assigned to a variable or constants.
Contains a sequence of characters, such as digits, alphabets, or any
other symbol, which represents the value to be stored.
Identifying Class Members (Contd.)
The various types of literals in Java are: