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

Object Oriented Programming - Course

The document discusses Java programming language and how to write, compile, and execute Java programs. It describes that Java was developed by Sun Microsystems in 1991 and led to an object-oriented language. It also outlines the basic properties of Java programs, including that they consist of classes, objects, and methods. The document then provides a step-by-step process for writing a simple Java program, compiling it, and executing the bytecode.

Uploaded by

crengutabogdan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Object Oriented Programming - Course

The document discusses Java programming language and how to write, compile, and execute Java programs. It describes that Java was developed by Sun Microsystems in 1991 and led to an object-oriented language. It also outlines the basic properties of Java programs, including that they consist of classes, objects, and methods. The document then provides a step-by-step process for writing a simple Java program, compiling it, and executing the bytecode.

Uploaded by

crengutabogdan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Course 1 OOP

Introduction

Java programming language


Properties of Java
Writing, compiling, execution of program

2021
Java programming language

 Java was developed by Sun Microsystems after a project started in 1991, led by
James Gosling. The project resulted in an object-oriented language, which the
Sun company called Java.

 A primary goal of Java is to write programs that can be executed on a variety of


computers and equipment containing computers. This quality is called software
portability ( "write once, run anywhere " ). However differences between
compilers, Java virtual machines and between computers, make portability
difficult to obtain. The simple use of Java does not guarantee portability.

 Sun Microsystems was bought by Oracle in 2009.

 We can create in our programs each class and method that we need. However,
most developers use Java class collections in so-called predefined Java class
libraries known as Java APIs (Application Programming Interfaces)

 
Properties of Java

 A Java program consists of code module called classes.


 During the execution, a program consists of a set of objects
which communicate among them by messages
 Object = structure (data) + behavior (methods)
 Class = set of objects
 Classes can be organized in trees based on the UML
generalization/specialization relation
 Java programs can be distributed over the network and
therefore adapted for use on the Internet and intranet
 Java programs are interpreted. A Java Virtual Machine (JVM) is
used that interprets a compiled program code
 Is a competing language because it includes facilities that allow
the creation and management of competing threads.
Java programs

 The process to write and execute a Java program is:

 Step 1. Create the program. This step consist in editing one or more files
with a text editor. The file is stored on a storage device (hard disk) with the
extension .java.

 Step 2. The Java program is compiled in bytecode. The compiler produces


files with the extension .class .
Java programs
(continuation)

Step 3. Loading the program in memory. At this stage, Java Virtual Machine
(JVM ) loads the program into memory to run. The class loader of the JVM takes
the .class files containing bytecode encoding the program and transfer them into
RAM . The class loader also loads all necessary .class files, provided by Java.
The .class files can be loaded from the local computer or a network (local or
Internet).
Java programs
(continuation)

Step 4. Checking the bytecode. At this stage, as the classes are loaded, the
bytecode verifier examines their bytecodes, to ensure they are valid and do not
violate Java 's security restrictions.
Java programs
(continuation)

Step 5. The program execution.

In this phase, JVM executes the bytecodes of the program, such performing
the actions specified by the program.

In the initial versions of Java, JVM was a simple interpreter for bytecodes.

This made most Java programs to run slowly because JVM interpreted and
executed one bytecode at a time.

With the current computer architectures, computers can execute instructions in


parallel.

The current JVM’s run bytecode using a combination of interpretation with a


so-called just-in-time compilation (JIT) .
Java programs
(continuation)

 A first example of a simple Java program:

public class P01 {


public static void main(String[] args) {
System.out.println("Hello world");
}
}

 There is defined the class P01 which contains only the method main. The
file .java must have the same name with the public class, P01.java.

 Each program consists of at least one class that must be defined. The
keyword class begins the class declaration and is immediately followed by the
class name (P01).

 Keywords are reserved for use by Java and must be written in lowercase only.
Java programs
(continuation)

 A class name is an identifier - a character sequence consisting of letters,


digits, underscores and dollar sign, and does not start with a digit.

 Normally, an identifier that does not start with a capital letter, is not a class
name.

 The open bracket { marks the beginning of the class definition body . A
corresponding close brace } should close the class declaration.

 Line 2 represents the starting point of any Java applications.

 The parentheses after main identifier indicates that it is a method.


Java programs
(continuation)

 Normally, a class declaration contains one or more methods. For a Java


application, one method must have the name main and must be defined as in
the example above; otherwise , Java Virtual Machine (JVM ) will not run the
application.

 The methods perform operations that can return information at the end of
these operations. The keyword void indicates that the method will not return
any information.

 The parameter String [ ] args specified in parentheses is mandatory when


declare the main method. The open brace begins the body of the method
declaration and closed corresponding accolade it closes it.

 Line 3 of the above Java program, commands the computer to perform an


action, namely, to display the string delimited by quotes. The variable
System.out refers an object from PrintStream that contains a bytes output flow
to the monitor’s screen (i.e. standard output). It allows Java applications to
display information in the display window.
Java programs
(continuation)

 The method System.out.println displays a line of text in the display window.


The string in parenthesis is the argument ( or parameter ) of the method.
When the method System.out.println completes its display, it places the
cursor of the display window at the beginning of the next line.
 
 There is a similar method called print that leaves the cursor at the end of the
text displayed.
 
 The method System.out.printf displays formatted data. The instruction
 
System.out.printf( "%s\n%s\n", "Welcome to ", "Java Programming!" );
 
uses this method to display the strings "Welcome to " and
"Java Programming "

 The call of the method specifies three arguments. When a method requires
several arguments, they are placed in the list with a comma as separator.

You might also like