Durga Sir Java Notes
Durga Sir Java Notes
JAVA
Note
By Bhushan Laware
[email protected]
Contents
1. Language Fundamentals
2. Operators and Assignments
3. Declaration and Access Control
4. Flow Control
5. Exception Handling
6. Assertions
7. OO Concepts
8. Inner Classes
9. Threads and Concurrency
10. Fundamental classes in java.lang.package
a. Object Class
b. String class
c. StringBuffer Class
d. StringBuilder Class
e. Wrapper Classes
f. Math class
11. The Collections framework and Generics
12. File I/O & Serialization
13. Garbage Collection
14. 1.5 version New Features
a. Enum
b. For-Each Loop
c. . Var-Arg Methods
d. Auto Boxing & Unboxing
e. Static Imports
15. Internationalization
16. Quick Reference
1.
Language Fundamentals
1. Identifiers
2. Reserved words
3. Data types
4. Literals
5. Arrays
6. Types of variables
7. Var-arg method
8. Main method
9. Command line arguments
10. Java coding standards
1.1 Identifier
A name in java program is called identifier. It may be class name, method name,
variable name and label name.
Example:
{
int x=10;
System.out.println("Hello World");
}
}
If we are using any other character we will get compile time error.
Example:
1. Total_number ►valid
2. Total# ►invalid
Example:
1. ABC123 ►valid
2. 123ABC ►invalid
C. java identifiers are case sensitive up course java language itself treated as case
sensitive language
Example:
class Test
{
int number=10;
int Number=20;
int NuMbEr=30;
Int numBER=40;
}
D. There is no length limit for java identifiers but it is not recommended to take
more than 15 lengths.
E. We can’t use reserved words as identifiers.
Example:
int if = 10 ►invalid
F. All predefined java class names and interface names we use as identifiers.
Example 1:
class Test
{
int String=10;
System.out.println(String);
}
}
►Output: 10
Example 2:
class Test
{
int Runnable=10;
System.out.println(Runnable);
}
}
►Output: 10
Even though it is legal to use class names and interface names as identifiers but
it is not a good programming practice.
★ Which of the following are valid java identifiers?
1. _$_ ►valid
2. Ca$h ►valid
3. java2Share ►valid
4. all@hands ►invalid
5. 123abc ►invalid
6. Total# ►invalid
7. int ►invalid
8. Integer ►valid
1.2 Reserved words
In java some identifiers are reserved to associate some functionality or meaning such
type of reserved identifiers are called reserved words.
Diagram:
Reserved words for data types:
1. byte 4. long 7. char
2. short 5. float 8. boolean
3. int 6. double
Reserved words for flow control:
1. if 5. default 9. break
2. else 6. for 10. continue
3. switch 7. do 11. return
4. case 8. while
If a method won’t return anything compulsory that method should be declared with
the
Unused keywords:
1. goto: Create several problems in old languages and hence it is banned in java.
2. Const: Use final instead of this.
By mistake if we are using these keywords in our program we will get compile
time error.
Reserved literals:
Enum:
Example:
enum Beer
{
}
Notes:
★ All reserved words in java contain only lowercase alphabet symbols.
★ New keywords are:
1. strictfp → 1.2v
2. assert → 1.4v
3. enum → 1.5v
★ Which of the following list contains only java reserved words?
1. final, finally, finalize
➢ Invalid. Here finalize is a method in Object class.
2. throw, throws, thrown
➢ Invalid. thrown is not available in java.
3. break, continue, return, exit
➢ Invalid. exit is not reserved keyword.
4. goto, constant
➢ Invalid. Here constant is not reserved keyword.
5. byte, short, Integer, long
➢ Invalid. Here Integer is a wrapper class.
6. extends, implements, imports
➢ Invalid. imports keyword is not available in java.
7. finalize, synchronized
➢ Invalid. finalize is a method in Object class.
8. instanceof, sizeOf
➢ Invalid. sizeOf is not reserved keyword.
9. new, delete
➢ Invalid. delete is not a keyword.
10. None of the above
➢ Valid.
1.3 Data Types
Diagram:
★ Note:
Except Boolean and char all remaining data types are considered as signed data types
A. byte:
❏ Size: 1 Byte (8 bits)
❏ Max-value: +127
❏ Min-value: -128
❏ Range: 128 to 127 [-27 to 27-1]
Example:
● byte data type is best suitable if we are handling data in terms of streams either
from the file or from the network.
B. short:
❏ Size: 2 Bytes
❏ Range: -32768 to 32767 (-215 to 215-1)
Example:
● The most rarely used data type in java is short.
● 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.
C. Int:
❏ Size: 4 bytes
❏ Range: -2147483648 to 2147483647 (-231 to 231-1)
Example:
D. long:
❏ Size: 8 bytes
❏ Range:-263 to 263-1
Example:
long l= 13l;
● To hold the no. Of characters present in a big file int may not enough hence the
return type of length() method is long.
● Whenever int is not enough to hold big values then we should go for long data
type.
★ 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.
float double
If we want to 5 to 6 decimal places of If we want to 14 to 15 decimal places
accuracy then we should go for float. of accuracy then we should go for
double.
Size: Size:
4 bytes 8 bytes
Range: Range:
-3.4e38 to 3.4e38 -1.7e308 to1.7e308.
float follows single precision. double follows double precision
Example 1:
Example 2:
int x=10;
if( x )
System.out.println(“Hello”);
else
System.out.println(“HI”);
►Compiler error: incompatible types
Found: int
Require: boolean
Example 3:
while(1)
System.out.println(“Hello”);
►Compiler error: incompatible types
Found: int
Require: boolean
G. Char data type:
❏ Size: 2 bytes
❏ Range: 0 to 65535
Example:
★ The default value for the object references is “null”.
1.4 Literals
Any constant value which can be assigned to the variable is called literal.
Example:
A. Integral Literals:
For the integral data types (byte, short, int and long) we can specify literal value
in the following ways.
int x=10;
int y=010;
int z=0x10;
System.out.println(x+"----"+y+"----"+z);
►Output: 10----8----16
➤ By default every integral literal is int type but we can specify explicitly as long
type by suffixing with small “l” (or) capital “L”.
1. int x=10; ►valid
2. long l=10L; ►valid
3. long l=10; ►valid
4. x=10l; ►invalid ►C.E:possible loss of precision
➤ There is no direct way to specify byte and short literals explicitly. But whenever
we are assigning integral literal to the byte variables and its value within the
range of byte compiler automatically treats as byte literal. Similarly short
literal also.
1. byte b=10; ►valid
2. byte b=130; ►Invalid ►C.E:possible loss of precision
3. short s=32767; ►valid
4. short s=32768; ►Invalid ►C.E:possible loss of precision
Example:
Example:
1. double d=123.456D;
➤ We can specify floating point literal only in decimal form and we can’t
specify in octal and hexadecimal forms.
Example:
★ Which of the following floating point declarations are valid?
1. float f=123.456; ►Invalid ►C.E:possible loss of precision
2. float f=123.456D; ►Invalid ►C.E:possible loss of precision
3. double d=0x123.456; ►Invalid ►C.E:malformed floating point literal
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 octal and Hexadecimal form also.
Example:
double d=0xBeef;
System.out.println(d);
►Output: 48879.0
But we can’t assign floating point literal directly to the integral types.
Example:
Example:
C. Boolean literals:
The only allowed values for the boolean type are true (or) false where case is
important.
Example:
D. Char literals:
➤ A char literal can be represented as single character within single quotes.
Example:
1. char ch1='\u0061';
System.out.println(ch1);
►Output: a
2. char ch2=\u0062; ►Invalid ►C.E:cannot find symbol
3. char ch3='\iface'; ►Invalid ►C.E:illegal escape character
Example:
\n Newline
\\ Backspace
E. String literals:
● Any sequence of characters with in double quotes is treated as String
literal.
Example:
★ Diagram:
1.5 Arrays
1. Introduction
2. Array declaration
3. Array construction
4. Array initialization
5. Array declaration, construction, initialization in a single line.
6. length Vs length() method
7. Anonymous arrays
8. Array element assignments
9. Array variable assignments
I. Introduction
➤ An array is an indexed collection of fixed number of homogeneous data
elements.
➤ The main advantage of arrays is we can represent multiple values with
the same name so that readability of the code will be improved.
➤ But the main disadvantage of arrays is: Fixed in size that is once we
created an array there is no chance of increasing or decreasing the size
based on our requirement that is to use arrays concept compulsory we
should know the size in advance which may not possible always.
➤ We can resolve this problem by using collections.
II. Array declarations:
A. Single dimensional array declaration:
Example:
1. int[] a;
2. int []a;
3. int a[];
At the time of declaration we can’t specify the size otherwise we will get
compile time error.
Example:
1. int[] a; ►valid
2. int[5] a; ►invalid
Example:
int[][] a;
int [][]a;
int[] []a;
int[] a[];
int []a[];
C. Three dimensional array declaration:
Example:
int[][][] a;
int [][][]a;
int a[][][];
int[] [][]a;
int[] a[][];
int[][] []a;
int[][] a[];
int []a[][];
int [][]a[];
If we want to specify the dimension before the variable that rule is applicable
only for the 1st variable. Second variable onwards we can’t apply in the same
declaration.
Diagram:
● For every array type corresponding classes are available but these classes are
part of java language and not available to the programmer level.
int[] [I
int[][] [[I
double[] [D
... ...
● Rules
1. At the time of array creation compulsory we should specify the size
otherwise we will get compile time error.
Example:
Example:
int[] a=new int[0];
System.out.println(a.length); ►Output:0
3. If we are taking array size with -ve int value then we will get runtime
exception saying NegativeArraySizeException.
Example:
4. The allowed data types to specify array size are byte, short, char, int. By
mistake if we are using any other type we will get compile time error.
Example:
Example:
a[0]=new int[3][];
a[0][0]=new int[1];
a[0][1]=new int[2];
a[0][2]=new int[3];
a[1]=new int[2][2];
value automatically.
Example 1:
System.out.println(a[0]); ►0
★ Note: Whenever we are trying to print any object reference internally toString()
method will be executed which is implemented by default to return the
following.
classname@hexadecimalstringrepresentationofhashcode.
Example 2:
System.out.println(a); ►[I@3e25a5
System.out.println(a[0]); ►[I@19821f
System.out.println(a[0][0]); ►0
Example 3:
System.out.println(a[0]); ►null
System.out.println(a[0][0]); ►R.E:NullPointerException
● Once we created an array all its elements by default initialized with default
values. If we are not satisfied with those default values then we can replays with
our customized values.
Example:
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50; ►R.E:ArrayIndexOutOfBoundsException: 4