Anda di halaman 1dari 28

Java Programming

Prepared by: Ms. Aileen Rose S. Nadela-Cruz


Introduction

• Java was developed by James Ghosling, Patrick


Naughton, Mike Sheridan at Sun Microsystems Inc. in
1991. It took 18 months to develop the first working
version.
• The initial name was Oak but it was renamed to Java in
1995 as OAK was a registered trademark of another Tech
company
JVM: Java Virtual Machine

• Java is platform independent.


 This means that it will run on just about any operating
system (Windows, Linux, Mac OS).
• Java virtual Machine(JVM) is a virtual machine that provides
runtime environment to execute java byte code.
• The JVM doesn't understand Java typo, that's why you
compile your *.java files to obtain *.class files that contain
the bytecodes understandable by the JVM
• JVM controls the execution of every Java program.
JVM Architecture
The Java Virtual Machine (CONT.)
• Class Loader : Class loader loads the Class for
execution.
• Method area : Stores pre-class structure as constant
pool; field and method data; the code for methods
and constructors, including the special methods
used in class, instance, and interface initialization.
• Heap : Heap is in which objects are allocated.
• Stack : Local variables and partial results are store here.
• Program register : Program register holds the address
of JVM instruction currently being executed.
The Java Virtual Machine (CONT.)
• Native method stack : It contains all native used in
application; also called C stacks
• Executive Engine : Execution engine controls the
execute of instructions contained in the methods of the
classes.
• Native Method Interface : Native method interface
gives an interface between java code and native code
during execution.
• Native Method Libraries : Native Libraries consist of
files required for the execution of native code
JRE: Java Runtime Environment

• The Java Runtime Environment (JRE) provides the


libraries, the Java Virtual Machine, and other
components to run applets and applications written
in the Java programming language. JRE does not
contain tools and utilities such as compilers or
debuggers for developing applets and applications.
JRE: Java Runtime Environment
(CONT.)
JDK: Java Development Kit

• JDK : The JDK also called Java Development Kit is a


superset of the JRE, and contains everything that is
in the JRE, plus tools such as the compilers and
debuggers necessary for developing applets and
applications.
JDK: Java Development Kit
(CONT.)
Simple Program: Hello World

class Hello
{
public static void main(String[] args)
{
System.out.println ("Hello World program");
}
}
Simple Program: Hello World
• class : class keyword is used to declare classes in
Java
• public : It is an access specifier. Public means this
function is visible to all. Access Specifiers are also
known as Visibility Specifiers, regulate access to
classes, fields and methods in Java. These specifiers
determine whether a field or method in a class, can
be used or invoked by another method in another
class or sub-class. Access Specifiers can be used to
restrict access.
Simple Program: Hello World

• Types of Access Specifiers


1. public
2. private
3. protected
4. default(no specifier)
Simple Program: Hello World

• static : static is a keyword used to make a function


static. To execute a static function you do not have
to create an Object of the class. The main() method
here is called by JVM, without creating any object
for class.
Simple Program: Hello World

• void : It is the return type, meaning this function


will not return anything
Simple Program: Hello World

• main : main() method is the most important


method in a Java program. This is the method which
is executed, hence all the logic must be inside the
main() method. If a java class is not having a main()
method, it causes compilation error.
• String[] args : declares parameters named args. It
is an array of objects of the type String
Simple Program: Hello World

• System.out.println : This is used to print anything


on the console
Identifiers in Java
• Name used for classes, methods, interfaces and
variables are called Identifier. Identifier must follow
some rules. Here are the rules:
• All identifiers must start with either a letter( a to z or
A to Z ) or currency character($) or an underscore
(_).
• After the first character, an identifier can have any
combination of characters.
• A Java keyword cannot be used as an identifier.
• Identifiers in Java are case sensitive.
Variables in Java

 Java Programming language defines mainly three


kind of variables.
• Instance variables
• Static Variables
• Local Variables
Variables in Java (CONT.)

Instance Variables
• Instance variables are variables that are declare
inside a class but outside any method, constructor
or block.
Variables in Java (CONT.)
Static Variables
• Class variables also known as static variables are
declared with the static keyword in a class, but
outside a method, constructor or a block.
• Static variables are initialized only once at the start
of the execution . These variables will be initialized
first, before the initialization of any instance
variables
• A single copy to be shared by all instances of the
class
Variables in Java (CONT.)

Local Variables
• Local variables are declared in method constructor
or blocks. Local variables are initialized when
method or constructor block start and will be
destroyed once its end.
Concept of Array in Java
• An array is a collection of similar data types.
• Array is a container object that hold values of
homogenous type.
• It is also known as static data structure because size
of an array must be specified at the time of its
declaration.
• An array can be either primitive or reference type. It
gets memory in heap area. Index of array starts
from zero to size-1.
Concept of Array in Java (CONT.)

Array Declaration
datatype[] identifier;
or
datatype identifier[];

Initialization of Array
new operator is used to initialize an array.
Access Level and Modifiers in Java

• Each object has members (members can be variable


and methods) which can be declared to have
specific access.
• Java has 4 access level (public, private, protected,
default) and 3 access modifiers (static, final,
abstract).
Access Level in Java (CONT.)

• Visible to the package. No modifiers are needed.


(default)
• Visible to the class only (private).
• Visible to the world (public).
• Visible to the package and all subclasses
(protected).
Access Level in Java (CONT.)

• static: static can be used for members of class.


final: This modifier applicable to class, method and
variables. This modifier tells compiler not to change
value of variable once assigned.
• abstract: There are situations in which you will want
to define a super class that declares the structure of
a given abstraction without providing a complete
implementation of every method. This modifier is
applicable to class and methods only.

Anda mungkin juga menyukai