Reference

Data Types and Comments

Declaration

  datatype varName;
  datatype varName = varValue;

Integer Data Types

  int   4 bytes -2*109  to 2*109
  short 2 bytes -32,000   to 32,000
  long  8 bytes -9*1018   to 9*1018
  byte  1 byte  -128  to 127

Floating Point Data types

  float   4 bytes approx 7 decimal places
  double  8 bytes approx 15 decimal places

Other Data Types

  char    2 bytes unicode character set
  boolean true or false

Examples

  int i;      //this is a declaration
  int j = 37; //this is initialization
  i = 42 ;    //this is assignment

Conversion

Implicit casting from left to right,

  byte -> short -> int -> long -> float -> double

and

  char -> int

Explicit casting example,

  double x = 9.997;
  int nx = (int)x;

x has the value 9.

Use Math.round method for rounding,

  double x = 9.997;
  int nx = (int)Math.round(x);

Constants

  final int CM_PER_INCH = 2.54;

Single Line Comment

  //This is a single line comment

Multiple Line Comment

  /*
    this is a
    multiple line
    comment
  */