Lecture 1 Core Java

61

Transcript of Lecture 1 Core Java

Page 1: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 1/61

Page 2: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 2/61

Programming Language Levels

• A programming language specifies the words and symbols thatwe can use to write a program

• A programming language employs a set of rules that dictatehow the words and symbols can be put together to form valid

 program statements

• There are three programming language levels: – machine language – assembly language – high-level language

• Each type of CPU has its own specific machine language

• The other levels were created to make it easier for a humanbeing to write programs

Page 3: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 3/61

Language Translators

• Machine language is the only language capable of directlyinstructing the CPU. Every non machine language programinstruction must be translated into machine language prior toexecution. Language Translators convert high-level code intomachine language.

• Interpreters translate one program statement at a time, as theprogram is running.

• Compilers translate a complete program into machine language,

then the machine language program is executed as needed.Because compiled programs run faster than programs that aretranslated line by line by an interpreter, programmers usuallychoose compilers to translate frequently run business programs.

Page 4: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 4/61

Java - An Introduction

• Java - Programming language from Sun Microsystems.

• Java - CPU Independent language

• Created for consumer electronics

• Oak -The predecessor of Java

• Java - James Gosling, Arthur Van Hoff, Andy Bechtolsheim andothers.

• Java is “C++ -- ++ “

• HotJava – The first Java-enabled Web browser

Page 5: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 5/61

Design objectives for the language

 – Simple

 – Object-oriented

 – Distributed

 – Multi-threaded – Platform neutral

 – Robust

 – Secure

 – Scalable

Page 6: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 6/61

Characteristics

• Java is simple

• Java is object-oriented

• Java is distributed

• Java is interpreted

• Java is robust

• Java is secure

• Java is architecture-neutral

• Java is portable

• Java’s performance

• Java is multithreaded

• Java is dynamic

Page 7: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 7/61

The Java Platform

A platform is the hardware and software environment in which aprogram runs. The Java platform differs from most otherplatforms in that it's a software-only platform that runs on top of other, hardware-based platforms. Most other platforms aredescribed as a combination of hardware and operating system.

The Java platform has two components:• The Java Virtual Machine (Java VM)

• The Java Application Programming Interface (Java API)

The Java API is a large collection of ready-made softwarecomponents that provide many useful capabilities, such as

graphical user interface (GUI) widgets. The Java API is groupedinto libraries (packages) of related components which allow you todo various things.

Page 8: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 8/61

The Java Platform

Page 9: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 9/61

Java Virtual Machine

Java is an unusual language because it is both compiled andinterpreted. "With a compiler, you translate a Java programinto an intermediate language called  Java  bytecodes--theplatform-independent codes interpreted by the Javainterpreter. With an interpreter, each Java bytecode

instruction is parsed and run on the computer. Compilationhappens just once; interpretation occurs each time theprogram is executed. This figure illustrates how this works.

Page 10: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 10/61

Java Virtual Machine(cont.)

You can think of Java bytecodes as the machine code instructions forthe Java Virtual Machine (Java VM). Every Java interpreter, whetherit's a Java development tool or a Web browser that can run Javaapplets, is an implementation of the Java VM. The Java VM can also beimplemented in hardware.

Java bytecodes help make "write once, run anywhere" possible.You can compile your Java program into bytecodes on any platformthat has a Java compiler. The bytecodes can then be run on anyimplementation of the Java VM. For example, the same Java programcan run on Windows NT, Solaris, and Macintosh.

Page 11: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 11/61

The Virtual Machine

• Java is both compiled and interpreted

 – Source code is compiled into Java bytecode

 – Which is then interpreted by the Java Virtual Machine (JVM)

 – Therefore bytecode is machine code for the JVM

• Java bytecode can run on any JVM, on any platform

 – …including mobile phones and other hand-held devices

• Networking and distribution are core features

 – In other languages these are additional APIs

 – Makes Java very good for building networked applications, serverside components, etc.

Page 12: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 12/61

Features of the JVM

• The Garbage Collector

 – Java manages memory for you, the developer has no control overthe allocation of memory (unlike in C/C++).

 – This is much simpler and more robust (no chance of memory leaksor corruption)

 – Runs in the background and cleans up memory while application isrunning

• The Just In Time compiler (JIT)

 – Also known as “Hot Spot” 

 – Continually optimises running code to improve performance

 – Can approach the speed of C++ even though its interpreted

Page 13: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 13/61

Features of the JVM

• Security – Java offers very fine control over what an application is allowed to

do – E.g. Read/write files, open sockets to remote machines, discover

information about the users environment, etc –

Used in Java Applets to create a “sandbox”. Stops a rogue appletattacking your machine. – Makes Java very safe, an important feature in distributed systems

• Class Loading – Loading of bytecode into the virtual machine for execution – Code can be read from a local disk, over a network, or the

Internet – Allows downloading of applications and applets on the fly – …and even ‘mobile code’ 

Page 14: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 14/61

Versions of Java

• Java Language vs. Java Platform – Current version of the language is 1.7 – Core language plus additional APIs is called the Java 2

 platform – Three versions of the Java 2 Platform, targeted at different

uses

• Java 2 Micro Edition (J2ME) – Very small Java environment for smart cards, pages,

phones, and set-top boxes – Subset of the standard Java libraries aimed at limited size

and processing power• Java 2 Standard Edition (J2SE)

 – The basic platform, which this course will cover• Java 2 Enterprise Edition (J2EE) – For business applications, web services, mission-critical

systems – Transaction processing, databases, distribution, replication

Page 15: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 15/61

What Can Java Do?

The Java API enables you to write many types of programs. The mostcommon types of programs are probably applets and applications.

An Applet is a program that is embedded in a Web page and can beautomatically downloaded and executed whenever someone uses aWeb browser to access that Web page. Applets still require the Java

VM to execute, but the VM is invisibly loaded by the Web browser.A Java Application is a standalone program that runs directly on theJava platform.

A  server  is special kind of application that serves clients on anetwork. Examples of servers include Web servers, proxy servers,mail servers, print servers, and boot servers.

A  servlet  is similar to an applet since it is a runtime extension of applications. However, instead of working in browsers, servlets runwithin Java servers, configuring or tailoring the server.

Page 16: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 16/61

Why Use Java?

Write less code: Comparisons of program metrics (class counts,method counts, and so on) suggest that a program written in Java canbe four times smaller than the same program in C++.

Write better code: The Java language encourages good codingpractices, and its garbage collection helps you avoid memory leaks.

Develop programs faster: Your development time may be as muchas twice as fast versus writing the same program in C++.

Avoid platform dependencies with 100% Pure Java:

Write once, run anywhere: Because 100% Pure Java programs arecompiled into machine-independent bytecodes, they run consistentlyon any Java platform.

Distribute software more easily: You can upgrade applets easilyfrom a central server. Applets take advantage of the Java feature of allowing new classes to be loaded "on the fly," without recompiling theentire program.

Page 17: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 17/61

Java

Write Once, Run Anywhere

Page 18: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 18/61

The Java Programming Environment

• Compared to C++: – no header files, macros, pointers and references, unions, operator

overloading, templates, etc.

• Object-orientation: Classes + Inheritance

• Distributed : RMI, Servlet, Distributed object programming.

• Robust : Strong typing + no pointer + garbage collection

• Secure: Type-safety + access control

• Architecture neutral : architecture neutral representation

• Portable

• Interpreted 

 – High performance through Just in time compilation + runtimemodification of code

• Multi-threaded 

Page 19: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 19/61

The Java Programming Environment

• Java programming language specification – Syntax of Java programs – Defines different constructs and their semantics

• Java byte code: Intermediate representation for Javaprograms

• Java compiler : Transform Java programs into Java byte code• Java interpreter : Read programs written in Java byte code

and execute them• Java virtual machine: Runtime system that provides various

services to running programs

• Java programming environment : Set of libraries thatprovide services such as GUI, data structures, etc.• Java enabled browsers: Browsers that include a JVM +

ability to load programs from remote hosts

Page 20: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 20/61

Object Oriented Languages -a

Comparison

Feature C++ Ada Java

Encapsulation Yes Yes Yes

Inheritance Yes No YesMultiple Inherit. Yes No NoPolymorphism Yes Yes Yes

Binding (Early/Late) Both Early LateConcurrency

Poor Difficult YesGarbage Collection No No Yes

Class Libraries Yes Limited Yes

Page 21: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 21/61

Java better than C++ ?

• No Typedefs, Defines, or Preprocessor

• No Global Variables

• No Goto statements

• No Pointers

• No Unsafe Structures

• No Multiple Inheritance

• No Operator Overloading

• No Fragile Data Types

Page 22: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 22/61

Removed From C++

• Operator overloading

• Pointers and Array/pointers

• Multiple-inheritance of implementation

• Enum, typedef, #define

• Copy constructors, destructors

• Templates

• And other stuff....

Page 23: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 23/61

Added or Improved over C++

• Interfaces: type Vs. class

• Garbage collection

• Exceptions (More powerful than C++)

• Strings

• Instanceof 

• Package

• Multi-threads

Page 24: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 24/61

Java Integrates

Power of Compiled Languages

andFlexibility of Interpreted Languages

Page 25: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 25/61

Two Types of Java Applications

• Different ways to write/run a Java codes are:

Application- A stand-alone program that can be invoked fromcommand line . A program that has a “mainmain” method

Applet- A program embedded in a web page , to be run when thepage is browsed . A program that contains no “main” method

• Application -Java interpreter

• Applets- Java enabled web browser (Linked to HTML via<APPLET> tag. in html file)

Page 26: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 26/61

Rich Object Environment

• Core Classeslanguage

Utilities

Input/Output

Low-Level Networking

Abstract Graphical User Interface

• Internet ClassesTCP/IP Networking

WWW and HTMLDistributed Programs

Page 27: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 27/61

Java Program Structure

• In the Java programming language:

 – A program is made up of one or more classes

 – A class contains one or more methods

 – A method contains program statements

• These terms will be explored in detail throughout the course

• A Java application always contains a method called main

Page 28: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 28/61

Java Program Structure

// comments about the class

 public class MyProgram 

class headerclass header

class bodyclass body

Comments can be added almost anywhereComments can be added almost anywhere

{

}

Page 29: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 29/61

Java Program Structure

 public class MyProgram 

{

}

 public static void main (String[] args)

{

}

// comments about the class

// comments about the method 

method headermethod headermethod bodymethod body

Page 30: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 30/61

Comments

• Comments in a program are also called inline documentation

• They should be included to explain the purpose of the program anddescribe processing steps

• They do not affect how a program works

•Java comments can take two forms:

// this comment runs to the end of the line

/* this comment runs to the terminatingsymbol, even across line breaks */

/** This kind of comment is a special* ‘javadoc’ style comment*/

Page 31: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 31/61

Identifiers

• Identifiers are the words a programmer uses in a program

• An identifier can be made up of letters, digits, the underscorecharacter (_), and the dollar sign

• They cannot begin with a digit

• Java is case sensitive, therefore Total and total are differentidentifiers

Page 32: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 32/61

Identifiers

• Sometimes we choose identifiers ourselves when writing aprogram

• Sometimes we are using another programmer's code, so weuse the identifiers that they chose

• Often we use special identifiers called reserved words thatalready have a predefined meaning in the language

• A reserved word cannot be used in any other way

Page 33: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 33/61

Reserved Words

abstract

 boolean

 break

 byte byvalue

case

cast

catch

char

classconst

continue

default

do

double

elseextends

false

final

finally

float

forfuture

generic

goto

if

implements

importinner

instanceof

int

interface

long

nativenew

null

operator

outer

 package

 private protected 

 public

rest

return

short

staticsuper

switch

synchronized 

this

throw

throwstransient

true

try

 var

 void 

 volatilewhile

Page 34: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 34/61

White Space

• Spaces, blank lines, and tabs are collectively called whitespace

• White space is used to separate words and symbols in aprogram

• Extra white space is ignored

• A valid Java program can be formatted many different ways

• Programs should be formatted to enhance readability, usingconsistent indentation

Page 35: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 35/61

Primitive Data Types

In Java, everything is an object with the exception of thesimple data types (primitives). Unlike objects, primitive typesare data objects that:

• Consist of a single value

• Are manipulated by using operators

• Cannot be extended by creating new types or operators

• Store values (object types store references)

Page 36: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 36/61

Primitive Data

• There are exactly eight primitive data types in Java

• Four of them represent integers:

 – byte, short, int, long

• Two of them represent floating point numbers:

 – float, double

• One of them represents characters:

 – char

• And one of them represents boolean values:

 –boolean

Page 37: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 37/61

Numeric Primitive Data

• The difference between the various numeric primitive types istheir size, and therefore the values they can store:

• Prefixes: byte, short, int, long, float, double

Type

byte

short

intlong

float

double

Storage

8 bits

16 bits

32 bits64 bits

32 bits

64 bits

Min Value

-128

-32,768

-2,147,483,648< -9 x 1018

+/- 3.4 x 1038 with 7 significant digits

+/- 1.7 x 10308 with 15 significant digits

Max Value

127

32,767

2,147,483,647> 9 x 1018

Page 38: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 38/61

Characters

• A char variable stores a single character from the Unicodecharacter set 

• A character set  is an ordered list of characters, and eachcharacter corresponds to a unique number

• The Unicode character set uses sixteen bits per character,allowing for 65,536 unique characters

• It is an international character set, containing symbols andcharacters from many world languages

• Character literals are delimited by single quotes:

'a' 'X' '7' '$' ',' '\n‘

Page 39: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 39/61

Characters

• The ASCII character set is older and smaller than Unicode, but is stillquite popular

• The ASCII characters are a subset of the Unicode character set,including:

uppercase letters

lowercase letters

punctuation

digits

special symbols

control characters

A, B, C, …

a, b, c, …

period, semi-colon, …

0, 1, 2, …

&, |, \, …

carriage return, tab, ...

Page 40: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 40/61

Boolean

• A boolean value represents a true or false condition

• A boolean can also be used to represent any two states, suchas a light bulb being on or off 

• The reserved words true and false are the only validvalues for a boolean type

boolean done = false;

Page 41: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 41/61

Variables

• A variable can be given an initial value in the declaration

int sum = 0;

int base = 32, max = 149;

When a variable is referenced in a program, its current value is used

Page 42: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 42/61

Assignment

• An assignment statement changes the value of a variable

• The assignment operator is the = sign

total = 55;

he expression on the right is evaluated and the result is stored in the variable on the lef 

The value that was in total is overwritten

You can only assign a value to a variable that is consistent with the variable's declared

type

Page 43: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 43/61

Constants

• A constant is an identifier that is similar to a variable exceptthat it holds one value for its entire existence

• The compiler will issue an error if you try to change a constant

• In Java, we use the final modifier to declare a constant

final int MIN_HEIGHT = 69;

• Constants:

 – give names to otherwise unclear literal values

 – facilitate changes to the code

 – prevent inadvertent errors

Page 44: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 44/61

Arithmetic Expressions

• An expression is a combination of operators and operands

•  Arithmetic expressions compute numeric results and make useof the arithmetic operators:

Addition +

Subtraction -

Multiplication *

Division /

Remainder %

If either or both operands to an arithmetic operator are floating point,

the result is a floating point

Page 45: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 45/61

Bitwise Operators

a<<Left shift<<

a>>>Right shift withzero fill

>>>

a>>Righ shift>>

a^bXor^

a|bOr|

a&bAnd& 

~aUnary Not~

Java ExpressionJava OperationBitwise Op

Page 46: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 46/61

Relational Operators

>=b>=

a<=b<=

a>b>

a<b<

a!=b!=

a==bTest forequality

==

Java ExpressionJava OperationRelational Op

Page 47: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 47/61

Logical Operators

a&&bShort circuitand

&& 

a||bShort circuit or||

!aUnary not!

a!=bNot equal to!=

a|bLogical or|

a&bLogical and& 

Java ExpressionJava OperationLogical Op

Page 48: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 48/61

Division and Remainder

• If both operands to the division operator (/) are integers, theresult is an integer (the fractional part is discarded)

14 / 3 equals?

8 / 12 equals?

4

0

The remainder operator (%) returns the remainder after dividing the second

operand into the first

14 % 3 equals?

8 % 12 equals?

2

8

Page 49: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 49/61

Operator Precedence

• Operators can be combined into complex expressions

result = total + count / max - offset;

• Operators have a well-defined precedence which determinesthe order in which they are evaluated

• Multiplication, division, and remainder are evaluated prior toaddition, subtraction, and string concatenation

• Arithmetic operators with the same precedence are evaluatedfrom left to right

• Parentheses can always be used to force the evaluation order

Page 50: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 50/61

Development Environments

• There are many development environments which developJava software:

 – Sun Java Software Development Kit (SDK)

 – Netbeans

 – Eclipse

 – Borland JBuilder

 – Symantec Café

 – WebSphere Studio

• Though the details of these environments differ, the basic

compilation and execution process is essentially the same

Page 51: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 51/61

Syntax and Semantics

• The syntax rules of a language define how we can putsymbols, reserved words, and identifiers together to make avalid program

• The semantics of a program statement define what that

statement means (its purpose or role in a program)• A program that is syntactically correct is not necessarily

logically (semantically) correct

• A program will always do what we tell it to do, not what wemeant to tell it to do

Page 52: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 52/61

Errors

• A program can have three types of errors

• The compiler will find problems with syntax and other basicissues (compile-time errors)

 – If compile-time errors exist, an executable version of the

program is not created• A problem can occur during program execution, such as trying

to divide by zero, which causes a program to terminateabnormally (run-time errors)

• A program may run, but produce incorrect results (logical 

errors)

Page 53: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 53/61

Problem Solving

• The purpose of writing a program is to solve a problem

• The general steps in problem solving are:

 – Understand the problem

 – Dissect the problem into manageable pieces

 – Design a solution

 – Consider alternatives to the solution and refine it

 – Implement the solution

 – Test the solution and fix any problems that exist

Page 54: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 54/61

Coding Guidelines

• coding standards:

 – Variable and method names (also add prefixes discussedearlier)

 – Class names

 – Constant names – Braces

 – Comments

 – Source File organization

Page 55: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 55/61

Getting Started with Java Programming

• A Simple Java Application

• Compiling Programs

• Executing Applications

Page 56: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 56/61

A Simple Java Application

• Define a class HelloWorld and store it

into a file: HelloWorld.java:

 public class HelloWorld {

 public static void main (String[]

args) {

System.out.println(“Hello,

 World”);

}

}

Page 57: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 57/61

Compiling Programs

• On command line

 – javac file.java

 

Source Code

Create/Modify Source Code

Compile Source Code

i.e. javac Welcome.java

Bytecode

Run Byteode

i.e. java Welcome

Result

If compilation errors

If runtime errors or incorrect result

Page 58: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 58/61

Executing Applications

• On command line

 – java classname

JavaInterpreter 

on Windows

JavaInterpreter 

on Sun Solaris

JavaInterpreter 

on Linux

Bytecode

...

Page 59: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 59/61

• Compile HelloWorld.java

javac HelloWorld.java

Output: HelloWorld.class

• Run

java HelloWorld 

Output: Hello, World 

Page 60: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 60/61

Life Cycle of Java Code

• Using Sun Java SDK

Command Line Interface

Editor 

SourceFile(s)

(.java)

Editor 

VirtualMachine

(java)

Compiler 

(javac)

ClassFile(s)

(.class)

Program

executes

Parts of Sun Java SDK

Programmer 

Program is

created inPhase 1 DiskEditor

Lifecycle of Java Code

Page 61: Lecture 1 Core Java

8/3/2019 Lecture 1 Core Java

http://slidepdf.com/reader/full/lecture-1-core-java 61/61

created in

the editor and 

stored 

on disk.Compiler creates

 bytecodes and stores

them on disk.

Class loader puts

 bytecodes in memory.

Bytecode verifier

confirms that all

 bytecodes are valid 

and do not violate

Java’s security

restrictions.

Interpreter reads

 bytecodes and 

translates them into a

language that the

computer can

understand, possibly

storing data values as

Phase 1

Phase 2 

Phase 3 

Phase 4

Phase 5 

DiskEditor

Compiler

Class Loader

Disk

Disk

Primary

 Memory

.

.

.

.

.

.

Primary

 Memory

.

.

.

.

.

.

Primary

 Memory

..

Bytecode Verifier

Interpreter