Uploaded by Marvin Marquez

L01 - Introduction to Java

advertisement
Lecture 01:
Introduction to Java
CS 214 / IT 213 – Object Oriented Programming
Overview
• Brief History of Java
• What is Java?
• Properties of Java
• JVM, JRE & JDK
• Bytecodes
• Structure of a Java Program
• Hello World Program
History of Java
• It was created by James Gosling from Sun
Microsystems (Sun) in 1991
• It was originally designed for consumer electronic
devices and was called Oak
• Oak was renamed Java in 1994
• The first publicly available version of Java (Java
1.0) was released in 1995
• In 2006 Sun started to make Java available under
the GNU General Public License (GPL).
History of Java
• Sun Microsystems was acquired by the Oracle
Corporation in 2010.
• Oracle continues project called OpenJDK.
• Over time new enhanced versions of Java have
been released.
• The current version of Java is Java 1.8 which is
also known as Java 8.
What is Java?
Java consists of:
• programming language – contains specifications
for writing or coding programs
• compiler – for checking syntax and converting
programs to bytecodes
• core libraries – rich set of APIs that can be reused
and modified
• runtime – to run java programs on various
operating systems
Properties of Java
• Platform Independent – a java program can run
unmodified on all supported platforms, e.g., windows
or linux.
• Object-orientated Programming Language – except the
primitive data types, all elements in Java are objects.
• Strongly-typed Programming Language – the types of
the used variables must be pre-defined and conversion
to other objects is relatively strict.
Properties of Java
• Interpreted And Compiled Language – java source
code is transferred into the bytecode format which
does not depend on the target platform. These
bytecode instructions will be interpreted by the Java
Virtual machine (JVM).
• Automatic Memory Management – java manages the
memory allocation and de-allocation for creating new
objects. The so-called garbage collector automatically
deletes objects to which no active pointer exists.
Java Virtual Machine (JVM)
• An abstract machine that enables your computer to run
a java program
• Written specifically for a specific operating system
• Java programs are compiled by the compiler into
bytecode.
• JVM interprets this bytecode and executes the java
program.
Bytecodes
• It is the machine language of JVM.
• Source code is written on a text file with a .java
extension.
• This source code is compiled by the java compiler and
produces bytecodes with a .class extension.
Java Runtime Environment (JRE)
• It consists of JVM and class libraries. Those contain the
necessary functionality to start java programs.
• JRE is the superset of JVM.
• If you need to run Java programs, but not develop
them, JRE is what you need.
Java Development Kit (JDK)
• Additionally contains the development tools necessary
to create java programs. It consists of a compiler, JVM
and class libraries
• When you download JDK, JRE is also downloaded with
it.
• If you want to develop Java applications, download JDK.
JVM, JRE and JDK
Programming Environment
Structure of a Java Program
Package Statement
Suggested
Optional
Import Statement
Essential
Interface Statements
Optional
Class Definitions
Optional
Documentation Section
MainMethod Class
{
main method definition
}
Compulsory
Hello World!
1. public class HelloWorld{
2. /*
3. My First Java Program
4. */
5. public static void main(String[] args){
6. System.out.println(“Hello World!”);
7. }
8.}
The HelloWorld Java Program
Steps in program coding using a text editor and CMD:
1. Open a text editor such as Notepad++ or
Sublime Text
2. Write or type your source code in the text editor.
3. Save your file as: HelloWorld.java
Don’t forget that your file should have an
extension of .java
The HelloWorld Java Program
4. Compile your program:
a. In Windows, open command prompt(CMD)
b. Go to the folder where your program
HelloWorld.java is saved.
c. To compile, type the command:
javac HelloWorld.java
d. Make sure there are no syntax (or typing)
errors.
The HelloWorld Java Program
5. Running your program:
a. If the compilation from the previous step was
successful, a HelloWorld.class file is created
on the current directory. Check to see if this
file exists.
b. To run your program, type the command:
java HelloWorld
The HelloWorld Java Program
Explanation of the HelloWorld program:
• Line 1: public is an access modifier, class is the
keyword to create class and HelloWorld is the
class name.
• Lines 3 to 4: are comment lines describing what
the class does. These lines are skipped by the
compiler.
The HelloWorld Java Program
• Line 5: A method named main( ) is declared inside
the class.
o All applications must contain at least one main method.
Other methods can be declared but must not be named
“main”
o When the program is run, main( ) is always the first
method to execute.
o All statements inside the main ( ) block are part of the
method
The HelloWorld Java Program
• Line 6: Prints out “Hello World” on the screen
• Line 7: Ends the main( ) method
• Line 8: Ends the HelloWorld class
Download