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

SWC2333 Topic 2 JAVA Programming Basic

The document covers the basics of Java programming, focusing on data types, input/output operations, output formatting, and packages. It explains primitive and reference data types, how to handle user input and output through console and dialog boxes, and the importance of organizing code using packages. Additionally, it highlights the use of standard Java packages and the drawbacks of the default package in larger projects.

Uploaded by

norhasiah akhir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SWC2333 Topic 2 JAVA Programming Basic

The document covers the basics of Java programming, focusing on data types, input/output operations, output formatting, and packages. It explains primitive and reference data types, how to handle user input and output through console and dialog boxes, and the importance of organizing code using packages. Additionally, it highlights the use of standard Java packages and the drawbacks of the default package in larger projects.

Uploaded by

norhasiah akhir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

SWC2333 – OBJECT-

ORIENTED PROGRAMMING
TOPIC 2 – JAVA PROGRAMMING BASICS

Prepared by Puan Norhasiah Mohd Akhir for Semester 1224


TOPIC CONTENT
2.1 Data Types
o Primitive
o Reference
2.2 Input/Output operations
o Console
o Dialog box
2.3 Output Formatting
2.4 Packages

2
2.1 Java Data Types

▪ Java data types are the foundation of data manipulation


in Java programming.
▪ They define the size and type of values that can be
stored in a variable.
▪ Java is a statically typed language, meaning every
variable must be declared with a data type before use.
▪ Java data types are categorized into two main groups:
primitive data types and reference data types.
3
2.1 Java Data Types

4
2.1 Java Data Types Primitive Data Types

▪ Primitive data types are the most basic data types


available in Java. There are eight primitive data types,
each serving a specific purpose.

byte short Int long float

double boolean char

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

▪ Reference data types or non-primitive data types are


objects that store references to the actual data.
▪ They include classes, objects, strings and arrays.
▪ Unlike primitive data types, reference data types do not
store the value directly.

classes objects strings arrays

14
2.1 Java Data Types
Reference Data Types
classes

▪ A Class is a user-defined blueprint or prototype


from which objects are created. It represents
the set of properties or methods that are
common to all objects of one type.

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

▪ Class declarations can include these components, in order:


3. Superclass(if any): The name of the class’s parent
(superclass), if any, preceded by the keyword extends. A class
can only extend (subclass) one parent.
4. Interfaces(if any): A comma-separated list of interfaces
implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one interface.
5. Body: The class body is surrounded by braces, { }.
17
2.1 Java Data Types

Reference Data
Types

classes

18
2.1 Java Data Types
Reference Data Types
objects

▪ An Object is a basic unit of Object-Oriented


Programming and represents real-life entities.
▪ A typical Java program creates many objects which
interact by invoking methods.

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

▪ Strings are defined as an array of characters.


▪ The difference between a character array and a string
in Java is, that the string is designed to hold a sequence
of characters in a single variable.
▪ A character array is a collection of separate char-type
entities. Unlike C/C++, Java strings are not terminated
with a null character.
22
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

▪ Java input/output (I/O) operations are fundamental


in interacting with users and systems - essential for
interacting with the external world.
▪ Allow Java programs to read data from various
sources and write data to various destinations.
▪ In a broad sense, input and output (I/O) operations
in Java can be categorized into two main types:
Console I/O and Dialog Box I/O.
27
2.2. Input / Output Operations
Functions of 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

(INPUT) Reading from the Console: Scanner Class


▪ One of the most common ways to read user input from
the console.

30
2.2. Input / Output Operations Console I/O

(OUTPUT) Writing to the Console:


▪ System.out.print() and System.out.println(): Used to
display output to the console.

31
2.2. Input / Output Operations Dialog Box I/O

(INPUT & OUPUT) Obtaining user input through dialog boxes


using JOptionPane - Part of the javax.swing package

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

▪ Relates to how data is presented to the user, whether it's


through the console or a dialog box.
▪ Console Output Formatting:
▫ System.out.printf() method in Java allows you to format
strings in a structured way. You can specify the width,
precision, and alignment of the output.

34
2.3 Output Formatting

▪ Dialog Box Output Formatting:


▫ Using HTML tags within JOptionPane to format text in the
message dialogs:

35
2.4 Packages

▪ A package is a namespace that organizes a set of related


classes and interfaces.
▪ For example, like a folder in a file directory that groups
together related files.
▪ Packages help in avoiding naming conflicts, making code
modular, easier to maintain, and enhancing access
control.

36
2.4 Packages Why we use packages?

1. Organization: Packages allow you to organize your


classes and interfaces into a directory structure. Helps
in managing a large codebase by logically grouping
related classes.
2. Namespace Management: Packages prevent naming
conflicts by providing a unique namespace for classes.
For example, you can have two classes with the same
name in different packages.
37
2.4 Packages Why we use packages?

3. Access Protection: Packages can control access to


classes, interfaces, and members using access modifiers.
Classes in the same package can access each other’s
package-private (default) and protected members.
4. Reusability: Packages promote code reuse. Classes in a
package can be easily reused by importing the package.

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.

When we don’t specify a package, your class belongs to the default


package. This is generally not recommended for larger projects.

40
2.4 Packages Default Package

▪ Definition: The default package is a special case where you do


not explicitly specify a package name at the beginning of your
Java source file.
▪ Usage: Classes that are part of the default package have no
package declaration and are simply placed in the same
directory.
▪ Drawback: Using the default package is generally discouraged
for larger projects because it can lead to disorganization and
potential naming conflicts. Classes in the default package
cannot be imported by classes from other packages.
41
2.4 Packages Standard Java Packages

▪ Definition: These are predefined packages provided by the Java


Standard Library and are part of the Java Development Kit
(JDK). They include packages like java.lang, java.util, java.io, etc.
▪ Usage: These packages contain a wide range of classes and
interfaces that are essential for Java programming, such as
collections, file I/O, string handling, and more.
▪ Advantage: They provide a well-organized, robust set of tools
and utilities that you can import and use in your programs.

42
2.4 Packages Standard Java Packages

▪ Some common examples include:


▪ java.lang: Automatically imported and contains fundamental
classes like String, System, Math, etc.
▪ java.util: Includes utility classes such as ArrayList,
HashMap, Date, etc.
▪ java.io: For input and output operations (e.g., File,
InputStream, OutputStream).
▪ javax.swing: Contains classes for building graphical user
interfaces (GUIs).
43
2.4 Packages Standard Java Packages

▪ For example – Using classes from these packages directly in


your code by importing it

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy