|
C / C++ |
Java |
C/C++ explanation |
Java explanation |
|
printf("Hello\nWorld"); |
System.out.print("Hello\nWorld"); |
Prints stuff ("\n" means newline) |
Prints stuff ("\n" means newline) |
|
printf("%s",mystring); |
System.out.print(mystring); |
Prints char array (mystring var), eg., a string. |
Prints string |
|
printf("%c",c); |
System.out.print(c); |
Print a char from the char declared above |
Print a char from the char declared above |
|
printf("%c hello %s",c,mystring); |
System.out.print(c+" hello"+mystring); |
Print a char followed by "hello", followed by the mystring variable. |
Print a char followed by "hello", followed by the mystring variable. |
|
printf("%c",mystring[99]); |
System.out.print(mystring.charAt(99)); |
Print last char from the string declared above |
Print last char from the string declared above |
|
printf("%s",array2d[19]); |
System.out.print(array2d[19]); |
Print last string from the string array (declared above under title "Declaring Variables"). |
Print last string from the string array (declared above under title "Declaring Variables"). |
|
printf("%c",array2d[19][99]); |
System.out.print( array2d[19].charAt(99) ); |
Print last char from the last string in the string array. |
Print last char from the last string in the string array. |
|
mystring[99] = 'z' |
--- |
Change last character in mystring string to 'z' |
No direct char editing in string |
|
mystring = "hello" |
mystring = "hello" |
Obvious, but illegal! You can't directly assign a string to a char array. Use strcpy instead. |
Unlike in C, this is fine, and just assigns "hello" to the mystring String. |
|
array2d[19][99] = 'y' |
--- |
Change last character in last string to 'y' |
No direct char editing in a string |
|
strcpy(mystring,
"hello"); |
mystring = "hello" |
Copy "test" to the "mystring" string as declared earlier. |
Assigns word "hello" to the mystring String. |
|
strcpy(array2d[5], "hello"); |
stringArray[5] = "hello" |
Copy "test" to the 5th string in "array2d" as declared earlier. |
Copy "test" to the 5th string in "stringArray" as declared earlier. |
|
strncpy(array2d[5], "hello", 5); |
stringArray[5] = "hello".substring(0, 5) |
Use strcpy instead usually. This is used if you want to define the number of chars to copy. |
Use substring (truncate) to define a limited section of the string. |
|
strncpy(array2d[5], mystring+10, 50); |
stringArray[5] = mystring.substring(10, 60) |
Takes a substring of mystring (start from 10th char and do 50 of them), and puts it into the array2d[5] string. |
Takes a substring of mystring (start from 10th char and do 50 of them), and puts it into the array2d[5] string. |
|
strcat(mystring,"hello"); |
mystring = mystring
+ "hello" |
Concatenate (add/append) "test" to the string, along with the escape code '\0' to signify the end |
Concatenate (add/append) "test" to the string. However, for large amounts of concatenations, use Java's StringBuffer() method, as it is much quicker. |
|
strcmp(a,b); |
a.compareToIgnoreCase(b) |
Compare two strings. Returns zero if same, -1 if a is less than b, and 1 if a is more than b. |
Compare two strings. Returns zero if same, Less than zero if a is less than b, and more than zero if a is more than b. |
|
strcmpi(a,b); |
a.compareTo(b) |
Case sensitive version of above |
Case sensitive version of above |
|
strlen(mystring); |
mystring.length() |
Number of chars so far stored in the string (everything up to escape code '\0') |
Number of chars so far stored in the string. |
|
int i = '7'-48; |
int i = '7'-48; |
Convert char to int, providing the char is a number from 0-9. |
Convert char to int, providing the char is a number from 0-9. |
|
int i = 'a'; |
int i = 'a'; |
Convert char to int code |
Convert char to int code |
|
char c = 7 +48; |
char c = 7 +48; |
Convert int to char, providing the number is from 0-9. |
Convert int to char, providing the number is from 0-9. |
|
char c = 97; |
char c = 97; |
Convert int code to char ('a' in this case). If you don't want to declare a new char, then use the wrapper type "(char)" next to 97. |
Convert int code to char ('a' in this case). If you don't want to declare a new char, then use the wrapper type "(char)" next to 97. |
|
int i = atoi("456"); |
int i = Integer.parseInt("456") |
Convert string to int. Use 'atof' to convert string to double/float, and 'atol' for converting string to long. |
Convert string to int. Use Double.parseDouble to convert string to double, and Long.parseLong for converting string to long. |
|
sprintf(mystring,
"%d", 123); |
mystring = String.valueOf(123) |
Convert int to string (or double to string, using %f instead of %d) |
Convert int to string (or double to string) |
|
itoa(123, mystring,
10); |
--- |
Convert int to string base 10 (nonstandard, so use above instead usually) |
--- |
|
int i = sizeof(mystring); |
int i = mystring.length |
Find (full) length of array (returns |
Find (full) length of array (returns |
|
exit(0); |
System.exit(0); |
Quit program |
Quit program |
|
system("PAUSE"); |
System.in.read(); |
"Press any key to continue" (Windows only) |
Pause program until return key is pressed. You must use allow to throw an IOException though. |
|
Sleep(500); |
try {Thread.sleep(500); } |
Wait 500 milliseconds (Windows only) |
Wait 500 milliseconds. |
|
char mystring2[]="hello"; |
String mystring2="hello"; |
Declare and initialize string in one go |
Declare and initialize string in one go |
|
int i2=99; |
int i2=99; |
Declare and initialize an integer in one go |
eclare and initialize an integer in one go |
|
ntype.o = 37; |
--- |
Assign the number 37 to the 'o' part of the ntype variable. |
No structs with Java. Use objects instead. |
|
ntype.p[3] = 'z'; |
--- |
Assign the char 'z' to the 3rd element of the array in the 'p' part of the ntype variable. |
No structs with Java. Use objects instead. |
|
long l = clock(); |
--- |
Assign to l, the number of milliseconds of CPU time used on the program since its execution. |
No measuring of CPU time in Java. |
|
long l = time(NULL); |
long l = System.currentTimeMillis()/1000; |
Assign to l, the number of seconds since 00:00:00, January 1, 1970. |
Assign to l, the number of seconds since 00:00:00, January 1, 1970. |
|
timeb ti; ftime(
&ti ); |
long l = System.currentTimeMillis() |
Assign to l, the number of milliseconds since 00:00:00, January 1, 1970. |
Assign to l, the number of milliseconds since 00:00:00, January 1, 1970. |
|
time_t mytime = time(0); |
? |
Assign to mystring, the local date and time in ascii format. |
? |
|
srand(77); |
Random r = new
Random(77); |
Randomize seed for
random numbers (77 used here, but use srand((unsigned)time(NULL))
instead |
Create random object, with a seed of 77. Remove '77' to create different random numbers every time the program is run. |
|
100 * rand()/(RAND_MAX+1) |
Random r = new
Random(); |
Compute random integer number between 0 and 100 (0 <= r < 100). |
Compute random integer number between 0 and 100 (0 <= r < 100). |
|
68 * rand()/(RAND_MAX+1) + 32 |
Random r = new
Random(); |
Compute random integer number between 32 and 100 (32 <= r < 100). |
Compute random integer number between 32 and 100 (32 <= r < 100). |
|
(float) rand()/(RAND_MAX+1) |
Random r = new
Random(); |
Compute random floating number between 0 and 1 (0 <= r < 1). |
Compute random floating number between 0 and 1 (0 <= r < 1). |
|
100*(float) rand()/(RAND_MAX+1) |
Random r = new
Random(); |
Compute random floating number between 0 to 99.999... inclusive (0 <= r < 100). |
Compute random floating number between 0 to 100 |
|
scanf("%s", &mystring); |
BufferedReader r = new |
Read from console input into string |
Read from console input into string |
|
char* token; |
... |
Split the string "str" into pieces or 'tokens'. One could use any symbol to split by, such as a comma or space (as used here), or any arbitrary letter or number. In the while loop, you'll want to do something with each token such as print it out. We use "NULL" (instead of the previously used "str") as the first parameter for future strtok calls to indicate that we're going from where we previously left off, otherwise we'll keep getting the first token of a string each time. |
... |