Java Notes
Java Notes
Identifiers:
Identifier is a name assigned to the programming elements like variables, methods,
classes, abstract classes, interfaces,.....
EX:
int a=10;
int ---> Data Types
a ---> Variable [Identifier]
= ---> Operator
10 ---> Constant
; ---> Terminator (Special Symbol)
To provide identifiers in java programming, we have to use the following rules and
regulations:
i. The only allowed characters in java identifiers are:
1) a to z
2) A to Z
3) 0 to 9
4) _
5) $
Ex.:-
int eno=111; // Valid
int 9eno=999; // Invalid
String _eaddr="Jai"; // Valid
float $esal=50000.0f; // valid
String emp9No="E-9999"; // Valid
String emp_Name="Dinesh"; // Valid
float emp$Sal=50000.0f; // Valid
ii. Identifiers do not allow all operators and all special symbols except '_'
and '$' symbols.
Ex.:-
int empNo=111; // valid
int emp+No=111; // Invalid
String emp*Name="Dinesh"; // Invalid
String #eaddr="Jai"; // Invalid
String emp@Jai="Dinesh"; // Invalid
float emp.Sal=50000.0f; // Invalid
String emp-Addr="Jai"; // Invalid
String emp_Addr="Jai"; // Valid
iii. java identifiers are case sensitive up course java language itself treated
as case sensitive language.
Ex.:-
class Test{
int number=10;
int Number=20;
int NUMBER=20; // We can differentiate with case.
int NuMbEr=30;
}
iv. We can’t use reserved words as identifiers.
Ex.:-
int if=10; // invalid
v. Identifiers do not allow spaces in the middle.
Ex.:-
concat() // Valid
forName() // Valid
for Name() // Invalid
getInputStream() // valid
get Input Stream() // Invalid
vi. Identifiers should not be duplicated with in the same scope, identifiers may
be duplicated in two different scopes.
Ex.:-
class A {
int i=10; // Class level
short i=20; // Error
double f=33.33; // No Error
void m1() {
float f=22.22f; // local variable
double f=33.33; // Error
long i=30; // No Error
}
}
viii. In java applications, we can use all predefined class names and interface
names as identifiers.
EX1:
int Exception=10;
System.out.println(Exception);
Status: No Compilation Error
Output: 10
EX2:
String String="String";
System.out.println(String);
Status: No Compilation Error
Output: String
EX3:
int System=10;
System.out.println(System);
Status: Compilation Error
Note: Specifying class names and interface names along with package names is
called as Fully Qualified Name.
EX:
java.io.BufferedReader
java.util.ArrayList
EX:
int System=10;
java.lang.System.out.println(System);
System=System+10;
java.lang.System.out.println(System);
System=System+10;
java.lang.System.out.println(System);
Status: No Compilation Error
Output: 10
20
30
ix. Along with the above rules and regulations, JAVA has provided a set of
suggestions to use identifiers in java programs. In java applications, it is
recommended to provide identifiers with a particular meaning.
EX:
String xxx="abc123"; // Not Recommended
String accNo="abc123"; // Recommended
x. In java applications, we don’t have any restrictions over the length of the
identifiers, we can declare identifiers with any length, but it is
Recommended to provide length of the identifiers around 10 symbols.
EX:
String permanentemployeeaddress="Jai"; // Not Recommended
String permEmpAddr="Jai"; // Recommended
EX:
String permEmpAddr="Jai"; // Not Recommended
String perm_Emp_Addr="Jai"; // Recommended
3. Data Types:-
➢ Every variable has a type; every expression has a type and all types are strictly
defined more over every assignment should be checked by the compiler by
the type compatibility hence java language is considered as strongly typed
language.
Ex.:-
i = 10; > invalid, no data type representation.
int i=10;--> Valid, data type is represented.
➢ Because of above reasons we can conclude java language is strongly/ Strictly
typed programming language.
➢ In java applications, data types are able to provide the following advantages:
i. We are able to identify memory sizes to store data.
EX:-
int i=10; // int will be provided 4 bytes of memory.
ii. We are able to identify range values to the variable to assign.
EX:-
byte b=130; > Invalid
byte b=125; > Valid
Reason: 'byte' data type is providing a particular range for its variables like -128 to
127, in this range only we have to assign values to byte variables.
FAQ: Java is pure object-oriented programming or not?
Java is not considered as pure object-oriented programming language because
several oops features (like multiple inheritance, operator overloading) are not
supported by java moreover we are depending on primitive data types which are
non-objects.
➢ To prepare java applications, JAVA has provided the following data types.
➢ float data type can hold 5 to 6 digits after point(.) while double data type
can hold 14 to 15 digits after point (.).
➢ float data type supports single precision while double data type supports
double precision. Here precision indicates accuracy.
3) Non-Numeric Data types: -
If we want to identify range values for variables on the basis of data types then we
have to use the following formula:
-2n-1 to 2n-1 - 1
Here 'n' is no. of bits.
EX:-
Data Type: byte
size= 1 byte = 8 bits.
-28-1` to 28-1-1
-27 to 27 - 1
-128 to 128 – 1
-128 to 127
Note:- This formula is applicable upto Integral data types, not applicable for other
data types.
➢ To identify "min value" and "max value" for each and every data type, JAVA
has provided the following two constant variables from all the wrapper
classes.
MIN_VALUE and MAX_VALUE
Note:- Classes representation of primitive data types are called as Wrapper Classes
Primitive Data Types Wrapper Classes
byte java.lang.Byte
short java.lang.Short
int java.lang.Integer
long java.lang.Long
float java.lang.Float
double java.lang.Double
char java.lang.Character
boolean java.lang.Boolean
EX:-
class Test{
public static void main(String[] args){
System.out.println(Byte.MIN_VALUE+"----->"+Byte.MAX_VALUE);
System.out.println(Short.MIN_VALUE+"----->"+Short.MAX_VALUE);
System.out.println(Integer.MIN_VALUE+"----->"+Integer.MAX_VALUE);
System.out.println(Long.MIN_VALUE+"----->"+Long.MAX_VALUE);
System.out.println(Float.MIN_VALUE+"----->"+Float.MAX_VALUE);
System.out.println(Double.MIN_VALUE+"----->"+Double.MAX_VALUE);
System.out.println(Character.MIN_VALUE+"----->"+Character.MAX_VALUE);
//System.out.println(Boolean.MIN_VALUE+"----->"+Boolean.MAX_VALUE);
}
}
byte:
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.
Ex:-
byte b=10;
byte b2=130;// C.E: possible loss of precision
byte b=10.5;// C.E: possible loss of precision
byte b=true;// C.E: incompatible types
byte b="durga";// C.E: incompatible types
byte data type is best suitable if we are handling data in terms of streams either
from the file or from the network.
short:
The most rarely used data type in java is short.
Size: 2 bytes
Range: -32768 to 32767 [-215 to 215-1]
Ex.:-
short s=130;
short s=32768;// C.E: possible loss of precision
short s=true;// C.E: incompatible types
Short data type is best suitable for 16-bit processors like 8086 but these processors
are completely outdated and hence the corresponding short data type is also out
data type.
int:
This is most commonly used data type in java.
Size: 4 bytes
Range: -2147483648 to 2147483647 [-231 to 231-1]
Ex.:-
int i=130;
int i=10.5;// C.E: possible loss of precision
int i=true;// C.E: incompatible types
long:
Whenever int is not enough to hold big values then we should go for long data type.
Ex.:-
To hold the no. Of characters present in a big file and 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.
There is no direct way to specify byte and short literal explicitly but indirectly we
can specify. Whenever we assign integral literal to the byte variable, and if the value
comes within the range of byte then compiler treats it automatically as byte literal.
Similarly short literal also works.
Ex.:-
byte b = 127;// OK (Range: -128 to 127)
byte b = 128;// CTE: Cannot convert int to byte
short s = 32767;// OK (Range: -32768 to 32767)
short s = 32768;// CTE: Cannot convert int to short
ii. Floating Point Literals:
➢ By default, every floating-point literal is of double type and hence we
cannot assign directly to the float variable.
➢ But we can specify floating point literal as float type by suffixed with f
or F.
Ex.:-
float f = 123.456;// CTE: Cannot convert double to float
float f = 123.456f;//OK
double d = 123.456;//OK
➢ We can specify explicitly floating-point literal as double type by suffixed
with d or D, but this convention is not required. It happens implicitly.
Ex.:-
double d = 12345.6789D;// No need to use D
float f = 12345.1234d;// CTE: Type mismatch: cannot convert from
double to float
➢ We can specify floating point literals only in decimal form and we can’t
specify in octal and hexa-decimal forms.
Ex.:-
double d = 123.456;//OK
double d = 0123.456;//OK (0123.456 will treated as decimal)
double d = 0x123;// OK (Convert it into decimal)
double d = 0x123.456;// CTE: error: malformed floating point literal
➢ We can assign integral literal directly to floating point variables and that
integral literal can be specified either in decimal or octal or hexa-
decimal forms.
Ex.:-
double d = 10;//OK Output: 10.0
double d = 0786;//(CE: Integer Number too Large)
double d = 0786.0;// OK
double d = 0777;// OK (0777 comes under 0-7)
double d = 0xFace;// OK
double d = 0xFace.0;// CE: malformed floating-point literal
➢ We cannot assign floating point literals to integral types.
Ex.:-
double d = 10;//OK
int i = 10.0.;// CE: Possible loss of precision (found: double,
required: int)
➢ We can specify floating point literal even in exponential(exponent)
form.
Ex.:-
double d = 1.2e3;//OK 1.2e3 means 1.2*103
float f = 1.2e3;// CE: Possible loss of precision (found: double,
required: float)
float f = 1.2e3f;//OK
iii. Boolean Literals:
➢ The only allowed values for Boolean data type are true or false.
boolean ---> true, false
Ex.:-
1) boolean b=true;// (valid)
2) boolean b=0;// C.E: incompatible types(invalid)
3) boolean b=True;// C.E: cannot find symbol(invalid)
4) boolean b="true";// C.E: incompatible types(invalid)
\n New Line
\t Horizontal Tab
\r Carriage Return
\b Back Space
\f Form Feed
v. String Literals:
➢ Any sequence of characters within double quotes is treated as string
literal.
Ex.:-
String s = “Java”;
JDK 1.7 version enhancement with respect two literals:-