Basic Concept of Identifiers and Variables in Java

Background of this article

  • Distinguish between valid and invalid identifiers
  • List of eight primitives types
  • Define literal values for numeric and textual types
  • Define the terms variables and reference variable


“An identifier is a name given to a variable, class or method.”

Identifiers have the following characteristics:

Can start with a Unicode letter, underscore (_), or dollar sign ($).

Are case-sensitive and have no maximum length.

Examples of valid identifiers:

  • identifier
  • username
  • user_name
  • _sys_var1
  • $chnage

The Java programming language supports the following two basic data types:

  1. Primitive types
  2. Class types

“Primitive data types are simple values, not objects.”

  • The Java programing language defines eight primitive data types, that can be considered in the following four categories:
    1. Logical: Boolean
    2. Textual: char
    3. Integral: byte, short, int and long
    4. Floating: double and float

      “Class types are used for more complex types, including all the types that you declare yourself.” They are used to create objects.

      How to declare variables, Declarations and Assignments
      1. Public class Assign  
      2.     {  
      3.         Public static void main(String[]args)  
      4.           {  
      5.              //declare and assign values to int  
      6.              Integer variables int x=6, y=1000;  
      7.              //declare and assign floating value  
      8.              Float z=3.414f;  
      9.              //declare and assign value to Boolean   
      10.              Boolean truth=true;  
      11.              //declare and assign value to char variable  
      12.              Char c=’A’;  
      13.           }  
      14.     }  
  • In Java programming, beyond primitive types all other types are reference types.
  • A reference variable contains a handle to an object.

    1. Public class MyDate  
    2.     {  
    3.         Private int day=9;  
    4.         Private int month=11;  
    5.         Private int year=1991;  
    6.         Public MyDate(int day, int month, int year)  
    7.         {…………..}  
    8.         Public String tostring()  
    9.         {…………}  
    10.       }  
    12. Public class TestMyDate  
    13.      {  
    14.         Public static void main(String[]args)  
    15.           {  
    16.                MyDate today=new MyDate(9,11,1991); // create object of MyDate Class  
    17.           }  
    18.       }  
  • Variable “today” is a reference variable holding one object of “MyDate” class

    Calling new xyz() performs the following actions:
    • Memory is allocated for the object
    • Explicit attribute initialization is done
    • A constructor is executed
    • The object reference is returned by the new operator
    • The reference to the object is assigned to a variable

    For example:

    MyDate my_birth = new MyDate(9, 11, 1991);

    In a single Java Virtual Machine, the Java programming language only passes arguments by value.

    When an object instance is passed as an argument to a method, the value of the argument is a reference to the object.

    The contents of the object can be changed in the called method, but the original object reference is never changed.

    Two uses of the this keyword are:

      1. To resolve ambiguity between instance variables and parameters
      2. To pass the current object as a parameter to another method or constructor

    Let's see how to define a reference type instance variable in a Java class and manipulate the object referenced by this variable.

    Examples of coding conventions

    The following are examples of coding conventions.

    Packages: com.example.domain;

    Classes, interfaces and enum types: SavingsAccount

    Methods: getAccount()

    Variables: CurrentCustomer

    Constants: HEAD_COUNT

    Variables defined inside a method are called local variables, also referred to as automatic, temporary, or stack variables. Local variables must be initialized before the first use.

    Variables defined outside a method are created when the object is constructed using the new xxx() call. They are the following two types:

    1. Static variables: They are created when the class is loaded and continue to exist for as long as the class is loaded.

    2. Instance variables: They are declared without using the static keyword. They continue to exist for as long as the object exists.

    SO Friends these are a few articles that will help you in with the basic concepts of identifiers and variables. The next time I will provide more commands that re interesting, thank you. I hope this is helpfull for you... enjoy :).KR

Up Next
    Ebook Download
    View all
    View all