Fundamental differences between C++ and Java

 

Java is based on C++

Some warnings for C++ programmers about to learn Java

 

Java identifiers

 

Java data types

 

Java variables

float shippingCharge = 6.25;

will not compile. This statement results in an error because the literal is double and the variable is float. The Java compiler is concerned about loss of data. To fix the error, the following can be coded:

float shippingCharge = 6.25f;

 

Java constants

final double TAX_RATE = .06;

declares a double constant named TAX_RATE and assigns it a value of .06 (or 6%).

 

Writing to the system output device (the console)

 

Reading from the system input device (the keyboard)

 

Arithmetic oprations

 

Conversions

byte 
|
short char

\

/

int
|
long
|
float
|
double

 

Casts

(data-type) expression

 

Comparisons and logical operations

 

Bitwise operations

Example:

// Variables for holding two integers.

int first;
int second;

// Prompt for and read the two integers.

System.out.print("First integer: ");
first = Keyboard.readInt();
System.out.print("Second integer: ");
second = Keyboard.readInt();

// Display the results of various bitwise operations.

System.out.println("\n\t" + first + " & " + second + " = " +
                   (first & second));
System.out.println("\t" + first + " | " + second + " = " +
                   (first | second));
System.out.println("\t" + first + " ^ " + second + " = " +
                   (first ^ second));
System.out.println("\t" + first + " << " + second + " = " +
                   (first << second));
System.out.println("\t" + first + " >> " + second + " = " +
                   (first >> second));
System.out.println("\t" + first + " >>> " + second + " = " +
                   (first >>> second));

Note: If a shift value greater than 31 is specified, only the right-most 5 bits of the shift factor will be used for shifting. In other words, a shift factor of 32 is the same as a shift factor of 0.

 

Decision handling

 

Looping

Example:

// Variable for loop control.

char again;

// Variables for holding fahrenheit and celsius temperatures.

double tempF;
double tempC;

// This is the main processing loop. It converts one fahrenheit
// temperature and asks the user if they want to convert another.

do {

  // Prompt for and read the fahrenheit temperature.

  System.out.print("Enter the fahrenheit temperature (nn.n): ");
  tempF = Keyboard.readDouble();

  // Convert to celsius and display the result.

  tempC = 5 * (tempF - 32) / 9;
  System.out.println("Celsius equivalent is " + tempC);

  // Ask the user if they want to convert another temperature and
  // read their reply.

  System.out.print("Again? (Y/N): ");
  again = Keyboard.readChar();

} while (again == 'Y' || again == 'y'); // Loop as required.

 

Defining a method

Example:

// This method converts a fahrenheit temperature to celsius.

public static double fToC(double oldTemp) {
  double newTemp = 5 * (oldTemp - 32) / 9;
  return newTemp;
}

Notes:  

  1. Because the method is public, it may be called from outside the class.

  2. Because the method is static, it may be called without instantiating an object of the class.

  3. The fToC method expects a double parameter representing the fahrenheit temperature and returns a double representing the celsius temperature.

  4. The variable newTemp is local to the method and may not be accessed from outside the method. Unlike C++ which permits the redefinition and hiding of variables within nested code, Java will not allow the variable to be redefined elsewhere in the method or in nested statement blocks.

 

Calling a method

identifier(arguments)

where the identifier is the name of the method. If the method is in a different class, the class name must be coded as a qualifier. For example,

System.out.println("Celsius equivalent is " + Utility.fToC(tempF));

calls the method named fToC in a class named Utility and passes it the value of the variable tempF. The result is converted to a string and appended to the string literal before displaying on the console.