lab#1 (14/3/1431h) introduction to java programming cs425 prepared by: i.raniah alghamdi
TRANSCRIPT
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)) .
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.
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.
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.
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
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“.
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
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.
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.
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.
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.
CPIT 425, Spring 2010
13
Good References
http://java.sun.com/docs/books/tutorial/java/index.html
GOOD LUCK