Title: COP 3331 Object Oriented Analysis and Design
1COP 3331Object Oriented Analysis and Design
2Java Part I
- // First Java Program
- // HelloWorld.java
- Public class HelloWorld
-
- public static void main (String args)
-
- System.out.println(Hello World!)
-
3Java Part I
- The following compile sequence occurs in Unix on
Diablo. - gaitrosd_at_diablo/javagtjavac HelloWorld.java
- gaitrosd_at_diablo/javagtjava HelloWorld
- Hello World!
4Java Part I
Primary Java Program Files
File Definition Function
javac.exe The Java compiler Converts Java source code into executable byte-code
java.exe The Java enterpreter Runs the java program
appletview.exe The applet view Locally runs a java applet from within an HTML file.
jdb.exe The Java debugger Used to debug your Java applications and applets
javah.exe The Java C header and stub file generator Creates C header files and C stub files from a java class, allowing your java and C code to interact.
javap.exe The Java class file disassembler Converts a compiled class back into source code.
javadoc.exe The Java API documentation generator Generates HTML documentation from Java Source Code
5Java Part I
- General Java points to remember
- Java is case sensitive.
- Use (semicolon) to terminate lines of code.
- Use (curly braces) to indicate blocks of code
for classes, methods, if statements, and loops. - Use spaces for indentation to make your code more
readable.
6Java Part I
Comment Type Function
// C Everything after the // and until the end of the line is treated as a comment.
/ / C Everything between the / and / is treated as a comment even if it crosses a line boundary.
/ / Hypertext Same as the C style comments except that these comments are read by Javas documentation generator, which can generated HTML code from your comments. You can include HTML code in these comments.
7Java Part I
- Memory variables Storage space declared inside
the computer on memory is allocated through
memory variables and constants. - Java is strongly typed which means that memory
variables can only hold their declared type of
data. - There are two types of data catetories
- Simple types.
- Object types.
8Java Part I
Simple Types
Type Size
byte 8 bits
char 16 bits (unsigned)
short 16 bits
int 32 bits
long 64 bits
float 32 bits
double 64 bits
boolean 1 bit
Simple data types are not based on any other
type. These sizes do not vary from machine to
machine. All numeric types are signed except
character. Java is case sensitive. Names of
simple data types Are in lower case as shown.
9Java Part I
- Declaring Variables
- Format
- data_type variable_name
- data_type variable_name value
- Example
- int j // integer
- char opt // character
- short I // short integer
- long L // long integer
- float F // real number
- double D // 64 bit real number
- boolean ok // True/False
- int I,j,k,l
- char Men A
10Java Part I
Arithmetic Operators
Operator Meaning Example
- Unary negation x -y
Addition x y z x
- Subtraction xy-z-x
Multiplication z x y
/ Division z x / y
Modulus n x 3
Increment by 1 x y
-- Decrement by 1 x--
Note There is not operator for exponent. You
must use the Math.pow() function for this task.
11Java Part I
Comparison and logical Operators
Operator Meaning Example
! Not Equal if(x ! 100) yZ
lt Less then if(x lt 100) yZ
lt Less then or equal to if(x lt 100) yZ
gt Greater then if(x gt 100) yZ
gt Greater then or equal to if( lt 100) yZ
! Not Answer !Answer
And if( x100 y0 ) yZ
Or if( x100 y0 ) yZ
Exclusive Or if( x100 y0 ) yZ
Short circuit And if (x lt3 y0) z7
Short circuit OR if (x lt3 y0) z7
On the short circuit operators, the next
comparison is not accomplished if it does not
affect the outcome.
12Java Part I
Operator Meaning Example
Bitwise compliment x x //flips each bit.
Bitwise And if(y2 2)
Bitwise Or x 35 // result 7
Bitwise Xor x35 // Result 6
ltlt Left Shift x 1ltlt2 // Result 4
gtgt Sign propagating right shift I 8gtgt 2 // Result 2
gtgtgt Zero-fill right shift I 8 gtgtgt2// result 2
13Java Part I
- Conditional Execution
- if(condition)
- statement
- if(condition)
- statement1
- statement 2
- statemetn 3
- etc
-
14Java Part I
- if(condition)
-
- statement1
- statement2
-
- else
-
- statement1
- statement2
-
15Java Part I
- Switch Statements
- switch (expression)
-
- case value1 statements
- case value2 statements
- . . .
- default statements
-
- switch (input_char)
- case y System.out.print(Entered y)
- case n System.out.print(Entered n)
- default System.out.print(Dont know)
16Java Part I
- Loops
- for (initialize condition increment)
-
- statements
- statements
-
- example
- for (i1 ilt100 i)
-
- System.out.print(i)
-
17Java Part I
- while (condition)
-
- statements
- statements
-
- while (J lt 100)
-
- J J 1
- System.out.print(J)
-
18Java Part I
- Command Line Arguments In all Java
applications, the main() function accepts an
argument, an array of strings called args
This array contains command line arguments. - For instance, if you entered the following on the
HelloWorld program - java HelloWorld Arg1 Arg2 Hello Again
- The args.length would equal three (3). and the
contents would be - args0 -gt Arg1
- args1 -gt Arg2
- args2 -gt Hello Again