0% found this document useful (0 votes)
16 views21 pages

Session 2

The document outlines the foundational elements of a Java program, including classes, data types, and class members necessary for building applications like the Classic Jumble Word application. It details how to define classes, name conventions, and the importance of keywords, as well as the different data types and their wrappers. Additionally, it explains the structure of methods, constructors, and packages within Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views21 pages

Session 2

The document outlines the foundational elements of a Java program, including classes, data types, and class members necessary for building applications like the Classic Jumble Word application. It details how to define classes, name conventions, and the importance of keywords, as well as the different data types and their wrappers. Additionally, it explains the structure of methods, constructors, and packages within Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Identifying the Building Blocks of a Java Program

Scenario:

Needs to build a Java


program in order to
develop the Classic
Jumble Word application.

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:

Integer Floating Boolean Character String Binary


literals point • Can store type literals literals
• Are non- literals only the • Can store • Are • Are the
fractional • Are values, a single enclosed literals
numeric numeric true and character in double that can
values. values false. , such as quotation be used
that a marks. to
contain a symbol, express
fractional letter, the
part. and primitive
number. integer
data
types
into
binary
form.
Identifying Class Members (Contd.)
A method:
Is a set of statements that is intended to perform a specific task.
Provides encapsulation and is also essential to refer to the data
members and access them.
Consists of two parts, method declaration and method body.
Syntax:
<Access specifier> <Return type> <Method
name>(Parameter list) //method declaration
{
<Method body> // body of the method
}
Identifying Class Members (Contd.)
While adding a method to a class, you should adhere to the
following method naming conventions:
The method names should be verb-noun pairs.
The first letter of the method should be in lowercase.
If the method name consists of several words, the first letter of each
word, except the first word, should be capitalized.
When a method is invoked from a Java program, the control is
transferred to that method.
The syntax for calling a method is:
<Method name>(argument list);
In object-oriented languages, when you instantiate a class, special
methods called constructors are automatically called.
Identifying Class Members (Contd.)
Constructors are:
Methods that have the same name as that of the class and have no
return type.
Used to construct an object of a class and initialize it.
An object is an instance of a class and has a unique identity.
To create an object, you need to declare, and then, instantiate an
object.
The following code snippet shows how to declare an object of the
class:
class_name object_name;
Identifying Class Members (Contd.)
To allocate memory to the object, you need to instantiate the object
by using the new operator.
The new operator allocates memory to an object and returns a
reference to that memory location in the object variable.
The following code snippet shows how to create an object:
object_name= new class_name();
In Java, a class can also contain another class. Such a class is
known as an inner class or a nested class.
Defining a Package
A package:
Is a collection of classes.
Provides the space essentially used to organize classes that are
related to each other.
Ensures that the non-related classes with the same name do not
conflict with each other.
 The following syntax is used to define a package:
package <package_name>;
Defining a Package (Contd.)
The following figure represents the concept of packages in Java.
Just a minute
Which one of the following types of literals can be specified only by
values, true or false?
String
Boolean
Integer
Floating point
Just a minute (Contd.)
Solution:
Boolean

You might also like