
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.
11. The Collections framework and Generics 12. File I/O & Serialization 13. Garbage Collection 14. 1.5 version New Features a.
b. For-Each Loop c. . Var-Arg Methods d.
Auto Boxing & Unboxing
15. Internationalization 16. Quick Reference
1. Language Fundamentals 1. 2. 3. 4. 5. 6. 7. 8. 9.
Identifiers Reserved words Data types Literals Arrays Types of variables Var-arg method Main method Command line arguments
10. Java coding standards
A name in java program is called identifier. It may be class name, method name, variable name and label name. Example: public class Main < public static void main(String[] args) < int x=10; System.out.println("Hello World"); >> ►So here, number of identifiers are 5.
Rules to define java identifiers A. The only allowed characters in java identifiers are a. a to z b. A to Z c. 0 to 9 d. _ e. $ If we are using any other character we will get compile time error.
Example: 1. Total_number
B. Identifiers are not allowed to starts with digit. Example: 1. ABC123
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=20;
we can differentiate with case.
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
F. All predefined java class names and interface names we use as identifiers. Example 1: class Test < public static void main(String[] args)< int String=10; System.out.println(String); >> ►Output:
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. _$_
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:
Reserved words for flow control: 1. if
Keywords for modifiers: 1. public
Keywords for exception handling: 1. try
Class related keywords:
Object related keywords: 1. new
void return type keyword: If a method won’t return anything compulsory that method should be declared with the void return type in java but it is optional in C++. 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: 1. true ►values for boolean data type. 2. false ►values for boolean data type. 3. null
►default value for object reference.
Enum: This keyword introduced in 1.5v to define a group of named constants Example: enum Beer
Notes: ★ All reserved words in java contain only lowercase alphabet symbols. ★ New keywords are: 1. strictfp 2. assert 3. enum
★ 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.
★ Which of the following are valid java keywords? 1. public (valid) 2. static(valid)
5. String (invalid) 6. args (invalid)
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. ● 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. Diagram:
★ Note: Except Boolean and char all remaining data types are considered as signed data types because we can represent both “+ve” and”-ve” numbers. 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;
►C.E: possible loss of precision
►C.E: possible loss of precision
►C.E: incompatible types
►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.
B. short: ❏ Size: 2 Bytes ❏ Range: -32768 to 32767 (-215 to 215-1) Example: short s=130;
►C.E: possible loss of precision
►C.E: incompatible 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
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: int i=130;
►C.E: possible loss of precision
►C.E: incompatible types
This is most commonly used data type in java.
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. long l=f.length() ;
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.
E. Floating Point Data types: float
If we want to 5 to 6 decimal places of accuracy then we should go for float.
If we want to 14 to 15 decimal places of accuracy then we should go for double.
Range: -1.7e308 to1.7e308.
double follows double precision
float follows single precision.
F. boolean data type:
❏ Size: Not applicable (virtual machine dependent) ❏ Range: Not applicable but allowed values are true or false. Example 1: boolean b=true;
►C.E:cannot find symbol
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: char ch1=97;
►C.E:possible loss of precision
In java we are allowed to use any worldwide alphabets character and java is Unicode based to represent all these characters one byte is not enough compulsory we should go for 2 bytes.
Summary of java primitive data type: Data Type
-27 to -27 -1 (-128 to 127)
-215 to -215 -1 (-32768 to 32767)
-231 to -231 -1 (-2147483648 to -2147483647)