Title: Java
1Java C Comparisons
How important are classes and objects?? What
mechanisms exist for input and output?? Are
references and pointers the same thing?? How are
parameters passed?? What makes Java safer??
2Hello World
//C include ltiostreamgt using namespace
std int main() cout ltlt Hello world ltlt
endl return 0
//Java public class Hello public static void
main() System.out.println(Hello world)
3Object oriented design is very important in Java.
All code should be either an application (client)
class or a support class. Many classes come with
Java. Some do not need to be imported. Example
System (always available, used for console
output and other utilities)
4Some aspects of Java are very similar to
C semantics (reserved words) syntax decision
statements control statements
What do the following loops do?
5Control Structures
- //C
- int oddCount 0
- for (int j0 jlt20 j)
-
- if (Aj 2 1)
-
- oddCount
-
-
//Java int oddCount 0 for (int j0 jlt20
j) if (Aj 2 1)
oddCount
6Variable declarations
//Java // Primitives int x double d
boolean done // Objects Dice cube new
Dice(6)
- //C
- // Primitives
- int x
- double d
- bool done
- // Objects
- Dice cube(6)
7BankAccount bc1
C
Java
Actually creates an instance of the class
BankAccount allocates enough memory to hold the
objects data
Creates a reference to an instance of the class
BankAccount. bc1 is only a variable that will
hold the address of an instance of this class
when memory is allocated for it.
In Java this statement actually creates the
object bc1 new BankAccount()
8Assignment of objects What does bc2
bc1 do?
A copy of the object, bc1, including all its
data, is made and named bc2 in C.
bc2, a reference to an object of the BankAcct
class, receives the address of the object, bc1 in
Java. That is, both bc1 and bc2 refer to the same
object !!
9A deep copy must be made.
After a new reference to an instance of the same
class is declared, the programmer must copy each
data field.
10Linear Aggregate Structures
Java arrays Bounds checking
- C arrays
- No bounds checking
As in C the size of an array cannot be changed,
once established. (Other classes exist for that.)
In Java an array is an object, inheriting from
the class Object, and is always available. Ex
int myInts (declares myInts a reference to an
array) myInts new int50 (allocates
memory needed)
11Parameter Passing
- C
- Value
- Reference
- const Reference
Java Value (primitives) Reference (objects)
In Java you can not pass a pointer by reference.
Therefore, addresses can not be inadvertently
changed. Of course there are no pointers - but
references do the same job. There are no
symbols or .
12Searching a linked list
C Java
13Out with the old
- In C
- Inheritance Its there, but more difficult to
implement - Multiple implementations of Abstract Data Types
through classes only
- Java supports
- Inheritance (extends) Its there and its easy
- Multiple implementations through implements
- keyword
14In with the New
- In C
- Const many uses
- Operator overloading
- delete to clean up memory management
- In Java
- final a Pascal/Ada-like use of const
- NO operator overloading
- Automatic garbage collection