/* C */ |
// Java | //comment |
#include <stdio.h> int main(int argc, char *argv[ ]) { printf("Hello, World\n"); } |
import java.io.*; import java.lang.*; public class hello { public static void main(String[ ] argv) throws IOException { int argc=argv.length; System.out.print("Hello, World\n"); System.out.close(); } } |
1. Save this in a text file called "hello.java". 2. Java "class hello" name must match source text file name "hello.java". 3. This is a java "application" not "applet". 4. int argc=argv.length; //not "length()"! 5. Alternate: System.out.println("Hello, World"); 6. "throws IOException" needed for file i/o processing. 7. Can also use: "Exception" instead of "IOException". |
gcc hello.c -o hello.exe |
javac hello.java |
javac: compiles "hello.java" into "hello.class". |
./hello.exe |
java hello |
java: virtual machine interpreter (JVM). passing 3 arguments: "java hello x y z" Arguments offset by one: C argv[1] equals Java argv[0] reading from stdin (System.in.read()): "java hello <file.txt" |
int i, n; |
int i, n; |
32 bit signed integer |
int *a; | int [ ] a; | pointer = object reference |
int a[10]; |
//no static or stack allocation | |
int *a =
(int*)malloc(10*sizeof(int)); |
int [ ] a = new int[10]; |
dynamic allocation |
n=a[ i ]; |
n=a[ i ]; | //can't do this with String class |
a[ i ]=n; |
a[ i ]=n; | //can't do this with String class |
#define N 10 /* preprocessor */ |
n = a.length; |
size of an array (e.g. 10). |
const int i = 64; |
final int i = 64; |
//public static final int i = 64; |
signed char cs; i=cs; |
byte cs; i=cs; |
java: 8 bit signed integer |
unsigned char cu; i=cu; |
byte cu; i=0xff & cu; |
java: does not support unsigned
arithmetic |
short i =32767; |
short i = 32767; |
java: 16 bit signed integer |
int i = 2147483647; |
int i = 2147483647; |
java: 32 bit signed integer; |
long i = 9223372036854775807L; |
long i = 9223372036854775807L; |
java: 64 bit signed integer; |
float f =3.4e+38; | float f =3.4e+38; | java: 32 bit IEEE 754 float |
double d = 1.8e+308; |
double d = 1.8e+308; |
java: 64 bit IEEE 754 float |
long double dd= 1.2e4932; |
java: none |
int i=0; |
boolean b=false; |
java integers are NOT boolean logic. |
int i=1; /* i!=0 */ |
boolean b=true; |
c negative and positive numbers
are true logic. |
if (i) { } else { } |
if (i!=0) { } else { } |
if (b) { } else { } |
n=i?1:0; |
n=(i!=0)?1:0; |
n=b?1:0; |
if (i && n) { } |
if ((i!=0) && (n!=0)) { } |
logical AND |
if (i || n) { } |
if ((i!=0) || (n!=0)) { } |
logical OR |
char c; |
char c; |
java: 16 bit Unicode character |
char c; c=i; |
c=(char)i; |
javac compiler error without cast |
#include <ctype.h> if (isalpha(c)) { } |
import java.lang.*; if (Character.isLetter(c)) { } |
import java.lang.Character; if (java.lang.Character.isLetter(c)) { } |
if (isupper(c)) { } |
if (Character.isUpper(c)) { } |
|
if (islower(c)) { } |
if (Character.isLower(c)) { } |
|
if (isdigit(c)) { } |
if (Character.isDigit(c)) { } |
java handles international digits |
if (isxdigit(c)) { } |
if (Character.digit(c,16)>=0
&& Character.digit(c,16)<16) { } |
|
if (isalnum(c)) { } |
if
(Character.isLetterOrDigit(c)) { } |
|
if (isspace(c)) { } |
if (Character.isWhiteSpace(c)) {
} |
|
if (iscntrl(c)) { } |
if (Character.isISOControl(c) { } |
|
c = toupper(c); |
c = toUpperCase(c); |
|
c = tolower(c); |
c = toLowerCase(c); |
#include <strings.h> char s[ ] ; //array of char's |
import java.lang.*; String s; //an "immutable" class |
import java.lang.String; import java.lang.StringBuffer; StringBuffer s; //"mutable" class |
char s[ ]="abc"; | String s="abc"; | StringBuffer sb=new
StringBuffer("abc"); |
char s[10]; |
//can't do this with String class | StringBuffer sb=new
StringBuffer(10); |
char s[10]="abc"; |
//can't do this with String class | |
char s[ ] = { 'a', 'b', 'c', 0 }; | String s=new String(new char [ ] { 'a', 'b', 'c' } ); |
StringBuffer sb =new StringBuffer(new String(new char [ ] { 'a', 'b', 'c' } )); |
char *s, *t; |
String s, t; |
|
s = t; |
s = t; |
copy reference pointer only |
strcpy(s, t); | s=t.clone(); s=new String(t); //alternate |
s=tb.toString(); sb.delete(0,sb.length()).append(tb); |
strcpy(s, t+i); | s=t.substring( i ); | sb.delete(0,sb.length()).append(t,i,t.length()); |
strcat(s, "def"); | s+="def"; //String class creates new instance | sb.append("def"); s+=sb; |
i=strlen(s); | i=s.length( ); |
i=sb.length(); |
c=s[ i ]; | c=s.charAt( i ); | c=sb.charAt( i ); |
s[ i ]=c; |
s=s.substring(0, i)+c+s.subtring(i+1); |
sb.setCharAt( i, c); |
if (!strcmp(s, "abc")) { break; } |
if (s.equals("abc")) { break; } |
if (s.compareTo("abc")==0) {
break; } |
if ( s == t ) { break; } |
if ( s == t ) { break; } |
compare reference pointer only |
if (strcmp(s, t)>0)) { break;
} |
if (s.compareTo(t)>0) {
break; } |
s is greater than t |
if (strcmp(s, t)<0)) { break;
} |
if (s.compareTo(t)<0) {
break; } |
s is less than t |
sscanf(s, "%d", &i); i=atoi(s); | i=Integer.parseInt( s ); |
import java.lang.Integer; |
sscanf(s, "%x", &i); |
i=Integer.parseInt( s, 16); | #include <stdio.h> |
sscanf(s, "%o", &i); | i=Integer.parseInt( s, 8); | |
sscanf(s, "%f", &f);
f=atof(s); |
f=Float.parseFloat(s); |
import java.lang.Float; |
sscanf(s, "%g", &d); |
d=Double.parseDouble(s); |
import java.lang.Double; |
i=strtol(s, 0, base); | i=Integer.parseInt( s, base); | |
sprintf(s, "%d", i); |
s=Integer.toString(i); |
s=String.valueOf( i ); //base 10
only |
sprintf(s, "%x", i); |
s=Integer.toHexString(i); | s=Integer.toString(i, 16); |
sprintf(s, "%o", i); |
s=Integer.toOctalString(i); |
s=Integer.toString(i, 8); |
sprintf(s, "%f", f); |
s=Float.toString( f ); |
s=String.valueOf( f ); |
#include <stdio.h> printf("Hello World\n"); |
import java.lang.*; System.out.print("Hello, World\n"); |
import java.lang.System; System.out.println("Hello, World"); |
printf("i=%d\n", i); |
System.out.print("i="+i+"\n"); |
java.lang.System.out.println("i="+i); |
printf("f=%f\n", f); |
System.out.print("f="+f+"\n"); |
uses PrintStream class |
fprintf(stderr, "error msg\n"); |
System.err.print("error msg\n"); |
|
c=getchar( ); c=getc(stdin); |
int c; c=System.in.read( ); | EOF=-1; uses InputStream class |
putchar(c); putc(c, stdout); |
int c='A'; System.out.write(c); //outputs "A" |
System.out.print(c); //outputs "65" System.out.print((char)c); //="A" |
putc(c, stderr); |
System.err.write(c); |
|
exit(1); |
System.err.close(); System.out.close(); System.exit(1); |
import java.lang.System; |
#include <stdio.h> FILE fout; //stream version |
import java.io.*; PrintStream fout = null; //text base output |
Binary files: DataOutputStream can't use abstract class: OutputStream NOT for random access files |
if (!(fout=fopen(fn, "w")) { fprintf(stderr, "fopen error: %s\n", fn); } |
try { fout = new PrintStream(new FileOutputStream(fn)); } catch (Exception e) { System.err.println ("PrintStream("+fn+") "+e); System.err.close( ); System.exit(1); } |
java does not "flush" and "close" files upon exit |
fprintf(fout, "i=%d\n", i); |
fout.println("i="+i); |
http://www.javacamp.org/javavscsharp/ |
fprintf(fout, "%d", i); |
fout.print( i ); |
PrintStream |
fprintf(fout, "%f", f); |
fout.print( f ); |
PrintStream |
fprintf(fout, "%s", s); |
fout.print( s ); |
PrintStream |
fprintf(fout, "%c", i); |
fout.printf((char)i); |
treat integer as character |
putc(c, fout); fputc(c, fout); | fout.write(c); | OutputStream, PrintStream; int
c; must cast |
fflush(fout); |
fout.flush( ); |
OutputStream, PrintStream |
fclose(fout); |
fout.close(); |
OutputStream, PrintStream |
#include <stdio.h> FILE fin; //stream version |
import java.io.*; DataInputStream fin=null; |
Binary files: DataInputStream NOT for random access files can't abstract class: InputStream |
if (!(fin=fopen(fn, "r")) { fprintf(stderr, "fopen error: %s\n", fn); } |
try { fin = new DataInputStream(new FileInputStream(fn)); } catch (Exception e) { System.err.println ("DataInputStream("+fn+") "+e); System.err.close( ); System.exit(1); } |
java does not "flush" and "close" files upon exit |
Internet file reading |
import java.net.*; try { fin = new DataInputStream((new URL(fn)).openStream()); } catch (Exception e) { System.err.println("DataInputStream("+fn+") "+e); System.err.close( ); System.exit(1); } |
import java.net.URL; Examples: fn="http:// bear.ces.cwru.edu/ index.html"; or fn="file:// /home /users /wolff /EECS382 /JAVA /testj.java"; |
c=getc(fin); c=fgetc(fin); |
int c; c=fin.read( ); |
8-bit ascii; EOF=-1; |
fgets(s, n, fin); |
int c; s=""; c=fin.read(); for(i=0; i<n && c>=0; i++) { s+=(char)c; fin.read(); } |
n is optional since java strings are unlimited. |
fclose(fin); |
fin.close( ); |
InputStream, DataInputStream |
#include <stdio.h> FILE fp; //Random access |
import java.io.*; RandomAccessFile fp = null; |
import java.io.RandomAccessFile; |
if (!(fp=fopen(fn, "rw")) { fprintf(stderr, "fopen error: %s\n", fn); } |
try { fp = new
RandomAccessFile(fn, "rw"); } catch (Exception e) { System.err.println("RandomAccessFile("+fn+") "+e); System.err.close( ); System.exit(1); } |
java requires "rw" for write and "r" for read. |
puts(s, fp); |
fp.writeBytes(s); |
8-bit ascii characters; Uni-code also. |
putc(c, fp); |
fp.write(c); |
always works; 8-bit ascii characters; int c; |
int c; c=getc(fp); |
c=fp.read( ); |
EOF = -1 |
fgets(s, n, fp); |
s=fp.readLine( ); |
rewrite using read() |
fseek(fp, offset, SEEK_SET); |
fp.seek(offset); |
from beginning of file |
offset=ftell(fp); |
offset=fp.getFilePointer( ); |
|
fseek(fp,0,SEEK_END);
n=ftell(fp); |
n=fp.length(); |
size of file |
fclose(fp); | fp.close( ); |
#include <stdio.h> #include <ctype.h> #include <strings.h> int main(int argc, char *argv[ ]) { int i, c; int uflag=0, fflag=0; char *fn=0; FILE *fin=0; /* argv[0]=program */ for(i=1; i<argc; i++) { if (!strcmp(argv[i], "-U")) { uflag=1; } else if (argv[i][0] != '-') { fflag=1; fn=strdup(argv[i]); } else { fprintf(stderr, "cat: unknown flag=%s\n", argv[i]); exit(1); } } if (fflag) { fin = fopen(fn, "r"); if (!fin) { fprintf(stderr, "fopen(%s) error\n", fn); exit(1); } } for(;;) { c=(fflag)?fgetc(fin):getchar(); if (c==EOF) { break; } if (uflag) { c=toupper(c); } putchar(c); } if (fflag) { fclose(fin); } } |
import java.lang.*; import java.io.*; public class cat { public static void main(String[ ] argv) throws IOException { int i, c, argc=argv.length; boolean uflag=false, fflag=false;; String fn = null; DataInputStream fin = null; for(i=0; i<argc; i++) { if (argv[i].equals("-U")) { uflag=true; } else if (argv[i].charAt(0) != '-') { fflag=true; fn=new String(argv[i]); } else { System.err.println("cat: unknown flag="+argv[i]); System.err.close(); System.exit(1); } } if (fflag) { try { fin = new DataInputStream(new FileInputStream(fn)); } catch (Exception e) { System.err.println ("DataInputStream("+fn+") "+e); System.err.close( ); System.exit(1); } } for(;;) { c=(fflag)?fin.read():System.in.read(); if (c<0) { break; } if (uflag) { c=Character.toUpperCase((char)c); } System.out.write(c); } if (fflag) { fin.close(); } } } |
Example: "wcat.java" program (compiled version: wcat.class) Read a file from command line, standard in or the web. Optional flag -U translates file to uppercase: 0. "javac wcat.java" 1. "java wcat <file.txt" 2. "java wcat -U <file.txt" 3. "java wcat file.txt" 4. "java wcat -U file.txt" |