Java Keywords
abstract , const, final, int,  public, throw,assert  ,continue, finally, interface, return, throws, 
boolean, default, float, long,  short, transient, break, do, for, native, static, true,byte, double, goto, new,  strictfp, try,case, else, if, null, super, void,catch, enum, implements,  package, switch, volatile,char, extends, import, private, synchronized,  while,class, false, instanceof ,protected this  
• Keywords use letters only; they do not use special characters  (such as $ , _ , etc.) or digits.
• All keywords are in all lowercase letters.
• Watch out for keywords that are from languages other than  Java, such as friend , include , and unsigned , which are not keywords in Java.  
• The words goto and const are reserved to the extent that  programmers cannot use them, but they have no specific meaning in the Java language. The meanings of other  keywords will be explained at appropriate places in this book.
Data Type
1. Primitive Data Types  :
a primitive variable holds the value for a primitive data type 
• boolean :  This data type is used to  represent a binary condition: true or false .
 So, at Java level, its size is 1  bit. The bit turned on represents 
true , and off represents false .
• byte : This type is an 8-bit, signed, two's  complement integer. Therefore, the range of values it can support is 
–27 to 27 – 1 (i.e. -128 to 127).
• short : This type is a 16-bit, signed, two's  complement integer. Therefore, the range of values it can support is –215 to 215  – 1 (-32,768 to 32,767). 
• char : This type is a 16-bit, unsigned  integer that is used to represent keyboard characters. Remember that a variable
  of this type never holds a negative value. Therefore, the range of values it can  support is 0 to 216 – 1 (0 to 65,535). 
• int : This type is a 32-bit, signed, two's  complement integer. Consequently, the range of values it can support is –231 to  231 – 1 (–2,147,483,648  to 2,147,483,647). This is the default type in the  integer arithmetic—that is, the arithmetic manipulations of integers that specifically could be represented by byte , short , or int . 
• long : This type is a 64-bit, signed, two's  complement integer. Consequently, the range of values it can support is –263 to  263 – 1 (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807). 
• float : The variable of  type float can hold a 32-bit, signed floating-point number. The range of values  it can support is +/–3.4028234738.
•  
double : 
A variable of type double can hold  a 64-bit, signed floating-point number. The range of values it can support is +/–1.79769313486231570308.
 
The general syntax for declaring and initializing a variable is shown here:
 modifier> dataType> variableName; =  initialValue
For example, the following statement declares a private variable id of data type int and assigns it an initial value of 10 that can later be changed:  private int id = 10;
 
Naming the Variables: Legal Identifiers 
Each variable has a name, given to it by the programmer while  declaring the variable. This name is called an identifier. For example,  id in the example in the previous section is an example of an identifier.
Following are the rules to name a variable (or define an  identifier):
• The first character of an identifier must be a letter, a  dollar sign ( $ ), or an underscore ( _ ).
• A character other than the first character in an identifier  may be a letter, a dollar sign, an underscore, or a digit.
• None of the Java language keywords (or reserved words) can be  used as identifiers.
So, name your variables according to the first two rules, and  do not use a reserved name because it will make an illegal identifier.
 
Following are some examples of legal and illegal identifiers: 
• 4us : This is an illegal identifier because the first  character of a legal identifier must be a letter, a dollar sign, or an  underscore, not a number.
• weigh#110 : This is an illegal identifier due to the presence  of the # sign. The only allowed characters are a dollar sign,
 an underscore, a  digit, and a letter.
• __420_lotto$ : As weird as this may look, it is a legal  identifier. Compare it to the rules in the preceding list.
Variable:
Local variables,  Instance variables, Static variables 
•Local variables : All the variables  declared inside a method Their scope is the method. 
In other words, they can be  accessed only from inside the method in which they are declared, and they are  destroyed when the method completes. Local variables are also called stack  variables because they live on the stack. 
•Instance variables : The variables  declared inside a class but outside of any method. 
Their scope is the instance  of the class in which they are declared. 
These variables, for example, can be  used to maintain a counter at instance level. 
Instance variables live on the  heap.
• Static variables : The instance  variables declared with the modifier static . 
Their scope is the class in which  they are declared. These variables can be used to maintain a counter at class  level, which is a variable shared by all the objects of the class.
 
Lateral : A literal is a value assigned to a variable in the source code  as opposed to the value determined at the time of program execution; i.e. a  literal is a value and not a variable. Therefore, a literal can appear only on  the 
right side of the assignment operator, such as int id = 10;   literal is a value and not a variable and It is raw data 
Default Initial Values for Variables of Different Data Types 
 
Type Default Initial Value 
boolean = false
byte = 0 
short = 0 
char = `\u0000` 
int = 0 
float = 0.0f 
double = 0.0d 
long = oL 
Nonprimitive Data Types
All nonprimitive data types in Java are objects, When you  declare a variable of a nonprimitive data type, you actually declare a variable  that is a reference to the memory where an object lives. Therefore, the variable  of a nonprimitive data type is called a reference variable , or an  object reference.
While a variable of a primitive data type holds the  value itself, the reference variable of a nonprimitive data type is only a  reference to the memory where the object is actually placed 
# A primitive variable holds the value of the data item, while  a reference variable holds the memory address where the data item (object) is  stored.
Arrays
Arrays in Java are objects that are used to store multiple  variables of the same type. These variables may be of primitive types or of  nonprimitive types (i.e. object references). Whether an array stores a primitive  variable or an object 
eference, the array itself is always an object. 
You declare an array by specifying the data type of the  elements that the array will hold, followed by the identifier, plus a pair of  square brackets before or after the identifier. The data type may be a primitive  or a class. 
Making an array of data items consists of three logical steps:
1. Declare an array variable.
2. Create an array of a certain size and assign it to the  array variable. 
 
3. Assign a value to each array element. 
 
Declaring an Array Variable  
Following is an example of declaring an array variable of primitive data type  int : int[] scores; 
The preceding syntax is used in this tutoril, but the following is also  legal: int scores []; 
Similarly, the following is an example of declaring an array variable of a  nonprimitive data type, 
where Student is the name of a class: Student[] students;  
Again, the following is also a legal declaration: Student students[];  
It is not legal to include the size of an array in the  declaration. For example, the compiler will generate an error on the following  statement
 : int [5] scores; The size is  included when you create the array.
Creating an Array 
Because an array is an object, you create it with the new  operator. An array of primitives is created and assigned to an already declared  array variable as shown in this example: scores = new int[3];  
This statement creates an array of three elements that can hold integer values and assigns it to the array variable scores , which is already declared.
An array of a nonprimitive data type is created and assigned to an already  declared array variable
as shown in the following example: students = new Student[3];  
This statement creates an array of three elements that can hold object references (which will reference to the objects of the Student class) and  assigns it to the array variable students , which we have already declared.
# The number of elements in an array, that is, the array size, is specified  when you create an array, not when you declare
 it. Once you create an array of a  specific size, you cannot change the size later.
Understanding the enum Type
The data type enum , introduced in J2SE 5.0, is useful when you want a  variable to hold only a predetermined set of values. You should use enums any  time you need a fixed set of constants, including natural enumerated types such  as days of the week. You define an enum variable in two steps: 
1. Define the enum type with a set of named values. 
2. Define a variable to hold one of those values. 
Following is an example: enum AllowedCreditCard {VISA, MASTER_CARD, AMERICAN_EXPRESS}; AllowedCreditCard visa = AllowedCreditCard.VISA;
No comments:
Post a Comment