Java Code Examples
[Geek Home] [PC Hints & Tips] [Internet] [Programming] [Nanet Sites]

A C Programmer’s Java Primer

This page contains Java Code Example fragments. This listing assumes you have knowledge of C programming or other similar language in order to understand how to use the code. It is not a Tutorial for the new programmer, but rather a primer for the C programmer learning Java. It is by no means complete.

if Conditionals
if (value == 1)
    strval = “one”;
else if (value == 2)
    strval = “two”;
else if (value == 3)
    strval = “three”;

switch Conditionals
The switch statement must test a variable or expression that evaluates to a simple primitive type: byte, char, short, or int -- which are automatically castable to int.

  case 1:
    return “one”;
  case 2:
    return “two”;
  case 3:
    return “three”;
  case 4:
    callother(val1, val2);
    break ;
  case 5:
  case 6:
  case 7:
    callother(val3, val4);
    break ;
    System.out.println(“We are done now.”);

Notice in the above code, the switch statement can return a value rather than issuing the break. Also, notice how if everything drops through, it issues the default statement.

When an array object is created using new, all slots are initialized for you:
0 = numeric arrays
false = boolean
‘\0’ = character arrays
null = objects.

Declaring Arrays

int[] array1 = { 10, 15, 3, 5, 54 };
float[] array2 = new float[array1.length];
String[] firstnames = { “Floyd”, “Sylvia”, “Barney” };
String[] lastNames = new String[firstNames.length];

Initializing a String array to null strings:

String strArray[] = new String[20]; // the array
int ctr ; // loop index
for (ctr=0; ctr < strArray.length; ctr++)
     strArray[ctr] = “”;

// strArray.length indicates the number of elements in the array

while Loops
Below we will do a while loop that copies the elements of thisarray to thatarray and prints out the elements of thatarray until one of the elements in thisarray = 0 or until it reaches the last element in thisarray. A while loop will never execute if the first element’s test is false.

int ctr = 0;
while ( ctr < thisarray.length && thisarray[ctr] !=  0)
    thatarray[ctr] =  thisarray[ctr];
    System.out.print(thatarray[ctr++] + “”);

do loop
If you need a loop to execute at least once, use the do loop instead of the while loop.

class RunDo
    public static void main (String args[])
      int ctr = 1;
        System.out.println(“Loop “ + ctr);
      } while (ctr <= 4);

Loop 1
Loop 2
Loop 3
Loop 4

Exiting a loop
To break out of a loop, use break.
To start the loop over on the next iteration, use continue.

Loop Labels
You can also use a label in a loop to break or continue to that point.

    for (int ctr = 0; ctr < 5; ctr++ )
      while (ctr2 < 50)
        if (ctr * ctr2 == 300)
           break out;


boolean sameAddress = address1 == address2;
Checks if they share the same address -- if one is cast into the other.

boolean sameAddress = address1.equals(address2);
Checks if the values are equal.

Declaring a Constant Variable
In Java, you cannot use the #define or const C/C++ constructs. You must declare a constant with the final keyword:

final float pi = 3.141592;
final boolean isnull = false;

You can only create a constant for an instance or class variable, not for local variables.



#include <stdio.h>

    printf("Hello, World\n");
    return 0;


#include <iostream.h>

    cout << "Hello, World\n";
    return 0;


public class hello
    public static void main(String  args[])
          System.out.println("Hello World");

 Source  Java Code  C CGI  JavaScript  Java DBF  VAJava & DB2
[Geek Home] [PC Hints & Tips] [Internet] [Programming] [Nanet Sites]

Modified: 02/11/99