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 a 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:
    Class types are used for more complex types, including all the types that you declare yourself.” They are used to create objects.
    1. Logical: Boolean  
    2. Textual: char  
    3. Integral: byteshortint and long  
    4. Floating: double and float   
    How to declare variables, Declarations and Assignments
    1. Public class Assign {  
    2.  Public static void main(String[] args) {  
    3.   Integer variables int x = 6, y = 1000;  
    4.   Float z = 3.414 f;  
    5.   Boolean truth = true;  
    6.   Char c = ’A’;  
    7.  }  
    8. }  
  • 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.  Private int day = 9;  
    3.  Private int month = 11;  
    4.  Private int year = 1991;  
    5.  Public MyDate(int day, int month, int year) {  
    6.   …………..  
    7.  }  
    8.  Public String tostring() {  
    9.   …………}  
    10. }  
    11. Public class TestMyDate {  
    12.  Public static void main(String[] args) {  
    13.   MyDate today = new MyDate(9111991);  
    14.  }  
    15. }   
  • 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:
    1. MyDate my_birth = new MyDate(9111991);  
    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 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.
    1. Packages: com.example.domain;  
    2. Classes, interfaces and enum types: SavingsAccount  
    3. Methods: getAccount()  
    4. Variables: CurrentCustomer  
    5. 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 with the basic concepts of identifiers and variables.  The next time I will provide more commands that are interesting.  Thank you!  I hope this is helpful for you... enjoy :).KR