Lab#1 (14/3/1431h) Introduction To java programming cs425

carenextSoftware and s/w Development

Nov 18, 2013 (3 years and 9 months ago)

126 views

LAB#
1
(
14
/
3
/
1431
H)

INTRODUCTION TO JAVA
PROGRAMMING

CS
425

Prepared By:
I.Raniah

Alghamdi

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)) .


2

CPIT
425
, Spring
2010

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.



3

CPIT 425, Spring 2010

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.


4

CPIT
425
, Spring
2010

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.



5

CPIT 425, Spring 2010

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

6

CPIT
425
, Spring
2010

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“.

7

CPIT 425, Spring 2010

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


8

CPIT
425
, Spring
2010

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.


9

CPIT 425, Spring 2010

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.


10

CPIT
425
, Spring
2010

Reading from a file Cont.

CPIT
425
, Spring
2010

11


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.

Need to do…

CPIT
425
, Spring
2010

12


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.

Good References


http://java.sun.com/docs/books/tutorial/java/inde
x.html



GOOD LUCK


13

CPIT
425
, Spring
2010