Lecture 03 v2
Lecture 03 v2
Lecture 03 v2
Ralph Tambala
MUST . CSIT
Lecture 3 1 / 24
Outline
1 Java Language
2 Hello World Program
3 Acces Modifiers
4 Declaring Classes
5 DeclaringAttributes
6 Declaring Methods
7 Declaring Objects
8 Accessing Instance Attributes and Methods
9 Constructors
Lecture 3 2 / 24
Java Language
Lecture 3 3 / 24
Java Language
Java Technology
JDK (Java Development Kit) – A software development kit used for
making applets and Java applications.
JRE (Java Runtime Environment) – A software layer for running Java
programs.
JVM (Java Virtual Machine) – A virtual machine that enables a
computer to run Java programs as well as programs written in other
languages that are also compiled to Java bytecode.
Lecture 3 4 / 24
Java Language
Features (1)
Simple –It is simple to learn and use. Java does not use pointers,
hence easy to write and debug programs. It also provides a simple
and clear approach to achieve a task using OOP concepts.
Pure object-oriented – Java Programs require the use of classes and
objects compulsorily.
Portable – Java compiler generates a set of instructions known as the
bytecode. Bytecode is executed by JVM.
Secure – Java programs are confined to JRE. They can access only
that part of the system which is required for program execution. Data
outside JRE cannot be accessed by Java programs, which makes it
secure.
Robust – Java performs exception handling that compels the
programmer to resolve mistakes during compile time. Memory
allocation and deallocation is automatic, and is done by means of
automatic garbage collection.
Lecture 3 5 / 24
Java Language
Features (2)
Lecture 3 6 / 24
Hello World Program
Hello World
1 /* *
2 * The HelloWorld class implements an application that
3 * simply prints " Hello World !" to standard output .
4 */
5
6 class HelloWorld
7 {
8 public static void main ( String [] args )
9 {
10 System . out . println ( " Hello World ! " ) ; // Display message
11 }
12 }
Listing 1: HelloWorld.java
Lecture 3 7 / 24
Hello World Program
Lecture 3 8 / 24
Hello World Program
Lecture 3 9 / 24
Hello World Program
The keyword class begins the class definition for a class named
HelloWorld.
The class body component contains declarations for the class’s
member variables, and declarations and implemenatations for the
class’s methods.
Lecture 3 10 / 24
Hello World Program
The modifiers public and static can be written in any order, but
the convention is to write public static.
The main method accepts a single argument: an array of elements of
type String. The argument can be given any name, but most
programmers choose “args” or “argv”.
.
Lecture 3 11 / 24
Hello World Program
In short, this statement uses the System class from the core library to
print the "Hello World!" message to standard output.
Lecture 3 12 / 24
Acces Modifiers
Acces Modifiers
Java has 4 access modifiers to set access levels for classes, attributes,
methods and constructors:
1 Default (No keyword) – If we do not provide any keyword explicitly,
the fields in an interface are implicitly public static final and the
methods are public by default.
2 private – Attributes, methods and constructors declared private
can only be accessed within the declared class itself. Class and
interfaces cannot be private.
3 public – Classes, attributes, methods and constructors declared
public can be accessed from any other class. The main method of
an application must be set to public.
4 protected – Attributes, methods and constructors declared
Declaring Classes
Classes are user-defined type. Class members describe the data
(attributes) and the behavior (methods).
Syntax:
<modifier>* class <class_name> {
<attribute_declaration>*
<constructor_declaration>*
<method_declaration>*
}
Example:
1 public class Counter {
2 private int value ; // attribute
3 public int getValue () { // method
4 return value ;
5 }
6 public Counter () {} // constructor
7 }
Lecture 3 14 / 24
DeclaringAttributes
Declaring/Initializing Attributes
Syntax:
// declaring attribute
<modifier>* <type> <attribute_name>;
// initializing attribute
<modifier>* <type> <attribute_name> = <initial_value>;
Example:
1 class Account {
2 private int accountNumber ; // declaration
3 public String accountName ; // declaration
4 private boolean status = false ; // initialization
5 }
Lecture 3 15 / 24
Declaring Methods
Declaring Methods
Syntax:
<modifier>* <return_type> <method_name>( <argument>* ) {
<statement>*
}
Example:
1 public class Counter {
2 public static final int MAX = 100;
3 private int value ;
4 public void inc () {
5 if ( value < MAX ) {
6 ++ value ;
7 }
8 }
9 public int getValue () {
10 return value ;
11 }
12 }
Lecture 3 16 / 24
Declaring Objects
Declaring Objects
An object is created from a class. The new keyword is used to create new
objects.
Syntax:
<class_name> <object_name> = new <class_name>();
Example:
1 public class Student {
2 public String name ;
3 public int year ;
4 public boolean active = true ;
5
6 public static main ( String [] argv ) {
7 Student st1 = new Student () ; // first object
8 Student st2 = new Student () ; // second object
9 }
10 }
Lecture 3 17 / 24
Accessing Instance Attributes and Methods
Syntax:
// creating an object
<class_name> <object_name> = new <class_name>();
Lecture 3 18 / 24
Accessing Instance Attributes and Methods
Example:
1 public class Student {
2 public String name ;
3 public int year ;
4 private boolean active ;
5
6 public boolean setStatus ( boolean status ) {
7 active = status ;
8 }
9
10 public static main ( String [] argv ) {
11 Student st = new Student () ;
12 st . name = " Tiyamike " ; // assigning attr of object st
13 st . setStatus ( true ) ; // calling method of object st
14 }
15 }
Lecture 3 19 / 24
Constructors
Constructors
Lecture 3 20 / 24
Constructors
Constructors
Default
Example:
1 class Student {
2 String name ;
1 class Student {
3
2 String name ;
4 Student () { }
3 }
5 }
Listing 5: Original code Listing 6: Java inserts default
constructor
Lecture 3 21 / 24
Constructors
Constructors
No-arg
Example:
1 class Student {
2 String name ;
3 Student () { // no arguments
4 this . name = " Mpanda Dzina " ; // some code
5 }
6 public static main ( String [] argv ) {
7 Student st = new Student () ;
8 System . out . println ( st . name )
9 }
10 }
Constructors
Parameterized
Example:
1 class Student {
2 String name ;
3 Student ( String fullName ) { // has 1 parameter
4 name = fullName ;
5 }
6 public static main ( String [] argv ) {
7 Student st = new Student ( " Alinafe Tambala " ) ; // ***
8 System . out . println ( st . name )
9 }
10 }
Lecture 3 23 / 24
Constructors
practice
Lecture 3 24 / 24