Java is a general-purpose computer-programming language. It is intended to let application developers "write once, run anywhere" (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. The latest version is Java 10, released on March 20, 2018.The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them.
Sun Microsystems released the first public implementation as Java 1.0 in 1995.James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. Java was originally designed for interactive television, but it was too advanced for the digital cable television industry at the time. The language was initially called Oak after an oak tree that stood outside Gosling's office. Later the project went by the name Green and was finally renamed Java, from Java coffee. Gosling designed Java with a C/C++-style syntax that system and application programmers would find familiar.
So Now Coming to JAVA Notes to Remember ✌:-
* Size of primitive can not be changed
* variable is a named memory location
* parameters are show(int x)
arguments are show(10)
* c -> function
java -> method
* Calling in java of function s.add() or ObjectName.FunctionName()
* return statement can be used in void datatype but will not return any value or expression
* ternary expression (x>0)?1:-1 or (expression)?value1:value2
* ; -> line terminator
* control statements control the flow of execution of the program
(like itteration , conditional , jump)
* Switch works only on constants so no condition can be check in it
* there is no global variable in java
* platform refers to OS
* JAVA was developed by James Gosling and his team in SUN Microsystem
* WORA -> Write once run anywhere
* in 1991 firstly introduced and named OAK then in 1995 it was renamed as JAVA
* Every OS has its own JVM which make JAVA a platform independent
* There is no pointer in JAVA
* JAVA is portable,secure,architecture neutral(i.e datasize doesn't vary with OS),distriuted,networking
* RMI -> Remote Model Invocation
* Robust -> Compatible
* JVM collects garbage
1- System.gc();
2- finalize();
a) resource check (i.e is permanent or not)
b) free
c) mem.free
* Encapsulation ->
-HOW?
-wrapping up of data with methods
OR
-wrapping up of data elements/members into one unit
-unit is called class
* Multiple inheritence is not supported by JAVA
* Abstraction ->
-WHAT?
-data hiding
-showing relevant information only
* float 4bytes
double 8bytes
* should -> convention
have to -> rule
* if a class is public then the file should be saved as a public class name
* Class ->
-A class is a blueprint of an object.
-HOW? WHAT?
-A class is a logical entity
-A class is a user defined entity
* A object is a physical entity
* Instance Variable -> No. of copies created of instances variables is equal to the number of objects created.
this are stored as object.
* Static Variable -> Only single copy is created of static variable and all the objects share the same copy
* the class level variables are defined ton there default values if not provided
* JVM M/M management
-heap -> object of any class
-non-heap -> a)Stack - methods
b)Static global area pool - Static Variables
c)Sting Constant/literals pool - String Literals
* A block with no name is instance block , it is executed at object creation time , order of execution depends on the order of there appearences in the class
* Static block gets executed first and only once
* Instance or static members can be invoked from instance block
* only static members can be invoked from static block
* it is not necessary to create a object of class to access its static members
* The static members of the class can be accessed directly with class name with dot(.) operstor
* A non static member of a class can not be referred Static context
* Static does not belong to object it belongs to class
* Instance belong to object
* non-static can refer to static as well as non-static
* static can refer to static and can not refer to non-static
* for static
-ClassName.MemberName
-MemberName can be variable or method
* what is static and String ar[] in public static void main(String ar[]) and why string is used ?
why Static
-JVM does not needs to create an object of the class to access the main method.It acess the method directly with class name
why String ar[]
-to hold command line arguments (CLA)
why String
-String can hold any other literals
* Arrays are treated as object in java
* Declaration of array in java
1- int a[],b[];
2- int []a,b;
* Initialization of array
1- a = { 4,3,9,10};
2- b = new int[]{4,10,22};
3- c = new int[2];
c[0] = 40;
c[1] = 12;
c[2] = 15;
* for each or enhanced for loop
for( datatype varname : Collection c )
{
}
example-
for( int j : m)
{
SOP(j);
}
-in above example m is an array
-there is no increament,decreament,reverse
-only forward and by one
-datatype should be comfortable
example-
double : int (correct)
int : double (wrong)
* Varname is treated as varname[]
* "this" keyword refers to the current invoking object. It is also used to distinguish among variables having same name
* Method Overloading ->
-More than one form of methods
-it depends upon
a)no. of parameters
b)type of parameters
c)order of parameters
-it does not depend upon the return datatype of method
-also known as static binding or early binding or compile time polymorphism
-also possible in super class or sub class relationship (Inheritence)
* Constructor ->
-This is a special method having same name as of class (case sensitive) with no return type
-Recurrsive constructor invocation is not possible
-they are never static and never inherited
-compiler provides a default compiler if not declared which has blank body
-if the compiler is declared as parameterised then the compiler will not give the default or non-paramaterised constructor and it can not be invoked
-use of this keyword in constructor invokes the constuctor of same class with the given parameter
-"this()" or "this(<--->) must be the first statement of the constuctor
* Inheritence ->
-Used for the concept of code reusability
-class of which codeis used is known as base class/parent class or super class
-class in which the code of super class is used is derived class/child class or sub class
-"extends" keyword is used for inheriting a class in a class
-All the properties (methods or variables) of the super class is inherited in a sub class except private.
the private members of the class can be accessed through its own class members only
-A class can extend a single class only
-Java support multi-level or linear inheritence
-It does not support multiple inheritence
-Generally , a object of sub class is created
-when a class contains a reference of another class the relationship is known as "HAS A" relation
-"extends" keyword defines "IS A" relation
-Object class is the super class by default of all the classes
-If you define a parameterized constructor in a super class then you have to pass values for the parameters of constructor of super class from the
constructor of sub class using keyword "super" or you have to define a default constructor in a super class
* Method Overriding ->
-the redefination of methods of super class in sub class
-inheritence is mandotary among super and sub class
-number , type & order of parameter must be same
-Return type must also be same
-Access specifier must be same or higher
* Run time polymorphism or Dynamic method dispatch ->
-At compile time it is checked whether the invoked method is present in super class or not and at run time it is decided which method is to be invoked
depending upon the object created for the class
* Abstract Method and Class ->
-What?
-Not How?
-Abstract method tells what to do , not how to do
-they dont have body and are declared using keyword "abstract"
Syntax :-
abstract return_type MethodName(<parameters>)
-if a class contains even a single abstract method it must be declared as abstract.
-It is not necessary that abstract class contains abstract method
-may contain abstract method or instance method or both
-in order to use the abstract methods of a class , it must be inherited and body must be defined for abstract methods
-an abstract class can not be instantiated
-no object is created of abstract class
-if we dont define all the abstract methods of a class then the implementation is known as partial implementation
* Interface ->
-it is a collection of abstract , default and static methods
-keyword "interface"
-by default interface variables are public , static & final
-by default the non-static & non default methods of interface are public & abstract
-keyword "implements" is used for implementing an interface in a class
-class can implement more than one interfaces
-the static method of a class can be invoked with the object or directly with class name
-the static method of a interface can be accessed only with the name of interface
-a class can extend a single class only but can implement multiple interfaces
-a interface can extend multiple interfaces but it can not implement an integer
* String class ->
-string objects are immutable
-string class is final
Some string functions/methods
1- public char charAt(int);
2- public string concat(String);
3- public string replace(String old , String new);
4- public string toUpperCase();
5- public string toLowerCase();
6- public string substring(int start);
7- public string substring(int start , int end);
8- public string trim();
10- public int length();
11- public int indexOf(String);
12- public int lastindexOf(String);
13- public boolean equals(string);
14- public boolean equalsIgnoreCase(String);
15- public char[] to charArray();
16- public static string.valueOf(int);
17- public static string.valueOf(double);
-if value is not found then index and last_index_of returns -1 value
-trim removes the spaces before and after first and last character/number/data
e.g-
SOP(" A B ".trim().length());
output : 6
SOP(" A B ".length());
output : 13
-String.valueOf converts the value into string
Question) Difference b/w "equals" and "==" ?
Answer) "==" checks whether both point to the same address or not whereas "equals" checks whether they have same content or not
* String Buffer - synchronized
String Builder
* Package ->
- it is simply a folder or directory
- it is a collection of classes
- the class file belonging to a package must contain the package name as its first executable statement
- the class file of the package must be declared as public
- the methods must also be declared as public so that they can be accessed from outside the package
- main method is declared as public so that it can be accessed by JVM from outside the folder
- Public class must be save with same name as of class(case sensitive)
- A file can have a single public class only
- "import" statement is used for importing package class file in a class
- multiple import statements can be placed in a file and that must be placed before begining of the class
- JIT loads the class at the run time
- when a class name is mentioned with package name it is known as fully qualified type name
" pack.calc.Add "
"import pack.*" // calls all the file present in that folder except any other packages
"import pack.calc.Calculator" // calls the single file Calculator which is present in package pack
- the protected members of the class can be accessed in same package or sub class of different package only
- " java.lang " package is default package of all the classes
" import static java.lang.Math.* "
* javac test1/Test.java -d test2
for creating the class file of Test.java stored in test1 folder in test2 folder
-d = destination
* javac -cp ;test2 CPTesting.java
cp = class path
; = contents of current folder
test2 is folder in which class file is stored of the files/classes which are being used in CPTesting.java
for run :-
java -cp ;test2 CPTesting
* Exception handling ->
- any condition due to which the execution of a block (or method) terminates abnormally
- the following keywords are used as exception handling
1)try :
the risky code is placed in try block which may generate an exception
SYNTAX :-
try
{
//code
}
or
try(resource)
{
//code
}
2)catch :
exception is handled in catch block
also known as exception handler
JVM is default exception handler
SYNTAX :-
catch(ExceptionName e)
{
//code
}
or
catch(Exception1|Exception2|Eception3 e) .....(ii)
{
//code
}
for (ii) case the given exception must not be related with super and sub class relationship(inheritence) with each other
3)finally :
the code which will execute either exception occurs or not
SYNTAX :-
finally
{
//code
}
4)throws :
tells the computer that the method may create/generate/throw an exception of given types
5)throw :
to explicitly generate an exception
SYNTAX :-
throw new ExceptionName();
- Checked Exceptions
those exceptions for which compiler checks that either you have handled the exception ( use of try-catch)
or declared to be thrown ( use of thrown )
- Unchecked Exceptions
Compiler does not perform any type of checking related to exception
- " public String getMessage() "
returns the message part (actual error) associate with the exception
- " public StackTrace printStackTrace() "
returns the information from where the exception has been generated and which method has invoked
- " public String toString() "
it represents the object in string form
- with " try " either " catch " or " finally " one is mandotory
- Exception is handled by JVM if programmer doesn't handle it
- If JVM handles the exception then the programs terminates at the exception line and come out of the block
- Any executable statement can not be placed between these blocks i.e try-catch & try-finally
- a try block can have multiple catch blocks but the hierachy of catch block must be from sub-class to super-class(top to bottom)
- IN THE CASE OF CHECKED EXCEPTIONS ONLY
If overridden method is declaring an exception to be thrown then the overriding method may declare same exception or sub
class of it or may not declare an exception to be thrown.
the overriding method can not move up in the hierachy and also it can not declare a new exception to be thrown
* Multi Threading ->
-A thread is a smallest executable/dispatchable unit of a process
-Each thread has its own stack & has its own path of execution
-By default threads are not synchronized in java
-Selfish Thread :
Does not release the CPU implicitily
-Forever Thread :
Always in execution mode never stops until any hardware failure
-There are two ways for creating a thread in java :-
1) By implementing runnable interface
2) By extending thread class
public Thread (Runnable r)
public Thread (Runnable r,String name)
public void setName(String)
public String getName()
public int getPriority()
public void setPriority(int) :
unreliable method doesn't assure if we set high priority of set it will execute first
Range :- 1 to 10
public static final MIN_PRIORITY=1
public static final MAX_PRIORITY=10
public static final NORM_PRIORITY=5
public ThreadGroup getThreadGroup()
public static Thread currentThread() :
returns the reference of currently executed thread
public static void sleep(int ms) throws InterruptedException :
Pauses the thread for given time period
public static void join() throws InterruptedException :
doesn't releases the CPU until & unless it has completed its task
public void wait() throws InterruptedException :
pauses threads until it gets notification
public final void notify() :
notifies one of the waiting threads
public final void notifyAll() :
notifies all of the waiting threads
-start() :
pushes the thread into runnable state after getting cpu time threads searches for run method
-In thread synchronization the objects are lock . it always hits the performance and a deadlock condition may arise
-by using "synchronize" keyword we can make a method synchronized
-Inter Thread Communication :
when one thread completes its task it notifies another thread these methods are of object class
* program in execution is known as process
* final
variable : value can not be altered
method : can not be override
class : can not be inherited
* cd.. : to back one directory
* " .\pack\add.java "
. = current folder
\ = path seprator
* main method is invoked by JVM through OS
* JIT - Just in time compiler
Comments
Post a Comment