Durga Sir Java Notes
Durga Sir Java Notes
Durga Sir Java Notes
Note
By Bhushan Laware
bhushanlaware@gmail.com
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
Example:
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
digit. 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=40;
identifiers. Example:
int if = 10 ►invalid
F. All predefined java class names and interface names we use as
identifiers. Example 1:
class Test
System.out.println(String);
►Output: 10
Example 2:
class Test
System.out.println(Runnable);
►Output: 10
Diagram:
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
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:
reference. 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.
Every variable has a type, every expression has a type and all types are strictly
define moreover every assignment should be checked by the compiler by
the type compatibility hence java language is considered as strongly typed
language.
Diagram:
★ Note:
Except Boolean and char all remaining data types are considered as signed data
A. byte:
❏ Size: 1 Byte (8 bits)
❏ Max-value: +127
❏ Min-value: -128
❏ Range: 128 to 127 [-27 to 27-1]
Example:
byte b=10; ►✔
precision
● 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:
short s=130; ►✔
types
● 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
C. Int: ● completely outdated and hence the corresponding short data type is also
out data type.
❏ Size: 4 bytes
❏ Range: -2147483648 to 2147483647 (-231 to 231-1)
Example:
int i=130; ►✔
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
Size: Size:
4 bytes 8 bytes
Range: Range:
-3.4e38 to 3.4e38 -1.7e308 to1.7e308.
Example 1:
boolean b=true; ►✔
Example 2:
int x=10;
if( x )
System.out.println(“Hello”);
else
System.out.println(“HI”);
Example 3:
while(1)
System.out.println(“Hello”);
65535 Example:
char ch1=97; ►✓
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;
Example:
➤ 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
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
\t Horizontal tab
\r Carriage return
\f From feeb
\b Backspace character
\’ Single Quote
\” Double Quote
\\ 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[];
Example:
1. int[] a; ►valid
2. int[5] a; ►invalid
declaration: 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[];
int[] a=new
int[3]; 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
double[] [D
... ...
● Rules
1. At the time of array creation compulsory we should specify the
size otherwise we will get compile time error.
Example:
java. 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:
Example 2:
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];
Example 1:
System.out.println(a[0]); ►0
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
Example:
int[] a=new
int[4]; a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50; ►R.E:ArrayIndexOutOfBoundsException: 4
a[-4]=60; ►R.E:ArrayIndexOutOfBoundsException: -4