Download Intro to Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Scala (programming language) wikipedia , lookup

Java syntax wikipedia , lookup

Object-oriented programming wikipedia , lookup

C++ wikipedia , lookup

Class (computer programming) wikipedia , lookup

Name mangling wikipedia , lookup

Java (programming language) wikipedia , lookup

C Sharp syntax wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Java performance wikipedia , lookup

Transcript
Introduction to
Java Programming
Key Benefits of Java
• Java is “write once, run anywhere”
– architecture neutral
– portable across different platforms
– Due to Java Virtual Machine (JVM)
• Security features
– highly configurable security levels
prevent any piece of Java code doing
harm to the host system
Key Benefits of Java
• Network-centric platform
– easy to work with resources across a
network and to create network based
applications
• Object Oriented
– an interacting collection of independent
software components
– dynamic extensible programs
Key Benefits of Java
• Internationalisation
– uses 16 bit Unicode characters that
represents the phonetic and ideographic
character sets of the entire world
• Performance
– although an interpreted language Java
programs run almost as fast as native C,
C++ programs
• Simple and easy to develop
– powerful & well designed set of APIs
JVM
1001100101001
…
…
class myCode {
…
…
…
…
}
Compiled by
Java
compiler
myCode.class
Bytecode
Interpreted
by JVM
myCode.java
Source
Code
Application
runs
JVM
• JVM provides the run time
environment for the bytecode
(Java Runtime Environment JRE)
– executes the bytecode and causes native
machine code instructions to execute on
the CPU that the JVM is on
 each target platform needs an
implementation of the JVM
Basic Program structure
• Basic class definition
class className {
}
// field declarations
…
// method declarations
…
• Each program needs a main method to tell
the program where to start executing
accessible
to all
classes
(info hiding)
Simple Java Program
public class HelloWorld{
// no fields
indicates
class
main
method
method
returns
nothing
comman
d line
args
//
public static void main (String [] args){
invoking
a
member
System.out.println("Hello World..");
}
}
Objects
• An object includes state (fields) and
behaviour (methods)
• A class object is the blueprint for
the instance objects
• All code must be included in a class
– no inline functions like C++
An Example Class
a business
class
predefined
public
class Student {
Java class
// member fields
Stringnoname;
return
reference to
// constructor
type
the object itself name) {
public Student(String
this.name=name;
}
// methods
void printDetails(){
String
concatenation
System.out.println("\nName: “ + name);
}
}
Instantiation
• Class definition creates “class object”
at runtime
• To instantiate “instance objects” use
new operator
ClassName myInstance = new ClassName();
where ClassName() is a constructor
Note: no need to allocate the memory
for the object like in C++
Using a Class in a Program
the program
control class
source file called
myProg.java
public class myProg {
public static void main(String args []){
case sensitive
// instantiate a Student
object
Student student= new Student("Joe
Bloggs");
// invoke printDetails method
student.printDetails();
}
}
Using the JDK
• Create source files
for each class in
your program
• The name of source
file should be the
same as the name of
class
public class myCode {
…
…
…
…
}
myCode.java
Source File
Compiling your source code
• Compile each class source
file into bytecode (class
files)
• To compile a java source
file
javac myCode.java
• This creates a classfile
called myCode.class
1001101001110101011
…
…
…
…
myCode.class
Class File
To run your program
• To start your program running you run the
bytecode of the program control class
• The program control class has the main
method
• To run bytecode – pass it to the JVM
java classFileName
e.g. java myProg
note no .class included