lecture-04 Mobile Application and Development.pdf

AleenaJamil4 0 views 17 slides Oct 29, 2025
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

Mobile Application and Development


Slide Content

Mobile Application Development
LECTURE 4

What is Package???
A package in Java is used to group related classes. We use packages to avoid name conflicts, and
to write a better maintainable code.
Why Are They Used For?
The benefits of using Packages in Java are as follows:
ØThe packages organize the group of classes into a single API unit
ØIt will control the naming conflicts
ØEasy to locate the related classes
ØReuse the existing classes in packages

Types of Packages
Packages are divided into two categories:
ØBuilt-in Packages (packages from the Java API)
ØUser-defined Packages (create your own packages)

Built-in Packages
These are predefined packages provided by Java. They contain classes and interfaces for
common functionalities and are organized into various libraries.
Examples:
java.lang - Contains fundamental classes (e.g., String, System, Math).
java.util - Provides utility classes (e.g., ArrayList, HashMap, Date).
java.io - For input and output classes (e.g., File, InputStream, OutputStream).
java.net - For networking classes (e.g., Socket, URL).
javax.swing - For GUI components (e.g., JButton, JFrame).

User-Defined Packages
These are custom packages created by users to organize their own classes and interfaces according to
their project's structure.
How to Create:
To create a package, you declare it at the beginning of the Java source file using the package keyword,
followed by the package name.
package mypackage;
public class MyClass {
// Class code here
}

§User-defined packages are bundles of groups of classes or interfaces created by the programmer for
their purpose.

Packages in Java Working Mechanism
Package_name.sub_package_name.class_name
E.g. 
Built-in packages
     java.awt.event
User-defined packages
   University.Department.Staff

What is class???
A class is an entity that determines how an object will behave and what the object will contain.
In other words, it is a blueprint or a set of instruction to build a specific type of object. Contain
variables(fields), methods, objects.
Syntax:
Class <class_name>{
field;
method;
}

Data Types in Java
Data Type is a special keyword used to allocate sufficient memory space for the data. In other
words, Data Type is used for representing the data in main memory (RAM) of the computer.
Java has two categories in which data types are segregated:
Primitive Data Type: such as boolean, char, int, short, byte, long, float, and double.
Non-Primitive Data Type or Object Data type or reference type: such as String, Classes, Arrays,
Interfaces.

Primitive Data Types
1.boolean Data Type
The boolean data type represents a single bit of information and can hold one of two possible
values: true or false. This data type is used for simple flags that track true/false conditions where
its default value is false.
boolean x = true;
2. char Data Type
The char data type is a single 16-bit Unicode character, which represents a wide range of
characters from different languages and symbols. With a range '\u0000' (or 0) to '\uffff' (or
65,535 inclusive). This data type is primarily used to store individual characters.
char y = 'A'

Primitive Data Types
3. int Data Type
The int data type is a 32-bit signed two's complement integer, allowing for a wide range of values
from -2,147,483,648 (-231) to 2,147,483,647 (inclusive) (231 -1). Here the integer is generally
used as the default data type for integral values unless there is a concern about memory. The
default value for an int variable is 0.
int a = 100000; int b = -200000;
4. Short Data Type
The short data type is a 16-bit signed two's complement integer, which provides a range of values
from -32,768 (-215) to 32,767 (inclusive) (215 -1). Like the byte data type, the short data type is
also beneficial for saving memory, as it occupies less space compared to an integer, being only
half the size. The default value for a short variable is 0.
short s = 10000; short r = -20000;

Primitive Data Types
5. byte Data Type
The byte data type is an 8-bit signed two's complement integer with a minimum value of -128 (-
27) and a maximum value of 127 (inclusive) (27 -1). The default value of a byte variable is 0,
which is used to save space in large arrays, which is mainly beneficial in integers since a byte is
four times smaller than an integer.
byte a = 100; byte b = -50;
6. long Data Type
The long data type is a 64-bit signed two's complement integer, capable of representing a vast
range of values from -9,223,372,036,854,775,808 (-263) to 9,223,372,036,854,775,807
(inclusive) (263 -1). This data type is used when a wider range than int is needed, where its
default value is 0L.
long a = 100000L; long b = -200000L;

Primitive Data Types
7. float Data Type
The float data type is a single-precision 32-bit IEEE 754 floating-point representation. It is
particularly useful for saving memory in large arrays of floating-point numbers. Its default value
is 0.0f. However, it's important to note that the float data type is not suitable for precise values,
such as currency, due to potential rounding errors in floating-point arithmetic.
float f1 = 234.5f;
8. double Data Type
The double data type is a double-precision 64-bit IEEE 754 floating-point representation, which
is generally used as the default data type for decimal values, generally the default choice. Double
data type should never be used for precise values such as currency, where its default value is
0.0d.
double d1 = 123.43267543454;

Non-Primitive Data Types
1. String
A sequence of characters. Although String is a class, it is widely used as if it were a primitive data
type because it’s so fundamental to Java. Strings are immutable, meaning their values cannot be
changed after they are created.
String x = "Hello, Java!";
2. Array
Arrays are collections of elements of the same data type (either primitive or non-primitive).
Once an array is created, its size cannot change.
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob", "Charlie"};

Non-Primitive Data Types
3. Class
A class is a blueprint for creating objects, providing structure and behavior for instances of that
class. User-defined classes can have fields, methods, constructors, and more.
class Person {
String name;
int age;
}

Non-Primitive Data Types
4. Interfaces
An interface defines a contract for classes to implement. It contains abstract methods that must
be defined in any class that implements the interface.
interface Animal {
void sound();
}
Tags