lab#1 (14/3/1431h) introduction to java programming cs425 prepared by: i.raniah alghamdi

13
LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

Upload: norma-horton

Post on 18-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

LAB#1 (14/3/1431H)INTRODUCTION TO JAVA PROGRAMMING CS425

Prepared By: I.Raniah Alghamdi

Page 2: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

CPIT 425, Spring 2010

2

About Java

Java (with a capital J) is a high-level, third generation programming language.

Java is most similar to C++ in syntax. Java has considerably more functionality than C,

primarily because of the large class library. It is platform independent programming

language (Write once, run every where) : The Java compiler produces a special format called byte

code. Java byte code can run on any Java Virtual Machine

(JVM) regardless of computer architecture Byte code still need an interpreter to execute them on

any given platform (ex: Sun's program java (with a little j)) .

Page 3: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

CPIT 425, Spring 2010

3

Java is Simple and Safe

Java was designed to make it much easier to write bug free code by: keeping the language simple. It is automatic memory management.

Java uses an automatic garbage collector to manage memory in the object lifecycle.

There are no pointers in Java. Java programs cannot access arbitrary

addresses in memory.

Page 4: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

CPIT 425, Spring 2010

4

Java is Simple and Safe

Java has strong typing Variables must be declared Casts are strictly limited to casts between types that

make sense.int to a long √byte to a short √long to a boolean X int to a String. X

implements a robust exception handling mechanism

Java enabled web browser checks the byte codes of an applet to verify that it doesn't do anything nasty before it will run the applet.

Page 5: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

CPIT 425, Spring 2010

5

Most special in Java Java lets you write special programs called applets that

can be downloaded from the Internet and played safely within a web browser.

Traditional computer programs : have far too much access to your system (downloaded and

executed) Even more dangerous software would be promulgated if any

web page you visited could run programs on your system. Java solves this problem by restricting what an applet

can do. A Java applet cannot write to your hard disk without your

permission. It cannot write to arbitrary addresses in memory and

thereby introduce a virus into your computer. It should not crash your system.

Page 6: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

CPIT 425, Spring 2010

6

Java is Object-Oriented

An Object : it consists of state and related behavior. An object stores its state in fields and exposes its behavior through methods.

A Class : it is the blueprint from which individual objects are created.

Inheritance: Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. use the extends keyword, followed by the name of the class to inherit from:

class subClass extends superClass

Page 7: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

CPIT 425, Spring 2010

7

Java is Object-Oriented Cont.

Interface: just like classes except that they use the word interface instead of class and is restricted to containing method declarations without bodies.Interface interface_name { }

And to use the interface in the class definition:Public class class_name implements interface_name { }

A package: is a namespace that organizes a set of related classes and interfaces. The Java platform provides an enormous class library (a set of packages). This library is known as the "Application Programming Interface", or "API“.

Page 8: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

CPIT 425, Spring 2010

8

Language Basics

Variables Instance Variables (Non-Static Fields) Class Variables (Static Fields) Local Variables (method’s variables) Parameters

Primitive Data Types: byte, short, int, long, float, double, boolean, char.

Arrays

Page 9: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

CPIT 425, Spring 2010

9

Java Development Kit (JDK)

(JDK) is a Sun Microsystems product aimed at Java developers.

Some of JDK contents: java – The loader for Java applications. This tool is an

interpreter and can interpret the class files generated by the javac compiler.

javac – The compiler, which converts source code into Java bytecode

jar – The archiver, which packages related class libraries into a single JAR file.

jdb – The debugger appletviewer – This tool can be used to run and debug

Java applets without a web browser.

Page 10: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

CPIT 425, Spring 2010

10

Reading from a file

For writing a java program to read file line by line: Use import java.io.*; Define FileInputStream object to obtain input

bytes from a file in a file system (to open the file). Define DataInputStream object to let an

application read primitive Java data types from an underlying input stream in a machine-independent way.

Define InputStreamReader object which is used as a bridge from byte streams to character streams.

Page 11: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

CPIT 425, Spring 2010

11

Reading from a file Cont.

Define BufferedReader object to Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

Read File Line By Line using readLine() method of BufferedReader object.

Close the input stream.

Page 12: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

CPIT 425, Spring 2010

12

Need to do…

Practice to write to a file… You will need that later in your HWs.

For writing to a file, you will need to use FileOutputStream object.

Page 13: LAB#1 (14/3/1431H) INTRODUCTION TO JAVA PROGRAMMING CS425 Prepared By: I.Raniah Alghamdi

CPIT 425, Spring 2010

13

Good References

http://java.sun.com/docs/books/tutorial/java/index.html

GOOD LUCK