Download presentation
1
Introduction to Java Programming
2
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
3
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
4
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
5
JVM Compiled by Java compiler Interpreted by JVM myCode.class Bytecode
… myCode.class Bytecode Compiled by Java compiler class myCode { … … } myCode.java Interpreted by JVM Source Code Application runs
6
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
7
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
8
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
9
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++
10
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
11
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++
12
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
13
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
14
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 … myCode.class Class File
15
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.