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 Object Oriented 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 Performance 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 Compiled by Java compiler Interpreted by JVM myCode.class Bytecode 1001100101001 … myCode.class Bytecode Compiled by Java compiler class myCode { … … } myCode.java Interpreted by JVM 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
Simple Java Program public class HelloWorld{ // no fields accessible to all classes (info hiding) Simple Java Program public class HelloWorld{ // no fields // main method public static void main (String [] args){ System.out.println("Hello World.."); } } command line args indicates class method returns nothing invoking a member
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++
reference to the object itself An Example Class a business class predefined Java class public class Student { // member fields String name; // constructor public Student(String name) { this.name=name; } // methods void printDetails(){ System.out.println("\nName: “ + name); } } no return type reference to the object itself String concatenation
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 []){ // instantiate a Student object Student student= new Student("Joe Bloggs"); // invoke printDetails method student.printDetails(); } } case sensitive
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