1 java b object oriented b powerful b interpreted b portable b platform independent b filename.java...
TRANSCRIPT
1
JavaJava
Object OrientedObject Oriented PowerfulPowerful InterpretedInterpreted PortablePortable Platform independentPlatform independent filename.javafilename.java filename.classfilename.class (bytecode)(bytecode)
2
JavaJava
Multi-threadedMulti-threaded Easy to work with multimediaEasy to work with multimedia
• imagesimages• animationanimation• audioaudio• videovideo
Graphics capabilities (and still portable)Graphics capabilities (and still portable) Case sensitiveCase sensitive
3
JavaJava
ApplicationsApplications AppletsApplets
• for the WWWfor the WWW
4
ApplicationApplication
//Java application//Java application
public class HelloWorld {public class HelloWorld {
public static void main(String args[])public static void main(String args[])
{{
System.out.println(“Hello World!…”);System.out.println(“Hello World!…”);
}}
}}
5
ApplicationApplication
Public:Public:• If the class is not If the class is not publicpublic, it can be used only by , it can be used only by
other classes in the same other classes in the same packagepackage A A packagepackage is a collection of other packages is a collection of other packages
and classesand classes
6
ApplicationsApplications
To display a dialog box containing To display a dialog box containing information use the class information use the class JOptionPaneJOptionPane
Class Class JOptionPaneJOptionPane is defined in the is defined in the package called package called javax.swingjavax.swing
e.g.e.g.
import javax.swing.*;import javax.swing.*;
7
ApplicationApplication
import javax.swing.JOptionPane;import javax.swing.JOptionPane;
public class HelloWorld {public class HelloWorld {
public static void main(String args[])public static void main(String args[])
{{
JOptionPane.showMessageDialog(JOptionPane.showMessageDialog(
null, “Hello\nWorld!…”);null, “Hello\nWorld!…”);
}}
}}
8
The The concatination operator “+”concatination operator “+”
int i =5;int i =5;
int j = 6;int j = 6;
JOptionPane.showMessageDialog(JOptionPane.showMessageDialog(
null, “i = ” + i + “j = “ + j);null, “i = ” + i + “j = “ + j);
System.out.println(“x = “ + i + “j = “ + j); System.out.println(“x = “ + i + “j = “ + j);
9
Input from keyboardInput from keyboard
int i;int i;
int j;int j;
i = System.in.read();i = System.in.read();
oror
read a string and convert it to an integerread a string and convert it to an integer
10
Input from keyboardInput from keyboard
import javax.swing.JOptionPane;import javax.swing.JOptionPane;
public class MyInputpublic class MyInput
{public static void main(String Args[]){{public static void main(String Args[]){
String firstNumber;String firstNumber;
int number1;int number1;
firstNumber = firstNumber = JOptionPane.showInputDialog("Enter an integer: ");JOptionPane.showInputDialog("Enter an integer: ");
number1 = Integer.parseInt(firstNumber);number1 = Integer.parseInt(firstNumber);
System.exit(0);}}System.exit(0);}}
11
Primitive data typesPrimitive data types
booleanboolean - 8 bits - true/false - 8 bits - true/false char 16 bits ‘\u0000’ to ‘\uFFFF’ (unicode)char 16 bits ‘\u0000’ to ‘\uFFFF’ (unicode) byte - 8 bitsbyte - 8 bits short - 6 bitsshort - 6 bits int - 32 bitsint - 32 bits long - 64 bitslong - 64 bits float - 32 bitsfloat - 32 bits double - 64 bitsdouble - 64 bits
12
OperatorsOperators
Arithmetic operators: +, -, *, /, %Arithmetic operators: +, -, *, /, % Equality and relational operators: ==, !=, >, Equality and relational operators: ==, !=, >,
<, >=, <=<, >=, <= Increment operator: i++, ++iIncrement operator: i++, ++i Decrement operator: i--, --IDecrement operator: i--, --I Assignment operators: =, +=, -=, *=, /=, %=Assignment operators: =, +=, -=, *=, /=, %=
13
Control StructuresControl Structures
if(grade >= 16)system.out.println( "passed");
else
system.out.println( "failed");
14
Control StructuresControl Structures
switch(grade) {case 'A':
gradeA++;
break;
case 'B': gradeB++;
break;
15
Control StructuresControl Structures
case 'C':gradeC++;
break;
case 'D': gradeD++;
break;
default:showStatus("invalid grade. Enter new grade.");
16
Repetition StructuresRepetition Structures
Whiledo/while for
17
Repetition StructuresRepetition Structures
int i = 2;
while(i <=100)
i = i + 1;
18
Repetition StructuresRepetition Structures
for(int i = 2; i <= 100; i++)
j = j + 1;
19
Repetition StructuresRepetition Structures
int i = 2;
do { i = i + 1;
} while(i <=100)
20
ArraysArrays
Creating an array:
MyObject arrayofobjs[] = new MyObject[10];
add actual MyObject objects to that array:
for (int i=0;
i < arrayofobjs.length; i++) arrayofobjs[i] = new MyObject;
21
Creating Arrays - examplesCreating Arrays - examples
String[] stringArray;String[] stringArray;
int[] intArray;int[] intArray;
boolean[] booleanArray;boolean[] booleanArray;
int[] myIntArray = new int[20];int[] myIntArray = new int[20];
int[] numbers = {2, 5, 8, 10};int[] numbers = {2, 5, 8, 10};
22
Input/outputInput/output
Files and StreamsFiles and Streams• System.in System.in - - standard input stream objectstandard input stream object• System.out - System.out - standard output stream objectstandard output stream object• System.err - System.err - standard input stream objectstandard input stream object
are three stream object created when a Java are three stream object created when a Java program is executed.program is executed.
23
Input/outputInput/output
To perform file processing in JavaTo perform file processing in Java
java.iojava.io
package must be imported.package must be imported. Stream ClassesStream Classes
• FileInputStreamFileInputStream - for input from a file - for input from a file
• FileOutputStreamFileOutputStream - for output to a file- for output to a file
24
Object
InputStream OutputStream
FileInputStream FileOutputStream
PipedInputStream PipedOutputStream
Class Hierarchy
25
More on Class HierarchyMore on Class Hierarchy
ObjectObject• FileFile• FileDescriptorFileDescriptor• StreamTokenizerStreamTokenizer• InputStreamInputStream• OutputStreamOutputStream• RandomAccessFileRandomAccessFile
26
More on Class HierarchyMore on Class Hierarchy
InputStreamInputStream• ByteArrayInputStreamByteArrayInputStream• SequenceInputStreamSequenceInputStream• StringBufferInputStreamStringBufferInputStream• PipedInputStreamPipedInputStream• FileInputStreamFileInputStream• FilterInputStreamFilterInputStream
– DataInputStreamDataInputStream– BufferedInputStreamBufferedInputStream– PushBackInputStreamPushBackInputStream– LineNumberInputStreamLineNumberInputStream
27
More on Class HierarchyMore on Class Hierarchy
OutputStreamOutputStream• ByteArrayOutputStreamByteArrayOutputStream• PipedOutputStreamPipedOutputStream• FileOutputStreamFileOutputStream• FilterOutputStreamFilterOutputStream
– DataOutputStreamDataOutputStream– BufferedOutputStreamBufferedOutputStream– PrintStreamPrintStream
28
Input/outputInput/output
PipesPipes• synchronised communication synchronised communication
channels between threadschannels between threads• One thread send data to another by One thread send data to another by
writing to a writing to a PipedOutputStream.PipedOutputStream.• The target thread reads information The target thread reads information
from the pipe via a from the pipe via a PipedInputStreamPipedInputStream
29
Creating a FileOutputStream Creating a FileOutputStream object and attempting to open a object and attempting to open a file:file:
try {try {
output = new DataOutputStream(output = new DataOutputStream(
new FileOutputStream(“afile.dat”));new FileOutputStream(“afile.dat”));
}}
catch ( IOException e ) {catch ( IOException e ) {
System.err.println(“File not opened System.err.println(“File not opened properly\n” + e.toString());properly\n” + e.toString());
System.exit( 1 );}System.exit( 1 );}
30
Writing to a fileWriting to a file
output.writeInt ( accountNumber);output.writeInt ( accountNumber);
output.writeDouble(doubleValue);output.writeDouble(doubleValue); Closing the fileClosing the file
output.close();output.close();
31
Opening a file to readOpening a file to read
try {try {
input = new DataInputStream(input = new DataInputStream(
new FileInputStream(“afile.dat”));new FileInputStream(“afile.dat”));
}}
catch ( IOException e ) {catch ( IOException e ) {
System.err.println(“File not opened System.err.println(“File not opened properly\n” + e.toString());properly\n” + e.toString());
System.exit( 1 );}System.exit( 1 );}
32
Reading from a fileReading from a file
Account = input.readInt();Account = input.readInt();
balance = input.readDouble();balance = input.readDouble();
input.close();input.close();
33
Read/WriteRead/Write
And of courseAnd of course
System.out.print(“Enter grade:”);System.out.print(“Enter grade:”);
grade = System.in.read();grade = System.in.read();