}Identifier
A name in java program is called identifier. It may be class name, method name, variable name and label name.
}
Class Test{ |1public static void main(string args) {| | |2 3 4int x = 10;|5}
- a toz
- A toZ
- 0 to9
- _ (underscore)
- $
- total_number-------valid
- Total#------------------invalid
- ABC123---------valid
- 123ABC---------invalid
class Test {int number=10;int Number=20;int NUMBER=20; we can differentiate with case. int NuMbEr=30;}
class Test { public static void main(String[] args){ int String=10; System.out.println(String); } }
Output:
10
Example 2:
class Test { public static void main(String[] args) { int Runnable=10; System.out.println(Runnable); } }
10
Reserved words
Reserved words fordata types: (8)
|
|
|
|
|
Keywords for exception handling:(6)
|
Class related  keywords:(6);
|
Object related keywords:(4)
|
Void return type keyword:
|
Unused keywords:goto:Create several problems in old languages and hence it is banned in java.Const:Use final instead of this. |
Reserved literals:
1) true values for boolean data type.
2) false
3) null-----------
------ default value
for object reference.
|
Enum:
This keyword introduced
in 1.5v to define a group of
named constants
Example:
enum Beer
{
KF, RC, KO, FO;
}
|
- All reserved words in java contain only lowercase alphabet symbols.
- New keywords in java are:
- strictfp-----------1.2v
- assert-------------1.4v
- enum--------------1.5v
- In java we have only new keyword but not delete because destruction of useles objects is the responsibility of Garbage Collection.
- instanceof but not instanceOf
- strictfp but not strictFp
- const but not Constant
- syncronized but not syncronize
- extends but not extend
- implements but not implement
- import but not imports
- int but not Int
- final, finally, finalize (invalid) //here finalize is a method in Object class.
- throw, throws, thrown(invalid) //thrown is not available in java
- break, continue, return, exit(invalid) //exit is not reserved keyword
- goto, constant(invalid) //here constant is not reserved keyword
- byte, short, Integer, long(invalid) //here Integer is a wrapper class
- extends, implements, imports(invalid) //imports keyword is not available in java
- finalize, synchronized(invalid) //finalize is a method in Object class
- instanceof, sizeOf(invalid) //sizeOf is not reserved keyword
- new, delete(invalid) //delete is not a keyword
- None of the above(valid)
- public(valid)
- static(valid)
- void(valid)
- main(invalid)
- String(invalid)
- args(invalid)
Data types
- Size: 1byte (8bits) Maxvalue: +127
- Minvalue:-128
- Range:-128to 127[-27 to 27-1]
- The most significant bit acts as sign bit. "0" means "+ve" number and "1" means "–ve" number.
- "+ve" numbers will be represented directly in the memory whereas "–ve" numbers will be represented in 2's complement form.
Int:
- This is most commonly used data type in java.
- Size: 4 bytes
- Range:-2147483648 to 2147483647 (-231 to 231-1:
long:
- Whenever int is not enough to hold big values then we should go for long data type. Example:
- To hold the no. Of characters present in a big file int may not enough hence the return.
- type of length() method is long.
- long l=f.length();//f is a file Size: 8 bytes
- Range:-263 to 263-1
- Note: All the above data types (byte, short, int and long) can be used to represent whole numbers. If we want to represent real numbers then we should go for floating point data types.
- If we want to 5 to 6 decimal places of accuracy then we should go for float.
- Size:4 bytes.
- Range:-3.4e38 to 3.4e38. float follows single precision.
- If we want to 14 to 15 decimal places of accuracy then we should go for double.
- Size:8 bytes.
- -1.7e308 to1.7e308.
- double follows double precision.
Boolean Data Type
Range: Not applicable but allowed values are true or false.
Which of the following boolean declarations are valid?
Char data type
- In old languages like C & C++ are ASCII code based the no.Of ASCII code characters are < 256 to represent these 256 characters 8 - bits enough hence char size in old languages 1 byte.
- In java we are allowed to use any worldwide alphabets character and java is Unicode
- based and no.Of unicode characters are > 256 and <= 65536 to represent all these
- characters one byte is not enough compulsory we should go for 2 bytes.
- Size: 2 bytes
- Range: 0 to 65535
Literals
int x = 10 | | |_ Constant value | literals | | | |______ Name of Variable | Identifier | |__________ Data Type | Keyword
Integral Literals:
- Decimal literals: Allowed digits are 0 to 9. Example: int x=10;
- Octal literals. : Allowed digits are 0 to 7. Literal value should be prefixed with zero. Example: int x=010;
- Hexa Decimal literals:
- The allowed digits are 0 to 9, A to Z.
- For the extra digits we can use both upper case and lower case characters.
- This is one of very few areas where java is not case sensitive.
- Literal value should be prefixed with ox(or)oX.
Floating Point Literals
1. float f=123.456; //C.E:possible loss of precision(invalid)
2. float f=123.456D; //C.E:possible loss of precision(invalid)
3. double d=0x123.456; //C.E:malformed floating point literal(invalid)
4. double d=0xFace; (valid)
5. double d=0xBeef; (valid)
We can assign integral literal directly to the floating point data types and that integral literal can be specified in decimal , octal and Hexa decimal form also.
double d=0xBeef;
But we can't assign floating point literal directly to the integral types.
int x=10.0;//C.E:possible loss of precision
double d=10e2;//==>10*102(valid)
float f=10e2;//C.E:possible loss of precision(invalid) float f=10e2F;(valid)
String literals
- Binary Literals
- Usage of '_' in Numeric Literals
Binary Literals
- Decimal
- Octal
- Hexa decimal