Vous êtes sur la page 1sur 49

COM 2305

Java Programming Language


Lecture 1 Introduction
Faculty of Applied Sciences Rajarata University of Sri Lanka

M.K.C.Surangika B.Sc.(RJT), M.Sc.(MRT)


1

Learning Objective
At

the end of this course you should be able to design and implement real life computer programs using Java.

Main Reading
Teach Yourself Java 2 platform by Laura Lemay, and Rogers Cadenhead, Techmedia India, 1999. ISBN 81-7635-243-8. Program Design, 4th edition by Peter Juliff, Prentice Hall India, 2000. ISBN-81-2031622-3. The Java Handbook by Patrick Naughton, Tata McGraw-Hill India, 2001. ISBN 0-07463290-6. Java 2 The Complete Reference Fifth Edition by Herbert Schildt

Web Reading

Thinking in Java (3rd Edition) Bruce Eckel (Downloadable) http://www.mindview.net/Books/TIJ/ Official Sun Java site (Downloadable) http://java.sun.com/docs/books/tutorial/in dex.html

Evaluation Criteria

Assignments - 10%
Project - 10%

Mid Exam paper - 10%


End Practical Exam - 20% End Theory Exam - 50%
5

Project

Introduction
The purpose of the system is to provide the facility for the bank to give the services to the customers. In general scenario the bank is facing so many problems while doing the transactions in manually. To overcome such problem you have to develop a system called banking system to facilitate the bank needs.

The main functionalities of the system are:


Create New Employee Create New Account ( Savings Account, Current Account) Update account ( Deposit, Withdraw, Transfer) Show account Details Check Balance Check Transaction Exit (Submit last week of the semester)

Generation of Programming Languages

Write a small report on generation of programming languages.

Introduction to Java
Java was developed by James Gosling at Sun Microsystems in 1991. The Java language was successfully used to develop a web browser called WebRunner and Java/Hotjava project was commenced. In December 1995, beta version2 of Java was released.(Experimental level)

Introduction to Java
On January 23, 1996 Java 1.0 was officially released and made available to download over the net. Latest version of Java 6 SDK and Documentation can be downloaded at Java.sun.com

Java Buzzwords

Simple Object-oriented Distributed Interpreted Robust Secure Architecture-neutral Portable High performance Multithreaded Dynamic
10

Running Java Programs

Introduction to Java Development Kit (JDK) JDK provides core set of tools that are necessary to develop professional Java applications JDK tools are also written in Java.

11

Java Tools

The Java Compiler (javac) The Java Interpreter (java) The Java Debugger (jdb) The Java Documentation Tool (javadoc) The Java Disassembler (javap) The Appletviewer (appletviewer) The RMI Stub Compiler (rmic) The RMI Remote Object Registry (rmiregistry) The Serial Version Command (serialver) The Native to Ascii Converter (native2ascii) The Java Archive Tool (jar) The Java Security Tool (javakey)
12

Java Terminology
Java Virtual Machine (JVM) set of computer software programs and data structures that use a virtual machine model for the execution of other computer programs and scripts. Java Runtime Environment (JRE) A runtime environment which implements Java Virtual Machine, and provides all class libraries and other facilities necessary to execute Java programs. This is the software on your computer that actually runs Java programs.
13

Java Terminology (cont.)


Java Development Kit (JDK) The basic tools necessary to compile, document, and package Java programs (javac, javadoc, and jar, respectively). The JDK includes a complete JRE. Just-In-Time Compiler (JIT) Part of the JVM that is used to speed up the execution time. Application Programming Interface (API) Contains predefined classes and interfaces for developing Java programs.

14

The Java Platform


A platform is the hardware or software environment in which a program runs. Microsoft Windows, Linux, Solaris OS, and Mac OS etc. The Java platform differs from most other platforms in that it's a software-only platform that runs on top of other hardware-based platforms.

15

The Java Platform

The Java platform has two components: The Java Virtual Machine The Java Application Programming Interface (API)

16

Java Platforms
There are three main platforms for Java: Java SE (short for Standard Edition) runs on desktops and laptops

Java ME (short for Micro Edition) runs on mobile devices such as cell phones
Java EE (short for Enterprise Edition) runs on servers
17

Running Java Programs

Java language program

class ILikeJava { public static void main(String[] args) { System.out.println(I Like Java); } }

Java Compiler

Java Byte Code

Java Liibrary

01101001010101 10111101010111

Java Interpreter
18

Running Java Programs

Creating a Java Source File Any plain text editor or text editor capable of saving in ASCII format can be used to create a Source file(Notepad) Source File should be saved with a .java extension This is very important java source file must have the same name as the class they define (including the same upper- and lowercase letters) Save the file somewhere on your disk with the name Hello.java for the following program.
/* This program display I Like Java on the computer Screen */ class Hello{ public static void main(String[] args) { System.out.println(I Like Java); } }
19

Running Java Programs


Setting The Path
To set the Java Environment

Control Panel->System->advanced ->Environment Variables


Learn how to set the path and classpath

20

Running Java Programs


Compiling Source File
Assuming you saved your source file in Practical Directory;

C:\Practical>Javac Hello.java
Java Compiler will create a Java byte code file

Java Compiler

Source File Name


ensure to use Same name as the class Name
21

Running Java Programs

Running the Source File


- To execute byte code file

C:\Practical>Java Hello
Interpreter Execute the Java Byte code class file

Java Interpreter

Class Name

22

A Closer Look at the First Sample Program


/* This program display I Like Java on the computer Screen */ class Hello{ public static void main(String[] args) { System.out.println(I Like Java); } }

Comment- The contents of a comment are ignored by the compiler. Three type of comment in java. 1. //text // This comment extends to the end of the line. // This type of comment is called a "slash-slash" comment

23

A Closer Look at the First Sample Program

/* text */ /* This comment, a "slash-star" comment, includes multiple lines.*/ 3. /** documentation */ /** The last type of comment is the Javadoc comment. This comment type has some guidelines that allows a Javadoc reader to display information about a Java method or class by using special tags: @param myNum - describe what the parameter myNum is used for @return - describe what this method returns 24 */
2.

A Closer Look at the First Sample Program


keyword class to declare that a new class is being defined. Hello is an identifier that is the name of the class. The main( ) method is simply an entry point for an application.

25

Exercise: Write a simple Java program which displays following two lines on the screen. I like Java programming. So I do practical.

26

Progress Check
1. Where does a Java program begin execution? 2. What does System.out.println( ) do? 3. What is the name of the Java compiler? Of the Java interpreter?

27

Progress Check
Answers 1. main( ) 2. Outputs information to the console. 3. The standard Java compiler is javac.exe; the interpreter is java.exe.

28

Statements, Expressions & Variables

Write a simple Java program which adds 12 and 13, and displays the result on the screen.

class Addition { public static void main(String[] args) { int a=12; int b=13; int c; c=a+b; System.out.println(a+b=+c); } }
29

Statements

Statements are used to accomplish simplest tasks in Java forms simplest Java operations. can be single line or Span to multiple lines. does not necessarily return a value.
System.out.println(a+b=+c);
30

Expressions

Expressions simplest form of Statements. returns a value when evaluated. can be assigned to a variable or can be tested in Java statements. most expressions are a combination of Operators & Operands
c=a+b;
31

Variables

What is a Variable ? Variables are locations in memory in which values can be stored. Each Variable has a Type, Name and a Value After Declaring, it can be used to store values of the appropriate Type
int a=12;
32

Variables
Variables can be of reference type or Primitive Type. There are Eight Primitive Data Types in Java. Integer Types (4) Character Floating Point types (2) Boolean Why does Java have different data types for integers and floating-point values? That is, why arent all numeric values just the same type?

33

Variables

Integer Types
Type Byte Bit Size 8

Floating Point Types


Type Float Bit Size 32

Short Int long

16 32 64

double

64

34

Variables
Primitive Data Types
Data Type Size Min Value
-128
-32768 -2147483648

Max Value
127
32767 2147483647

byte
short int

8 bits
16 bits 32 bits

long Float
double char

64 bits 32 bits
64 bits 16 bits

-9223372036854775808
1.40239846E-45 4.94065645841246544E-324 \u0000

9223372036854775807
3.40282347E+8 1.79769313486231570E+ 308 \uFFFF

boolean

n/a

True or False
35

Variables
Reference Data Type

Reference types (composite)


objects arrays

strings are supported by a built-in class named String (java.lang.String)

36

Variable
class Variables { public static void main (String args[]) { boolean b = true; int low = 1; long middle = 74; float pi = 3.1415292; double e = 2.71828; char s = a; System.out.println() //print the values } }

37

Variables
Variable Naming Can only Start with a Letter, Underscore (_) , or Dollar Sign ($) After the first letter Name can include any letter or Number but cannot include Symbols such as %, @, * and so on. Names are Case Sensitive (Value,VALUE and value are different)
38

Variables
Use meaningful Names such as number, areaOfCircle, firstName Convention is to start a variable with a lower case If the name is made up of several words, then from the second word onwards the first letter of the word is capital.
Illegal Names 1More, #Two, @Two Legal Names timeOfDay, temp_val, $_
39

Variable
Write down whether the following variable names are legal? &name _postalCode *age %company Age #$date Address $_month 8year $lastName

40

Variable
Declaring variables
type identifier[= value] [,identifier [=value]]
Class Square{ Public static void main(String args[]) { Double a = 3.5, b = 4.8 ; Double c = a*b ; System.out.println(Value of c is +c); } }
41

class ExampleDiv { public static void main(String args[]) { int var; // this declares an int variable double x; // this declares a floating-point variable var = 10; // assign var the value 10 x = 10.0; // assign x the value 10.0 System.out.println("Original value of var: " + var); System.out.println("Original value of x: " + x); System.out.println(); // print a blank line var = var / 4; x = x / 4; System.out.println("var after division: " + var); System.out.println("x after division: " + x); } }

42

The output from this program is show here:


Original value of var: 10 Original value of x: 10.0

var after division: 2 x after division: 2.5

43

Escape Sequence Characters


Escape Sequence \t \b \n \r \ \ \\ \f Definition
Tab Backspace New line Carriage return Single quote Double quote Backslash Form feed
44

Type Conversions
Conversion between integer types and floating point types. this includes char No automatic conversion from or to the type boolean. You can force conversions with a cast . int i = (int) 1.345;

45

Type Conversions
class typeConversion { public static void main(String args[]) { int a = 65; char c = A; double b = 3.5; //a=b; //b=a; //a=c; //c=a; //b=c; //c=b; //a=(int)b; int i = (int) 1.345; System.out.println(i);

}
}
46

Summary
Today we learned History, Features Java statements, expressions and variables
Exercise: Write a simple Java program which divides 12 by 5 and displays the result on the screen. Write a simple Java program multiplies 12.4 and 45678932. You should display the result on the screen up to two decimal points Write a program which converts inches to centimeters (1m= 39 inches)
47

Questions

48

Thank You

49

Vous aimerez peut-être aussi