Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Language Fundamentals - Variable declarations and Identifiers

Syntax

        modifiers Type declarator;

Example:
        public int i;
        private long myNumber;
        protected myVar = 10;        
  • variables provide named access to data stored in memory
  • variables may be declared as a primitive type or a reference type
  • Java supports two different kinds of variables: field or class variables and local or automatic variables
  • field variables are declared as members of a class; they store information (data) relating to an object
  • valid field modifiers: public, protected, private, final, static, transient, volatile
  • local or automatic variables are declared within methods; they are temporary placeholders which store values and references to data for objects being operated on by the method
  • valid local modifiers: final

Identifiers

  • an identifier is an unlimited-length sequence of Java letters and Java digits
  • an identifier cannot have the same spelling as a Java keyword, boolean literal, or null literal
  • valid identifiers begin with one of the following:
    • a Unicode letter
    • the underscore character ( _ )
    • a dollar sign ( $ )
  • JLS §3.8 recommends that the dollar sign only be used for identifiers that are mechanically generated (ie within IDE's)
  • JPL pg 5.4 recommends sticking to one language when writing identifiers as a number of characters look alike in various languages but have seperate Unicode values
  • methods and variables can have the same names; method identifiers always take the form
        methodName()
    
    the parantheses allow Java to recognize the identifier as a method vs a variable and therefore distinguish between the two.

Naming Conventions

  • Package names - lowercase.for.all.components
  • Class and Interface names - CaptializedWithInternalWordsCaptialized
  • Method names - firstWordLowercaseButInternalWordsCapitalized()
  • Variable names - firstWordLowercaseButInternalWordsCaptialized
  • Constants - UPPER_CASE_WITH_UNDERSCORES

Tips

  • variables can have the same name as a method or a class

Traps

  • local (automatic) variables declared with a modifier other than final
  • identifier names beginning with a number or # sign


Source Package Import Class Interface Constructors
  Methods main() Identifiers Keywords Defaults Arrays
  Primitives # Literals char Literal String Literals Class Literals