a. Package declaration
b. Import statements
c. Class/Interface definitions
2. Importing packages doesn’t recursively import sub-packages.
3. Java language automatically imports all classes and interfaces in the java.lang package.
The compiler treats each source file as if its first import statement were
import java.lang.*; // implicit in every Java source file
4. Sub-packages are really different packages, happen to live within an enclosing package. Classes in sub-packages cannot access classes in enclosing package with default access.
5. There can be an ambiguous situation if two packages happen to have classes with the same name and you don’t explicitly provide a fully qualified name.
import java.sql.*;
import java.util.*;
…..
System.out.println(new Date()); //ERROR
System.out.println(new java.util.Date()); //OK
…..
6. Comments can appear anywhere. Can’t be nested.(No matter what type of comments). There are three types of comments in Java.
(a) Single Line //
(b) Multi Line /* …. */
(c) Documentation Comment /** ……. **/
· If // appears, everything will be commented out after it, in the same line
· If /* or /** appears, it will comment all the content till the first encoutered */
Examples
/* This is a comment */ [OK]
/** This is a comment */*/ [ERROR]
// /* This is a comment */ */ [OK]
/* // This is a comment */ [OK]
7. At most one public class definition per file. This class name should match the file name. If there are more than one public class definitions, compiler will accept the class with the file’s name and give an error at the line where the other class is defined.
8. Even an empty file is a valid source file. It can be compiled but can’t run.
9. An identifier must begin with a letter, dollar sign ($) or underscore (_). Subsequent characters may be letters, $, _ or digits.
10. An identifier cannot have a name of a Java keyword. Embedded keywords are OK. true, false and null are literals (not keywords), but they can’t be used as identifiers as well.
11. const and goto are reserved words, and can’t be used as identifiers. You must differentiate between keywords, reserved words and literals.
12. Unicode characters can appear anywhere in the source code. The following code is valid.
ch\u0061r a = 'a';
char \u0062 = 'b';
char c = '\u0063';
12. Java has 8 primitive data types.
boolean
byte
short
char
int
long
float
Double
13. All numeric data types are signed. char is the only unsigned integral type.
14. Object reference variables are initialized to null.
public class TestProgram{
static String s1;
static String s2;
public static void main(String args[]){
s2=s1+s2;
System.out.println(s2);
System.out.println(s1);
}
}
Output : nullnull
null
15. Octal literals begin with zero. Hex literals begin with 0X or 0x.
16. Char literals are single quoted characters or unicode values (begin with \u).
17. A number is by default an int literal, a decimal number is by default a double literal.
18. 1E-5d is a valid double literal, E2d is not (since it starts with a letter, compiler thinks that it’s an identifier)
19. Two types of variables.
Member variables
· Accessible anywhere in the class.
· Automatically initialized before invoking any constructor.
· Static variables are initialized at class load time.
· Can have the same name as the class.
Automatic variables (method local)
· Must be initialized explicitly. (Or, compiler will catch it.) Object references can be initialized to null to make the compiler happy. Local variables can’t have any accessibility modifier, also can’t be marked static, volatile, transient, but can be final. The following code won’t compile. Specify else part or initialize the local variable explicitly.
public String testMethod ( int a) {
String tmp;
if ( a > 0 ) tmp = “Positive”;
return tmp;
}
· Can have the same name as a member variable, resolution is based on scope, in such a case “this” keyword is used.
Member Variables
Local Variables/Automatic Variables
Created when an instance is created and are destroyed when the object is destroyed (or garbage collected)
Thus, accessible anywhere within the class.
Created on entry to the method or initialization block, exists only during the execution of the method or block
Accessible only inside the method where defined.
All member variables that are not explicitly assigned a vales upon declaration are automatically assigned initial value
Local variables are not implicitly assigned value. This can cause a compile time error if we try to access an un-initialized local variable.
.Can have any accessibility modifier (public, private, protected) and other modifiers like transient, final, static and volatile
Can’t have any accessibility modifier.
Can be final.
Member variables can further be classified in two categories – (1) Instance variables, and (2) static or class variables.
Note that array types whether member or local are implicitly initialized with default values when no explicit assignment has been made.
20. Arrays are Java objects. If you create an array of 5 Strings, there will be 6 objects created. Since arrays are implemented as Objects in Java, Arrays in Java implicitly extend from the Object class. This means that you can call any method of the Object class from an Array reference.
int i[]=new int[2];
int j[]=new int[2];
System.out.println(i.equals(j)); //false
System.out.println(i.toString()); //prints hash code
21. Arrays should be
22. Declared. (int[] a; String b[]; Object []c; Size should not be specified now)
23. Allocated (constructed). ( a = new int[10]; c = new String[arraysize] )
24. Initialized. for (int i = 0; i < a.length; a[i++] = 0)
25. The above three can be done in one step.
int a[] = { 1, 2, 3 }; (or )
int a[] = new int[] { 1, 2, 3 }; But never specify the size with the new statement.
23. Java arrays are static arrays. Size has to be specified at compile time. array_ref.length returns array’s size. (Use Vectors for dynamic purposes).
Since, Java arrays are dynamically created at run time, we can specify variable rather than a literal to specify the size of the array.
24. Array size is never specified with the reference variable, it is always maintained with the array object. It is maintained in array.length, which is a final instance variable.
25. Anonymous arrays can be created and used like this: new int[] {1,2,3} or new int[10]
Example
public class TestProgram {
static int[] method(){
return new int[]{34,90,12,89,10,90};
}
static int min(int arr[]){
int least=arr[0];
for(int i=1;i
return least;
}
public static void main(String args[]){
System.out.println("smallest element "+min(method()));
}
}
26. Arrays with zero elements can be created. args array to the main method will be a zero element array if no command parameters are specified. In this case args.length is 0. There is a strong bounds checking on Java Arrays, For example, if you try to access (e.g. System.out.println(args[0])), and if no command line arguments are provided then an ArrayIndexOutOfBoundsException will be thrown.
27. Comma after the last initializer in array declaration is ignored.
int[] i = new int[2] { 5, 10}; // Wrong
int i[5] = { 1, 2, 3, 4, 5}; // Wrong
int[] i[] = {{}, new int[] {} }; // Correct
int[] i[] = {null, new int[] {} }; // Correct
int i[][] = { {1,2}, new int[2] }; // Correct
int i[] = { 1, 2, 3, 4, } ; // Correct
· When constructing multidimensional arrays with the new operator, the length of the deeply nested array may be ignored.
int i[][]=new int[4][]; //OK
int i[][]=new int[][4]; //Wrong
int i[][]=new int[4][4]; //OK
Note,
int[] i[] = {null, new int[] {2,3} };
System.out.println(i[1]); //calls the toString() method, prints hash code
System.out.println(i[0]); //this will print null
for(int j=0;j
for(int k=0;k System.out.println(i[j][k]);
}
Output: [I@df6ccd
null
Exception in thread "main" java.lang.NullPointerException
28. Array indexes start with 0. Index is an int data type.
29. Square brackets can come after datatype or before/after variable name. White spaces are fine. Compiler just ignores them.
Example :
int[] a,b; // here both a and b are arrays of type “int”
int a[],b; //only a ia an “int” array, b is an “int” variable
When the [] notation follows the type, all the variables in the declaration are arrays
No comments:
Post a Comment