Tuesday, June 25, 2013

Java Variables, Methods, Classes, Constants, naming and escape sequences

Variables

  • Can start with dollar symbol or underscore
  • Cannot start with a number
  • Cannot be a reserved word
  • No limit on the length of the name
<datatype> <variable name>;
int number = 1;

// array
<dataytpe> [] arrayname = new <datatype> [<number of elements>];
int [] list = new int[10];

// multi-dimensional arrays
int matrix [] [] = new int[4] [5];

Method Names

  • Verbs or verb phrases
  • first letter of the method name is lowercase
  • first letter of subsequent words is capitalized
  • Cannot be reserved word
  • Example: calculateMonthlyPayments

Class and Interface Names

  • Begins with capital letter
  • First letter of each word in the name is capitalized

Constant Names

  • Use all caps
Example: Color.BLUE

Escape sequences

  • \n New line
  • \t Horizontal tab
  • \b Backspace
  • \r Carriage return
  • \f Form feed
  • \\ Backlash
  • \" Double quote
  • \ddd Octal character
  • \xdd Hexadecimal
  • \udddd Unicode characters

Operators

  • <
  • <=
  • == ( for primitives compares values; for reference types compares object identity; for reference types, by convention, equals() method is implemented to compare values contained in the object for determining equality; String class even has equalsIgnoreCase)
  • !=
  • >=
  • >
  • &&
  • ||
  • !