Programming Language Design and Implementation: Overview of C, C++ and Java
Programming Language Design and Implementation: Overview of C, C++ and Java
Book sections:
• Section 1.5, Appendix A.2
• Section 2.2.4, Appendix A.3
• Section 6.5, Appendix A.5
There are also web notes for each of these languages
& %
' $
These three languages all have the same basic syntax. They were developed at
different times and the differences between them reflect the changes in what was
thought to be important in a programming language.
C and to some extent C++ have become the dominant languages used to develop
systems software (operating systems, compilers, utility programs, video games,
etc.)
Java is widely used for programs that need to run from the web on multiple
platforms.
& %
' $
& %
' $
C overview
& %
' $
& %
' $
C program structure
& %
' $
C program structure
#include <stdio.h>
#include "myHeader.h"
return 0;
}
& %
' $
The C compiler we use under Linux is the GNU C compiler which is called gcc.
To compile a program contained in a single file called prog.c, type
gcc proc.c
To get warnings about things that aren’t technically errors, use the -Wall option
gcc -Wall proc.c
The default name for the executable file is a.out. To give the output file a different
name, type
gcc -o prog proc.c
To run the executable file, just type its name
prog
or possibly
./prog
& %
' $
C++ overview
& %
' $
C/C++ Compatibility
& %
' $
& %
' $
& %
' $
C++ program
#include <iostream.h>
#include "myHeader.h"
#include "myClass.h" // class declaration
& %
' $
The C compiler we use under Linux is the Gnu C compiler which is called g++.
To compile a program contained in a single file called prog.c, type
g++ proc.c
The default name for the executable file is a.out. To give the output file a different
name, type
g++ -o prog proc.c
To run the executable file, just type its name
prog
or possibly
./prog
& %
' $
Java development
Java development began in 1991 at Sun Microsystems, where James Gosling led
the Green Team in a project to develop a language for use on consumer digital
devices.
In 1993, the Mosaic Web Browser was released. The Green Team immediately saw
the role of their language as a way to enhance Web browsers.
Because of slow transmission speeds, Sun saw this new language as a way to
incorporate executable behavior in the browser rather than in the web server.
A critical development: The Web server would not know what machine the user’s
browser was executing on. To solve this problem, a Java virtual machine was
designed and the applet would be compiled into a series of bytecodes for that
virtual machine.
Java Overview
For the most part, Java is C++ with the excess baggage of C removed.
• Data are strongly typed, with integers, Booleans, and characters all being
separate types.
• Arrays are separate types, and a string is not simply an array of characters.
• Method invocation is the only subroutine linkage. Because all objects are part
of classes, there is no need for separate function or procedure calls.
• Struct objects are not needed because the same effect can be achieved via
instance variables in class definitions.
• Pointers are implicit in the language, but there is no pointer data type.
& %
' $
A Java application consists of one or more classes. Only import statements exist
outside of a class.
Each class has the structure shown below.
Generally, each public class is put into a separate file.
The name of the file and the name of the class have to match. That is the class
MyClass needs to be in a file called MyClass.java.
& %
' $
import java.io.*;
& %
' $
• Numeric data can be signed or unsigned and several different sizes are pro-
vided: short, int, long int, long long int, unsigned int, unsigned long. Repre-
sentation is left up to the implementer.
• Floating point data: float, double, long double.
• Character data are stored as type char using the ASCII code.
char ch = ’a’;
creates a variable ch with the ASCII value for the character a stored in the
memory location associated with ch.
• Boolean data is stored in int format; 0 is false and everything else is true.
C++ provides a bool type and the keywords true and false to represent the
two possible values.
& %
' $
• Numeric data can be 8-bit, 16-bit, 32-bit or 64-bit signed integer data. There
are also 32-bit float and 64-bit double real data that follow IEEE Standard
754.
Note: The storage representation for numeric types is defined into the lan-
guage instead of being left up to the implementor.
• Java also stores data as byte and short, although these are coerced to int
before performing any operations on them.
• Character data are stored using unicode. Unicode is an international standard
that creates 16-bit characters. For example,
char x = ’a’
creates a variable x with a value which is the unicode value for the character
a.
• boolean data may be true or false.
& %
' $
Control structures
The control structures (selection and repetition) have the same basic syntax in all
three languages.
• Selection
– if and if-else
if (condition)
ifBody;
else /* else part is optional */
elseBody;
• Repetition
– while
while (condition) /* condition tested first */
whileBody;
– do-while
do /* body executed first */
doBody;
while (condition); /* condition tested last */
& %
' $
& %
' $
C standard I/O
& %
' $
Formatted I/O in C
Both printf and scanf use format strings to specify the format. For each data type,
there is a format specifier which is one or two characters preceded by a %
& %
' $
& %
' $
& %
' $
Files in C
Files in C++
• include fstream.h The filestream classes inherit from the istream and ostream
classes used for standard I/O.
• Open a file with the constructor or the open function.
ifstream fin( "datafile");
ofstream fout;
fout.open( "outfile");
• Use the insertion and extraction operators as before but replace cin and cout
by the filestream object names.
• Close a file with the close function.
fout.close();
& %
' $
Files in Java
• Use a FileReader object which is connected to the physical file by the con-
structor or the open method.
FileReader fileReader = new FileReader( args[0]);
• Create a BufferedReader from the FileReader and use the readLine method
BufferedReader infile = new BufferedReader( fileReader);
• Use a FileWriter object which is connected to the physical file by the con-
structor or the open method.
FileWriter fileWriter = new FileWriter( args[1]);
• Create a PrintWriter from the FileWriter and use the readLine method
PrintWriter outfile = new PrintWriter( fileWriter);
• Close a file with the close method.
infile.close();
& %
' $
& %
' $
Arrays in Java
& %
' $
• Can make dynamic arrays by declaring a pointer to the appropriate type and
allocating memory for the desired number of components.
– in C
someType * CdynArray = malloc( numElements * sizeof( someType));
– in C++
someType * dynArray = new someType[numElements];
The programmer is also responsible for deallocating the memory allocated for
dynamic arrays. (free in C and delete in C++)
• returning an array from a function has to be done by returning a pointer
• static arrays allocated in a function are local and will disappear when the
function returns
& %
' $
structs in C
structs in C
& %
' $
C++ classes
& %
' $
C++ classes
• The class definition is all that the compiler needs for creating object code for
a program that uses the class. Object code for the class functions are needed
at load time.
• Member access is similar to component access for structs.
• As with structs, functions get a copy unless passed by reference.
• Derived classes specify the base class as follows:
class derived : public className { ... }
Inherited functions are only listed if they will be overridden.
& %
' $
Java Classes
Class Libraries
& %
' $
Strings in C
& %
' $
String Functions in C
• int strlen( char str[]) : returns the number of characters in the string,
not including the null character
• void strcpy( char destination[], char source[]) : copy the source string
into the destination
• void strncpy( char destination[], char source[], size t n) : copy
up to n characters of the source string into the destination
• void strcat( char destination[], char source[]) : append the source
string into the destination
• char * strrchr( const char str[], const char ch) : points to the first
occurrence of ch in str. If you need an index to that position, the returned
valur - the original string name will give it to you.
• char * strtok( char str[], const char str2[]) : a function that can
be used to break a string up into tokens using the characters in str2 as de-
limiters. This destroys the original string by writing over the first delimiter
found. It returns a pointer to the remainder of the string.
& %
' $
Strings in C++
• C++ has a string class with methods to do most of what you need to do.
include <string>
using namespace std;
Note that there is no .h
• strings can be declared and created in several ways:
string str( "a string");
string str2 = "another string";
• You can use assignment (=) and concatenation (+) with C++ strings.
& %
' $
& %
' $
Strings in Java
• Java also has a String class which is in the java.lang package. You do not need
to import anything.
• Create a String in two ways
1. Using a constructor
String str1 = new String( "some text"
& %
' $
& %
' $
String I/O
& %