Java Notes Language
Java Notes Language
Foreach:
The keyword this can be used for unmasking a variable of current class by others in a
method(or constructor) and can be used to call other constructor in the same class
o Must be used as first statement
The finalize( ) Method can be defined inside any class : executed before an object is claimed
during garbage collection
Methods and constructors can be overloaded
Passing object to a method = call by reference
Objects can be returned
Recursion
Any static member can be accessed before any object of the class is declared
All instances of a class share same static variable, no copies of static variables are created
Access protection:
Chained exceptions:
o @Inherited can be used only on classes and it will be inherited to its subclasses.
o @Override can be used only on methods, further this method must be overriding a
superclass method.
o @SuppressWarnings specifies that one or more warnings that might be issued by
the compiler are to be suppressed. The warnings to suppress are specified by name,
in string form. This annotation can beapplied to any type of declaration.
Some restrictions
o Annotation cannot inherit other.
o Methods in annotations cannot have body and must return one of the following
A primitive type, such asintordouble
An object of type String or Class
An enum type
Another annotation type
An array of one of the preceding type
o annotation methods cannot specify a throws clause.
o Annotations cannot be generic.
File handling
Reading from console
o BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
o int i=br.read( ) throws IOException;
o String br.readLine();
To write to console instead of println, write can be used , e.g., …write(int bytevalue);
To read a file
o FileInputStream fin = new FileInputStream(filename);
o Int I =fin.read();
o Fin.close();
To write to a file
o FileOutputStream fout = new FileOutputStream(filename); int I;
o Fout.write(i);
o Fout.close();
Other topics
Transient modifier: When an instance variable is declared astransient, then its value need
not persist when an object is stored. For example:
class T {transient int a; // will not persist
int b; // will persist }
Volatile modifier: instance variable modified by volatile will be constantly updated . (in
multithread programs each thread may keep a copy of original variable).
, object instanceof typ - will return true if object is of type typ or can be casted.
Stricfp can be used as modifier for class or method to increase the precision of calculations.
Assert has two forms
o , assert condition;
o , assert condition:expression;
o if the condition in assertion fail, an exception is thrown, e.g.,
….
assert n > 0 : "n is negative!";
….
Exception in thread "main" java.lang.AssertionError: n is
negative!
To run program with assertion: java –ea filename
To enable and disable assertion in a package
-ea:MyPack
-da:MyPack
Static import : if a method is imported as static, it can be used without qualification by its
class name.
Multithread programming
Main thread should be the last to terminate.
Current thread can be obtained by function currentThread (static function): Thread t=
Thread,currentThread();
…Println(t);This displays, in order: the name of the thread, its priority, and the name of its
group. By default, the name of the main thread is main. Its priority is 5, which is the default
value, and mainis also the name of the group of threads to which this thread belongs.
Athread groupis a data structure that controls the state of a collection of threads as a whole.
e.g., [main,5,main]
Java’s multithreading system is built upon the Thread class (can be extended), its methods,
and its companion interface Runnable (can be implemented and must implement its method
run()).
One of the method for Thread class : Thread(RunnablethreadOb, StringthreadName)
For implementation of runnable: Thread t;
NewThread() {t = new Thread(this, "Demo Thread"); … }
For Thread extended class: super(class name.)
Few important methods in Thread class