Java NOTE
Java NOTE
JVM
----------
what is scope?
anything within a {} is a particular scope
what is an object
objects are special variables which are like pointers or reference variables which
can store address OF SOME MEMORY BLOCK WHICH LIKES ACCORDING TO THE DEFINITIONS OF
ANY PARTICULAR CLASS.
using that address present in that object / reference variable we can access all
the properties "the member variables & the member methods" of each of those MEMORY
BLOCKS.
## thats why we write static in case of main() method. JATE ALADA KORE OBJECTS NA
BANATE HOI DIRECTLY FILE NAME/ CLASS NAME DIE I main() ke call kora jai.
NOTE AFTER COMPILATION JOTOKOTA CLASS ER DEFINITIONS AMRA BANAI TOTOKOTAI .class
files toiri hoi for each of those classes defined in the main java file before
compilation .
EXAMPLE CODE
class AB
{
public int a;
public int b;
System.out.println(a+b);
}
}
class Mainclassabcd
{
public static void main()
{
AB varr = new AB();
AB varr2 = new AB();
AB varr3 = new AB();
varr.add(1,2);
varr2.add(3,4);
varr3.add(67,89);
}
}
___________________________________________________________________________________
___________________________________________________________________________________
____________
DYNAMIC DISPATCH- INHERITANCE
-----------------------------
Compile-time Type: This is the type declared for the reference variable. In your
case, it's Animal myAnimal.
Runtime Type: This is the actual type of the object the reference variable is
pointing to. In your case, it's new Dog().
Method Invocation:
At compile-time, the compiler checks if the methods called on myAnimal are declared
in the Animal class. It doesn't have information about the actual runtime type of
the object.
At runtime, the JVM uses the actual type of the object (Dog in this case) to
determine which overridden method to invoke.
myAnimal = new Cat(); //in case of changing directly to another subclass
flexibility;
dynamic dispatch refers to the "refernce concept" && method overriding links to the
runtime function call behavior which is a product of DD
Animal myAnimal = new Dog();
___________________________________________________________________________________
___________________________________________________________________________________
______________
METHOD OVERLOADING AND METHOD OVERRIDING - INHERITANCE
------------------------------------------
Method Overloading: In method overloading, multiple methods in the same class have
the same name but different parameter lists (different types or different numbers
of parameters).
Method Overriding: In method overriding, a subclass provides a specific
implementation for a method that is already provided by its superclass. The method
signature (name, return type, and parameter types) in the subclass must be the same
as that in the superclass.
___________________________________________________________________________________
________________________________________________________________________
___________________________________________________________________________________
_____________________________________________________________________
ARRAY
----------
Annonymous array S.O.P(new int[] {1,2,3,4,5}) func(new int[] {1,2,3,4})
int[] var = {1,2,3}//static
int var = new int[10]//dynamic
Arrays.sort(arr)
Arrays.equals(arr1,arr2)
ob == ob2 [checks by address therefor maybe givivng not equal even if same element
having array ]
arr.length
s.length()
int arr[][] = new int[3][5] OR int arr[3][] ; arr[0]= new int[5] arr[1] = new
int[4] arr[2] = new int[6];
___________________________________________________________________________________
____________________________________________________________________
___________________________________________________________________________________
_____________________________________________________________________
STATIC KEYWORD
----------------
within any class we can have both static and non static functions . within the
class we can call the methods just by name but outside the functions we need to
create the objects for non static functions and just call by classname.method() in
case of static methods of that class.
-----------------------------------------------------------------------------------
-----------------------
But within the class if any method is called within any static method then that
called must also be static if used with only name of method , Since we cannot use
this.method() without creating object since methods have to be static or else we
have to create an object of that class itself an then call that method by that
object name
class Use
{
public void fun()
{
HelloWorld.ok();
System.out.println("hello ji");
check();
}
public void check()
{
System.out.println("call me whenever you want me within you");
}
}// within a static method if i call a function or variable even if of the same
class then if we use only function name or variable name then it will be a compile
time error if that method or variable is also not static but thats not the case if
within which it is called is also not static.
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
Use ob = new Use();
ob.fun();
ok();
HelloWorld ob1 = new HelloWorld();
ob1.hi();
}
___________________________________________________________________________________
________________________________________________________________________
________________________________
final keyword: used before any variable to make it constant or any method to stop
it from being overriden or class so that no child of it can be made
finalize() ---- it is a method called before destrying any unused porethaka object.
___________________________________________________________________________________
_________________________________________________________________________
INTERFACES & ABSTRACT CLASSES
-----------------------------------------------------------------------------------
--------
Abstract class in java can't be instantiated. We can use abstract keyword to create
an abstract method, an abstract method doesn't have body. If a class have abstract
methods, then the class should also be abstract using abstract keyword, else it
will not compile
The subclass of abstract class in java must implement all the abstract methods
unless the subclass is also an abstract class
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
_______________________________________________________________
EXCEPTION HANDELING:
there are two types of errors compile time error & run time error:
compile time error = syntax error
try
{
}
catch(typeofExecption e )
{
// jakon nijesso bananop kono logical error define krce erm object banate chai
throw e;
}
finally{
// throws
here e is an object thrown which is of the type of "ANY OF THE PEVIOUSLY MENTIONED
CLASSES" whenever the code inside the try block fails
2.IOException
EndOfFileException
FileNotFoundExecption --- this class is a checked exception throws
FileNotFoundExecption
InterruptedIOException
3.ServeletException
4.RemoteExecption
5.InterruptedException
#checked Execptions- the exceptions checked by the compiler that can possibly occur
at runtime but has not occured yet since code has not run yet . Ex-unreported
exception in file handeling [[[[[[USE OF "throws" KEYWORD AFTER THE FUNCTION
DECLARATION]]]]]
#unchecked - Ex- ArithmeticException etc
FOR USERDEFINED EXCEPTION CLASSES-- the jvm itself cannot throw this type of
object we need to throw it using "throw" keyword
also throw keyword can be used for AE()
ALL CHECKED EXCEPTIONS MUST BE CAUGHT OR DECLARED BEFORE THE FUNCTION
bullets--;
}
System.out.println(
"The firing process is complete");
}
-----------------------------------------------------------------------------------
-------------------------------------
#default priority = 1 to 10 [ FOR MAIN THREAD ITS 5 AND OTHERS INHERIT FROM
PARENT AS DEFAULT but we can also set it ] // we can also set priority of main
thread
-----------------------------------------------------------------------------------
------------------------------------
Demon thread
synchronized keyword for any method or block of code. If multiple threads are
trying to operate on the same java object then this keyword helps to perform
methods of that object one by one by all the threads using it.//when a thread takes
an object as constructor parameter and uses it in its run method.
use of static synchronized usage
synchronized(this){} // if any thread has a lock on this object then only it can
access this area now
synchronized(classname.class){}
___________________________________________________________________________________
_____________________________________________
________________________________________________________
WRAPPER CLASSES-- autoboxing
all these are child classes of wrapper class
classes used to convert primitive type to object type
Integer() //static methods -- .tostring(), .valueOf(99) parseX("string",int
radix--which will be converetd to that Xclass)
Double()
// object methods .byteValue(), .shortValue() etc
Integer i = new Integer(-8);
}
fun(1,2,3,5)
___________________________________________________________________________________
_____________________________________________
________________________________________________________
LAMBDAS
to instantiate any object with direct class defination implementing any interface
having only one method
interfacename ob = new interfacename(){write defination of method as normal
definition}
OR
ineterfacename ob = (method parameters)->{method definition as just statements of
code }
}
class Ourownclass
{
p v method(AL<?> a)// when we know which collection is it but not its type of
things inside it or else AL<String> // here also extends/supper possible
{
return "ok";
}
}
Ourownclass<Integer> ob = new Ourownclass<Integer>()
Ourownclass<Integer,String> ob = new Ourownclass<Integer,String>()
AL<? or ? extends Number> ob = new AL<Integer>()
//Yes, it is valid <Object> but not purpose serving
A class present in java.util Streams which is provides some functions for stream
objects . This object has no such represenation like Lists its used to directly
invoke some functions to perform iterartive abilities by converting List objects
to stream objects
//inner class
//length
//
//String x = "Suvadip" x.length()
// x ={1,2,3,4} x.length
/* for(i=0;i<=x,length();i++)
{
if(x[i]=='e')
{
print ok;
}
}*/
/*
* class CarMaking
* {
*
* class Tyre
* {
*
*
* }
*
*
* }
*
*
*/