Sie sind auf Seite 1von 30

Lecture 1

Principles of Programming
in
Java

First Year
Computer Science Insttiute

Rebwar Kh Muhmmed

2019-2020
1
History of Programming Languages

2
Top Viral Programming Languages & Their
Creators...!

3
History

4
History (Cont.)
 Sun Microsystems in1991 funded an internal corporate
research project (Green Project) led by James Gosling.
 Java was officially announced in May 1995.

 A key goal of Java is to write once, run anywhere


 JDK Evolutions

5
Why Computer Programming?
“Everybody in this country should learn how to program a
computer… because it teaches you how to think.”

((Steve Jobs))

6
Why Java?
 Java is a general purpose programming language.
 Java is the Internet programming language.
 Java is a versatile programming language, you can use
 it to develop applications for:
 Desktop computers,
 Servers, and
 Small handheld devices.
Example:The software for Android cell phones is developed using Java.

7
8
Programming Language Level

9
The Java Programming Language
 As a programming language, Java can create all kinds of
applications that you could create using any conventional
programming language.

10
In the Java programming language
 All Java source files must end with the .java extension
 Those source files are then compiled into .class files by the javac compiler.
 it instead contains bytecodes — the machine language of the Java Virtual Machine
(Java VM).
 The java launcher tool then runs your application with an instance of the Java
Virtual Machine.

An overview of the software development process


11
Compiling, running a program

 compiler: A program that translates a computer


program written in one language into an equivalent source code
program in another language (often, but not always, (Hello.java)
translating into machine language). compile
byte code
(Hello.class)
execute
output

12
The Advantage

 the Java VM is available on many different operating systems, the same .class
files are capable of running on Microsoft Windows, Linux, or Mac OS.

Through the Java VM, the same application is capable of


running on multiple platforms
13
Java Editions
 Java Standard Edition (Java SE)
Can be used to develop client-side standalone applications or
applets.
 Java Enterprise Edition (Java EE)
Can be used to develop server-side applications such as Java
servlets and Java ServerPages.
 Java Micro Edition (Java ME)
Can be used to develop applications for mobile devices
such as cell phones.
14
Java Development Kit (JDK) Versions

15
The Java APIs
 An Application Program Interface (API) is a set of
commands, functions, and protocols which
programmers can use when building software.

 It allows programmers to use predefined functions,


instead of writing them from scratch.

16
Java IDE Tools
 Borland JBuilder
 NetBeans Open Source by Sun

 Eclipse Open Source by IBM

 Other tools:

 TextPad Editor

 JCreator LE

 JGrasp

 BlueJ

 DrJava
17
Java Program Development
 Phase 1.0 : Editor
 Phase 2.0 : Compiler
 Phase 3.1 : Class Loader
 Phase 3.2 : Bytecode Verifier
 Phase 3.3 : Java Virtual Machine

Program is edited in a text editor, much as a paper for an


English class is edited in a word processor.

18
Java Program Development: Edit

File naming conventions for Java programs on a Unix system


(most also apply to Windows/Mac):
 Filenames must end with .java in lowercase letters.
 Filenames must match their class names (more info on this
soon).
 Filenames cannot contain spaces. Replace spaces with
initial capital letters: this is fun.java should be named
thisIsFun.java instead.
 Filenames are case sensitive: firstTry.java vs. FirstTry.java
vs. FIRSTTRY.java
 Filenames consist only of letters (A-Z, a-z), digits (0-9),
underscores (_), dashes (-) and periods (.).
19
Structure of Java programs
public class <name> {
public static void main(String[] args) {
<statement(s)>;

}
}

 Every executable Java program consists of a class...


 that contains a method named main...
 that contains the statements to be executed
20
Simple First Program: "Hello World
// This is the first program most people write in a new
// language, the "Hello World!" program. In Java, this
// file must be named Hello.java, with the first part
// of the name –Hello –being the same as the name of
// the class in line below. The filename itself (not
// the class name) must always end in .java to indicate
// to the operating system that it's a java source file.
public class Hello
{
public static void main ( String[] args )
{
System.out.println ( "Hello World!" );
}
}

21
Simple First Program: "Hello World
public // Accessible outside this file. "public" is a reserved word.
class // "class" is a reserved word.

Hello// The name of the class - same name as filename.


{ // Braces instead of begin/end.
public // This function must be accessible. "public" is reserved.

static // "static" is reserved.


void // The return value - nothing. "void" is reserved.
main // To start execution, a class must have a "main" function. //
"main" is sort-of reserved.

(String[] argv) // Command line parameters are placed by the run-time system
// in argv[0], argv[1], etc.

System.out.println ("Hello World!"); // System.out is an object that contains a


useful function called println, which takes in a string and prints it out on a
new line.
22
System.out.println
 Java programs use a statement called
System.out.println to instruct the computer to print
a line of output on the console
 pronounced "print-linn"; sometimes called a println
statement for short

 Two ways to use System.out.println :


1. System.out.println("<Message>");
 Prints the given message as a line of text on the console.

2. System.out.println();
 Prints a blank line on the console.

23
Syntax and syntax errors
 syntax: The set of legal structures and commands that can be used in
a particular programming language.

 syntax error or compiler error: A problem in the structure of a


program that causes the compiler to fail.
 If you type your Java program incorrectly, you may violate Java's
syntax and see a syntax error.

public class Hello {


pooblic static void main(String[] args) {
System.owt.println("Hello, world!")_
} compiler output:
}
H:\summer\Hello.java:2: <identifier> expected
pooblic static void main(String[] args) {
^
H:\summer\Hello.java:5: ';' expected
}
^
2 errors
Tool completed with exit code 1
24
Fixing syntax errors
 Notice how the error messages are cryptic and do not always
help us understand what is wrong:
H:\summer\Hello.java:2: <identifier> expected
pooblic static void main(String[] args) {
^
 We'd have preferred a friendly message such as,
"You misspelled 'public' "
 The compiler does tell us the line number on which it found the
error, which helps us find the place to fix the code.
The line number shown is a good hint, but is not always the
true source of the problem.
 Java has a fairly rigid syntax.
 Several languages are friendlier than Java, but we must pay
this price in order to learn an industrial-strength language
that is used in real jobs.
25
Another Java program
public class Hello2 {
public static void main(String[] args) {
System.out.println("Hello, world!");
System.out.println();
System.out.println("This program produces");
System.out.println("four lines of output");

}
}
 The code in this program instructs the computer to print four messages on the
screen.

26
Comments
.
 Comments in Java can be one of three styles:

 –Single line
 •starts at //anywhere on a line
 •ends at the end of that line
 –Multi-line
 •starts with character sequence /*anywhere
 •ends with character sequence */anywhere after that
 •can span multiple lines

27
Escape sequences
.
 Escape sequences, or escape characters, begin with a slash and are
immediately followed by another character.
 This two-character sequence, inside ""allows you to control your output (\n,
\t, \b) or output characters you wouldn't otherwise be able to (\\, \") inside a
string.

28
Output multiple lines with one output statement

public class Ex1


{
public static void main ( String[] args )
{
System.out.println ("Hello\n\tWorld\n!" );
}
}

29
30

Das könnte Ihnen auch gefallen