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

Introduction To Java: Object-Oriented Programming

1) The document introduces Java as an object-oriented programming language that was created in 1991 and can compile code into bytecode that runs on any Java Virtual Machine (JVM). 2) It provides an example "HelloWorld" Java application to demonstrate how to write, compile, and run a simple Java program. 3) The document outlines some key aspects of writing Java code, including using multiple classes, compiling and running applications, and the basic structure of a Java class with a main method.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Introduction To Java: Object-Oriented Programming

1) The document introduces Java as an object-oriented programming language that was created in 1991 and can compile code into bytecode that runs on any Java Virtual Machine (JVM). 2) It provides an example "HelloWorld" Java application to demonstrate how to write, compile, and run a simple Java program. 3) The document outlines some key aspects of writing Java code, including using multiple classes, compiling and running applications, and the basic structure of a Java class with a main method.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Introduction to Java

Object-Oriented Programming

Outline





Brief introduction of the language and platform


Sample programs
Code structure
How to compile and run a program

Readings:
 HFJ: Ch.1, Ch.5.
 GT: Ch. 2.

Introduction to Java

Java


1991: developed by Sun Microsystems as a small


programming language for embedded household devices
 Initially called Oak
Java 1.0.2, 1.1
 Write Once, Run Anywhere
 very slow
 became popular with webpages running applets
Java 2 (versions 1.2 - 1.4)
 much faster, powerful,
 3 platforms: J2ME, J2SE, J2EE
Java 5, 6, 7, 8 (versions 1.5-1.6)
 more powerful
Introduction to Java

Why Java?




Object-oriented
Portability
Safe
 Data always initialized, references always typesafe
 Access to "private" or "package private" data and
methods is rigidly controlled.
 Addresses the weaknesses of older programming
languages
Built-in multi-threading
Introduction to Java

Introduction to Java

Compiling and interpreting






Program source code compiled into bytecode


Bytecode is platform-independent
Bytecode is executed in an interpreter environment
(virtual machine)
Java Virtual Machine
for Windows

Java
program

Compiler

Java
bytecode
program

Java Virtual Machine


for Linux

Java Virtual Machine


for Mac OS
Introduction to Java

Java Virtual Machine (JVM)







Virtual machines depend on specific platforms


(hardware, OS)
Provide Java programs with (platform-independent)
run-time environments
Ensure system security
Normally provided as software
 JRE - Java Runtime Environment
Java platform: JVM + APIs

Introduction to Java

Types of Java applications







Desktop applications Java Standard Edition (J2SE)


 Java Application: normal Java application running
on desktops; console or GUI
 Java Applet: embedded application running within
Web browsers
Server applications Java Enterprise Edition (J2EE)
 Webservices, JSP and Servlet
Mobile applications Java Microedition (J2ME)
Java Card

Introduction to Java

Java Desktop Applications




Focus of this course

Complete application programs


Console or GUI
Launched by java command




Introduction to Java

HelloWorld application
same name with the class
HelloWorld.java:

this is a class

class name

public class HelloWorld {

start of the class


method name

public static void main (String[] args) {


System.out.println("Hello, world");

a statement
it says print to
standard output

}
}

end of the class

public, so that
everyone can access
Introduction to Java

10

Compile and run


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

Compile HelloWorld.java
javac HelloWorld.java
 Run
java HelloWorld


compiler

HelloWorld.class

%> javac HelloWorld.java


%> java HelloWorld
Hello, world
Introduction to Java

11

Application with more than one class


Two classes in separated files
TestGreeting.java:

Greeting.java:

public class TestGreeting {


public static void main(String[] args) {
Greeting gr = new Greeting();
gr.greet();
}
}
public class Greeting {
public void greet() {
System.out.print("Hi there!");
}
}

Introduction to Java

12

Compile and run


Compile
javac TestGreeting.java
 Greeting.java is automatically compiled
 Run
java TestGreeting


%> javac TestGreeting.java


%> java TestGreeting
Hi there!

Introduction to Java

13

JDK Java Development Kit






Free development and run-time environment


Most widely used Java software development kit
Main components:
 javac
compiler, converts source code into Java
bytecode






java

interpreter and application loader

javadoc documentation generator, automatically


generates documentation from source code comments
jdb
debugger

Introduction to Java

14

Code structure

Introduction to Java

15

Writing a class with a main





In Java, everything goes in a class.


When you run a program, you run a class:
 load the class then start executing the class's
main() method
 the class MUST have a main() method!

Introduction to Java

16

What can we do in a method




Looping

while (x > 12) {


x = x 1;
}
for (int x = 0; x < 10; x++) {
System.out.print(x);
}

Conditional branching
if (x == 2) {
System.out.println("x must be 2");
} else {
System.out.println("x is not 2");
}
Introduction to Java

17

What else can we do?







do-while?
switch?
int, long, float, double, boolean?
other syntactical stuff?

Read text books!

Introduction to Java

18

You might also like