Download presentation
1
Programming in Java Unit 1
2
After studying this unit, you should be able to:
Learning outcome: LO1: Understand the principles of programming in Java LO2: Be able to design Java solutions Assessment criteria: AC1.1: Discuss the principles, characteristics and features of programming in Java. AC2.1: Critically evaluate the environmental flexibility of programming in Java Learning objectives After studying this unit, you should be able to: Understand the principles of programming in Java Understand the Java environment Understand the role of Java in distributed networking applications
3
Java development environment
The Java Virtual Machine: Java is platform independent. Programs written in Java can be compiled just once and be run on any operating system by making use of the Java Virtual Machine (JVM). JVM is part of the Java Development Kit (JDK) and the foundation of the Java platform. A virtual machine is a software application that that simulates a computer but hides the underlying operating system and hardware from the programs that interact with it. The JVM understands the environment and version of Java in which bytecode were compiled. The JVM is invoked by the java command. For example to execute a Java application called Mike, using the Command Prompt window you type javac Mike.java to use the Javac compiler to convert the java file Mike.java to a bytecode file Mike.class. Then you run the compiled file by typing java Mike, and this then executes the JVM and therefore will initiate all necessary steps to execute the program.
4
5 Phases in programming Edit Compile Load Verify Execute
This is where you create your program. It consists of editing a file with an editor program (editor). This means you type a Java program using the editor, you can therefore make any changes and save it on any secondary storage device with file extension .java. For example Mike.java. Compile In this phase you use the command javac (Java compiler) to compile a program. For example javac Mike.java within the command prompt. This means that the Java compiler will translate Java source code into byte codes. Byte codes will be executed by the Java Virtual machine. Load At this stage the JVM places the program in memory to execute it (loading). This basically means that the JVM’s class loader takes the .class files containing the byte code of the program and puts them into primary memory. Verify As the classes are loaded, the byte code verifier checks the byte code to ensure that they are valid and do not violate security restrictions. Execute JVM executes the program’s byte code i.e. performing actions specified by the program. Before JVM was an interpreter for Java byte codes. This had a problem of causing the programs to execute slowly. Nowadays they use Just In Time compilation (JIT).
6
Java class libraries/API packages
These are the pre-defined classes that are grouped into categories of related classes called packages. All together they form the Java Application Programming Interface or the Java class libraries. A package is also defined as a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. The next table shows some of the examples of packages and their descriptions.
7
program to be customized to locales.
Package Description java.applet The Java Applet Package contains a class and several interfaces for creating Java applets - programs that execute in web browsers. java.awt The Java Abstract Window Toolkit Package contains the classes and interfaces required to create and manipulate GUIs in early versions of Java. In current versions of Java, the Swing GUI components of the javax.swing packages are typically used instead. java.awt.event The Java Abstract Window Toolkit Event Package contains classes and interfaces that enable event handling for GUI components in both the java.awt and javax.swing packages. java.awt.geom The Java 2D Shapes Package contains classes and interfaces for working with Java’s advanced two-dimensional graphics capabilities. java.io The Java Input/Output Package contains classes and interfaces that enable programs to input and output data java.lang The Java Language Package contains classes and interfaces that are required by many Java programs. This package is imported by the compiler into all programs. java.net The Java Networking Package contains classes and interfaces that enable programs to communicate via computer networks like the Internet. java.text The Java Text Package contains classes and interfaces that enable programs to manipulate numbers, dates, characters and strings. The package provides internationalisation capabilities that enable a program to be customized to locales. java.util The Java Utilities Package contains utility classes and interfaces that enable such actions as date and time manipulations, random-number processing and the storing and processing of large amounts of data. javax.swing The Java Swing GUI Components Package contains classes and interfaces for Java’s Swing GUI components that provide support for portable GUIs. javax.swing.event The Java Swing Event Package contains classes and interfaces that enable event handling (e.g., responding to button clicks) for GUI components in package javax.swing
8
Variables In most programming languages, variables are strongly-typed.
What this means is that you need a specific type of variable to store a specific type of value. You cannot, for example, store a name in a variable intended to hold numbers. The following represents the different types of variables in object oriented programming: Instance variables: These are variables that are used to store the state of an object (for example, color). Every object created from a class definition would have its own copy of the variable. They are variables within the class but declared outside any method. The variable is valid for and occupies storage for as long as the corresponding object is in memory. Lines 7 and 8 in the code below show how instance variables to describe every Car object are declared in a class called Car.
10
Class variables: These variables are explicitly defined within the class-level scope with a static modifier. Because these variables are defined with the static modifier, there would always be a single copy of these variables no matter how many times the class has been instantiated. They live as long as the class is loaded in memory. Line 10 in the code below shows a declaration of a static variable model in a class Car.
11
Parameters or Arguments:
These are variables passed into a method signature (for example, maxSpeed). They are not attached to modifiers i.e. public, private, protected or static. They can be used everywhere in the method. They are in memory during the execution of the method and can't be used after the method returns. Line 12 in the code below shows how an argument maxSpeed is used in a method called accelerate.
12
Local variables: These variables are defined and used specifically within the method-level scope (for example, currentSpeed) but not in the method signature. They do not have any modifiers attached to it. They are destroyed when method finishes execution completely. Code below shows the method accelerate that has one argument maxSpeed. Line 14 shows a declaration of a local variable currentSpeed that can only be used inside the method accelerate.
13
Data types During run time, variables and all the information they store are kept in the computer's memory for access. Think of a computer's memory as a table of data — where each cell corresponds to a variable. Upon creating a variable, we basically create a new address space and give it a unique name. Java goes one step further and lets you define what you can place within the variable — in which is called variable data type. So, you essentially have to do two things in order to create a variable: Create a variable by giving it a unique name. Define a data type for the variable.
14
Line 14 below shows an example of a variable declaration.
The name of the variable is currentSpeed. The variable has been declared as of type int, meaning currentSpeed is a location in the computer memory that is ready to hold any integer value assigned or placed in it during program execution.
15
Data types are divided into two - primitive and reference types.
Every variable, whether it’s of a primitive type or of a class type, is implemented as a location in computer memory. For a variable of a primitive type, the value of the variable is stored in the memory location assigned to the variable. So, if an integer variable is declared as “int x = 3″, then when we look at the memory location of “x”, there will be a “3″ stored there just as expected.
16
However, a variable of a reference type only stores the memory address of where the object is located - not the values inside the object. So, if we have a class called “SomeClass”, when we create an object like this: “SomeClass anObject”, then when we look at “anObject” in memory, we will see that it does not store any of the variables that belong to that object in memory. Instead, the variable “anObject” just stores an address of another place in memory where all the details of “anObject” reside. This means that the object named by the variable is stored in some other location in memory and the variable contains only the memory address of where the object is stored. This memory address is called a reference to the object.
17
Java data types
18
Type Description Example string Used to store a sequence of characters, such as a word or sentence String name =”Michael”; int Used to store an integer cannot be used to store floating point numbers int passMark = 60; double Used to store a decimal number double pi = 3.14; char Used to store a single character char gender=”m”; bool Used to store a true or false value bool passed = true;
19
OO Programming: Objects
Everywhere you look in the real world you see objects—people, animals, plants, cars, planes, buildings, and computers and so on. Humans think in terms of objects. Telephones, houses, traffic lights, microwave ovens and water coolers are just a few more objects. An object has characteristics -Attributes and behaviors. Let us analyze a ball as an object. An attribute is a piece of information about an object; hence the ball has attributes color, size and weight. The ball also exhibits behaviors e.g. a ball can bounce, roll and inflate.
20
An object is defined as an instance of a class.
A computer program consists of a collection of objects interacting with each other by sending messages to achieve certain results. Object-oriented programming (OOP) involves programming using objects. An object is defined as an instance of a class. When we create a new object, we say that we have instantiated that object. Another word for instantiated is created. An object-oriented programming language combines many separate self- contained objects to form a complete program. Each object contains its own program logic which defines what the object is and how it can interact with other objects. In a nutshell, an object has a unique identity, state and behavior.
21
A state of an object, also known as properties or attributes is represented by data fields with their current value. For example, a rectangle object has data fields width and height, which are the properties that characterize a rectangle. width height
22
Attributes An attribute is a piece of information about an object.
Attributes are a property of objects that helps describe an object, giving it state. e.g. a car object can be described using its color and year, with values red and respectively (state of the object) used to describe the car object. Attributes are also referred to as instance variables. A variable represents a location in memory that stores a value used during run time. All variables are of a certain data type e.g. a variable name may contain a value “Mike” which is of type String.
23
Classes A class is a template for creating objects.
A class contains characteristics i.e. attributes and behaviors that are common amongst all objects created by the same class. Code below shows an example of a class called Car used to create various car objects.
24
According to the Car class, each car object will have the following characteristics:
Attributes: speed year model Methods/Behaviours: reverse() accelerate (int maxSpeed) brake()
25
Methods A method is also known as a ‘function’, ‘behavior’ or ‘operation’. We will be using the terms interchangeably throughout the context of the guide. Behavior is something which an object does. A behavior is also known as operation that is defined by methods. Methods are used to ask the object to perform an action. For example, a method called getArea() can be invoked on the rectangle class to return its area.
26
Events Any system or program that is created consists of events.
An event is anything that occurs within the context of the program, for example a button being clicked.
27
Event handlers When an event occurs, the program reacts through methods known as ‘event handlers’. For example, if a user clicks on a login button, an event occurs to log the user into the account. The event handler is the back-end section of the program which controls the event and logs the user into the account.
28
Object oriented programming principles
Abstraction: An ‘abstraction’ is an idea or concept which is not associated with one specific object. In programming languages, we create abstract templates (classes) which store the essential attributes and behaviors for a type of object. For example, you could create an abstract Vehicle class which would define all the attributes and behaviors common to all vehicles. Because the Vehicle class would be abstract, you would not be able to create objects from it. It is more of a template from which you can create other classes through inheritance (which can then be used to create objects).
29
Encapsulation: Encapsulation, also known as information-hiding, is a principle of object oriented programming which states that all the attributes and behaviors of an object should be grouped together in one data type or class. This means objects may know how to communicate with one another across well- defined interfaces but are not allowed to know how other objects are implemented, thus implementation details are hidden within the objects themselves.
30
Inheritance: Inheritance is a core principle of object-oriented programming which allows one class to inherit all the attributes and behaviors of another class. This allows you to specialize a class by inheriting from it and providing additional functionality (attributes and behaviors).
31
Polymorphism: The principle of polymorphism allows us to override behaviors inherited from a parent class such that the derived class (the class which inherits from the parent) has behaviors that work differently to the parent class, but share the same name. In a nutshell it allows you to write code that process objects that share the same superclass.
32
Test your knowledge Complete Self review exercises from your e-book, p Complete exercises from the Toolbox: Individual exercise: 2.2 & 2.3. Tutorial folder: Tutorial 1 and Configuring the Environment Lab exercise: 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.