Java Reference Index


  1. Introduction
  2. Data Types
  3. Arrays
  4. Operators
  5. Program Control
  6. Classes
  7. Objects
  8. Interfaces
  9. Containers
  10. Class Structure
  11. Error Handling

    Program Types

  12. Applications
  13. Applets

    How To

  14. Type Conversions

    Packages

  15. java.lang.reflect
  16. java.applet
  17. java.awt
  18. java.beans
  19. java.io
  20. java.lang
  21. java.math
  22. java.net
  23. java.rm
  24. java.security
  25. java.sa
  26. java.text
  27. java.util
  28. java.servlet

    Classes

  29. Object Class

    Appendices

  30. Terms
  31. Credits

Data

Java Variable Name Declaration

Variable names must begin with one of:

  • A letter (a-z, A-Z)
  • Dollar sign ($)
  • Underscore (_)

Any letter or number may be used in the variable name after the first character. Beginning variable names with numbers is not allowed. To make it easier to spot the words, the following rule of thumb is used:

  • The first letter of the variable name is lowercase.
  • Each successive word in the variable name begins with a capital letter.
  • All other letters are lowercase.

In addition to a name, a variable declaration must include the type of information being stored" which may be a basic data type, array, class, or interface.

Scope

  • Local - Variables with local scope are only accessable by the method or block in which they are declared.
  • Global

Variable Scope

class ClassName
{
   member variables are here

   public void Method1(parameters)
   {
       Local variables are here
   }
}

Member variables remain as a part of the object or class, but local variables are gone when the method exits. Two other types of variable scope exist which are:

  • Method parameter - This is the scope of the variable being passed to the method.
  • Exception handler parameter - Inside the method may be exception handler code for handling any errors that occur during the execution of the method. These variables are passed to the exception handler and are local to the exception handler code.

Java Basic Data Types

Also called primitive types.

  • Integers - All types are signed.
    • byte - 8 bits
    • short - 16 bits
    • Int - 32 bits
    • long - 64 bits
  • Floating point numbers
    • float
    • double - For more precise real number calculations.
  • Characters - char
  • Boolean - boolean - True or false.

Literal Value Representations

  • Octal - The value is prepended with a 0 such as 0737.
  • Hexadecimal - The value is prepended with 0x such as 0x4ab2

All other values are considered to be in decimal form. Floating point values use a period for a decimal point. A floating point value can also be expressed using the 'e' or 'E' character such as "3e12".

Character Literal Values

Codes representing special non printable characters.

  • \b - Backspace
  • \d - Octal
  • \f - Formfeed
  • \n - New line
  • \r - Carriage return
  • \t - Tab
  • \un - Unicode character. The character is symbolized by the "N" character.
  • \xn - Hexadecimal. The character is symbolized by the "N" character.
  • \\ - Backslash
  • \ - Single quotation mark
  • \" - Double quotation mark

Java Complex Data Types

Complex data types in Java are objects.

String netIPAddress = "192.168.1.1";
ClassData dataobj;
Socket clientSocket = null;

Keywords used in Variable Declarations

  • final - Used to set variable values so they cannot be changed as in the following example:

    final int maxValue = 10;

  • static - Used to declare class variables

Other key words set variable scope such as:

  • Public - Any class may access the member.
  • Protected - Only subclasses may access the member.
  • Private - No other class may access the data member.