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
Identifiers
“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:
- Primitive types
- 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.
- Logical: Boolean
- Textual: char
- Integral: byte, short, int and long
- Floating: double and float
How to declare variables, Declarations and Assignments
- Public class Assign {
- Public static void main(String[] args) {
- Integer variables int x = 6, y = 1000;
- Float z = 3.414 f;
- Boolean truth = true;
- Char c = ’A’;
- }
- }
- In Java programming, beyond primitive types, all other types are reference types.
- A reference variable contains a handle to an object.
Example:
- Public class MyDate {
- Private int day = 9;
- Private int month = 11;
- Private int year = 1991;
- Public MyDate(int day, int month, int year) {
- …………..
- }
- Public String tostring() {
- …………}
- }
- Public class TestMyDate {
- Public static void main(String[] args) {
- MyDate today = new MyDate(9, 11, 1991);
- }
- }
- 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 this keyword are:
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 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