SWC2333 Topic 2 JAVA Programming Basic
SWC2333 Topic 2 JAVA Programming Basic
ORIENTED PROGRAMMING
TOPIC 2 – JAVA PROGRAMMING BASICS
2
2.1 Java Data Types
4
2.1 Java Data Types Primitive Data Types
5
2.1 Java Data Types Primitive Data Types
6
2.1 Java Data Types Primitive Data Types
7
2.1 Java Data Types Primitive Data Types
8
2.1 Java Data Types Primitive Data Types
9
2.1 Java Data Types Primitive Data Types
10
2.1 Java Data Types Primitive Data Types
11
2.1 Java Data Types Primitive Data Types
12
2.1 Java Data Types Primitive Data Types
13
2.1 Java Data Types Reference Data Types
14
2.1 Java Data Types
Reference Data Types
classes
15
2.1 Java Data Types
Reference Data Types
classes
▪ Class declarations can include these components, in
order:
1. Modifiers : A class can be public or has default access.
Refer to access specifiers for classes or interfaces in
Java
2. Class name: The name should begin with an initial letter
(capitalized by convention).
16
Reference Data Types
2.1 Java Data Types
classes
Reference Data
Types
classes
18
2.1 Java Data Types
Reference Data Types
objects
19
2.1 Java Data Types
Reference Data Types
objects
▪ An object consists of :
1. State : It is represented by the attributes of an object. It
also reflects the properties of an object.
2. Behavior : It is represented by the methods of an object.
It also reflects the response of an object to other
objects.
3. Identity : It gives a unique name to an object and
enables one object to interact with other objects.
20
2.1 Java Data Types
Reference Data
Types
objects
21
Reference Data Types
2.1 Java Data Types
strings
23
2.1 Java Data Types
Reference Data Types
arrays
▪ An Array is a group of like-typed variables that are referred to
by a common name. Arrays in Java work differently than they
do in C/C++.
▪ In Java, all arrays are dynamically allocated. (discussed below)
▪ Since arrays are objects in Java, we can find their length using
member length. This is different from C/C++ where we find
length using size.
▪ A Java array variable can also be declared like other variables
24
with [ ] after the data type.
2.1 Java Data Types
Reference Data Types
arrays
▪ The variables in the array are ordered and each has an index
beginning with 0.
▪ Java array can also be used as a static field, a local variable, or
a method parameter.
▪ The size of an array must be specified by an int value and not
long or short.
▪ The direct superclass of an array type is Object.
25
2.1 Java Data Types
Reference Data Types
arrays
26
2.2. Input / Output Operations
Reading Input:
▪ User Input: Capturing data entered by the user
through the console or GUI components.
▪ File Input: Reading data from files stored on the disk.
▪ Network Input: Receiving data from network
connections (e.g., web services, sockets).
▪ Stream Input: Reading data from different input
streams like System.in.
28
2.2. Input / Output Operations
Functions of Input/Output
Operations
Writing Output:
▪ Console Output: Displaying messages to the console.
▪ File Output: Writing data to files for storage or later
use.
▪ Network Output: Sending data over network
connections.
▪ Stream Output: Writing data to different output
streams like System.out.
29
2.2. Input / Output Operations Console I/O
30
2.2. Input / Output Operations Console I/O
31
2.2. Input / Output Operations Dialog Box I/O
32
2.2. Input / Output Operations Summary
▪ Ease of Use: Console I/O operations are simpler and faster for basic
tasks, whereas dialog boxes provide a more interactive and user-
friendly way to handle input and output.
▪ User Experience: Dialog boxes can enhance user experience by
providing visual prompts and handling user interactions in a
graphical way.
▪ Context of Use: Console I/O is often used in command-line
applications or beginner-level coding exercises. In contrast, dialog
boxes are used in GUI applications where interaction with the user
is more sophisticated.
33
2.3 Output Formatting
34
2.3 Output Formatting
35
2.4 Packages
36
2.4 Packages Why we use packages?
38
How to Create and Use
2.4 Packages
Packages
Creating a Package: Use the package keyword at the top of your
Java file.
39
How to Create and Use
2.4 Packages
Packages
Using a Package: Import the package using the import keyword.
40
2.4 Packages Default Package
42
2.4 Packages Standard Java Packages
44
SUMMARY
▪ Fssdsd
▪ Default Package: For small or temporary projects with no explicit
package declaration. Avoid in larger, complex projects.
▪ Standard Java Packages: Predefined packages provided by Java,
like java.util, java.io, etc., which offer a wide range of built-in
functionality.
45
“Object-oriented programming offers a
sustainable way to write spaghetti code. It lets
you accrete programs as a series of patches.
Large organizations always tend to develop
software this way, and I expect this to be as
true in a hundred years as it is today.”
― Paul Graham, Hackers & Painters: Big Ideas from the
Computer Age
46