Java Notes
Java Notes
Java Operators:-
Uniary operators:-
++x:Increment first then print
x++:print then increment
x--:print then decrement
--x:decrement then print
ex:-
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}}
public class OperatorExample{
int a=10;
int b=10;
System.out.println(a++ + ++a);//10+12=22
System.out.println(b++ + b++);//10+11=21
}}
int a=10;
int b= -10;
boolean c=true;
boolean d=false;
System.out.println(!d);//true
}}
The Java left shift operator << is used to shift all of the bits in a value to
the left side of a specified number of times.
for ex:-
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(20<<2);//20*2^2=20*4=80
System.out.println(15<<4);//15*2^4=15*16=240
}}
The Java right shift operator >> is used to move the value of the left
operand to right by the number of bits specified by the right operand.
public OperatorExample{
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
}}
System.out.println(20>>2);
System.out.println(20>>>2);
System.out.println(-20>>2);
System.out.println(-20>>>2);
}}
The logical && operator doesn't check the second condition if the first
condition is false. It checks the second condition only if the first one is
true.
ex:-
int a=10;
int b=5;
int c=20;
}}
The logical || operator doesn't check the second condition if the first
condition is true. It checks the second condition only if the first one is
false.
int a=10;
int b=5;
int c=20;
//|| vs |
}}
It is used in Servlet and JSP where the container creates a thread pool
to process the request.
Remaining Topics:-
Thread Group
Shutdownhook
1) By nulling a reference:
e=null;
3) By anonymous object:
new Employee();
finalize() method
The finalize() method is invoked each time before the object is garbage
collected. This method can be used to perform cleanup processing. This
method is defined in Object class as:
The Garbage collector of JVM collects only those objects that are
created by new keyword. So if you have created any object without
new, you can use finalize method to perform cleanup processing
(destroying remaining objects).
gc() method
Remaining Topics
Java synchronization
Java uses the concept of a stream to make I/O operation fast. The
java.io package contains all the classes required for input and output
operations.
Stream
In Java, 3 streams are created for us automatically. All these streams are
attached with the console.
OutputStream
InputStream
Java application uses an input stream to read data from a source; it may
be a file, an array, peripheral device or socket.
OutputStream class
OutputStream class is an abstract class. It is the superclass of all classes
representing an output stream of bytes. An output stream accepts
output bytes and sends them to some sink.
OutputStream Hierarchy
InputStream class
InputStream class is an abstract class. It is the superclass of all classes
representing an input stream of bytes.
public abstract int read()throws IOException reads the next byte of data
from the input stream. It returns -1 at the end of the file.
InputStream Hierarchy
Java FileOutputStream Class
Java FileOutputStream is an output stream used for writing data to a
file.
void write(byte[] ary) It is used to write ary.length bytes from the byte
array to the file output stream.
void write(byte[] ary, int off, int len) It is used to write len bytes from
the byte array starting at offset off to the file output stream.
void write(int b) It is used to write the specified byte to the file output
stream.
int read() It is used to read the byte of data from the input stream.
int read(byte[] b, int off, int len) It is used to read up to len bytes of
data from the input stream.
long skip(long x) It is used to skip over and discards x bytes of data from
the input stream.
void write(byte[] b, int off, int len) It write the bytes from the
specified byte-input stream into a specified byte array, starting with the
given offset
When the bytes from the stream are skipped or read, the internal buffer
automatically refilled from the contained input stream, many bytes at a
time.
int read() It read the next byte of data from the input stream.
int read(byte[] b, int off, int ln) It read the bytes from the specified
byte-input stream into a specified byte array, starting with the given
offset.
void close() It closes the input stream and releases any of the
system resources associated with the stream.
long skip(long x) It skips over and discards x bytes of data from the input
stream.
remaining topics
sequenceinputstream
bytearrayoutputstream
bytearrayinputstream
Remaining Io packagen startting generics
Generics in Java
The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects. It makes
the code stable by detecting the bugs at compile time.
3) Compile-Time Checking: It is checked at compile time so problem will not occur at runtime.
The good programming strategy says it is far better to handle the problem at compile time than
runtime.
Generic class
A class that can refer to any type is known as a generic class. Here, we are using the T
type parameter to create the generic class of specific type.
Collections in Java
The Collection in Java is a framework that provides an architecture to store and manipulate the
group of objects.
Java Collections can achieve all the operations that you perform on a data such as searching,
sorting, insertion, manipulation, and deletion.
The Collection framework represents a unified architecture for storing and manipulating
a group of objects. It has:
Iterator interface
Iterator interface provides the facility of iterating the elements in a forward direction only.
Iterable Interface
The Iterable interface is the root interface for all the collection classes. The Collection
interface extends the Iterable interface and therefore all the subclasses of Collection
interface also implement the Iterable interface.
Collection Interface
The Collection interface is the interface which is implemented by all the classes in the
collection framework. It declares the methods that every collection will have. In other
words, we can say that the Collection interface builds the foundation on which the
collection framework depends.