Java - PowerPoint PPT Presentation

About This Presentation
Title:

Java

Description:

Title: PowerPoint Presentation Author: Patricia VanHise Last modified by: user Created Date: 5/21/2003 2:28:38 PM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 15
Provided by: Patrici457
Learn more at: https://owd.tcnj.edu
Category:

less

Transcript and Presenter's Notes

Title: Java


1
Java 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??
2
Hello 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)
3
Object 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)
4
Some aspects of Java are very similar to
C semantics (reserved words) syntax decision
statements control statements
What do the following loops do?
5
Control 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
6
Variable 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)

7
BankAccount 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()
8
Assignment 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 !!
9
A 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.
10
Linear 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)
11
Parameter 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 .
12
Searching a linked list
C Java
13
Out 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

14
In 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
Write a Comment
User Comments (0)
About PowerShow.com