Java Core
Java Core
Java Core
Its a way of setting value for an object,ex = Al3 obj = new Al3(3,9);
we just gav parameters
*****FOR ATT/METHODS
*****FOR CLASS
***************PACKAGES
***************INNER CLASSES
WE CAN HAVE CLASS INSIDE OF A CLASS.
int x = 10;
int y = 5; }
System.out.println(myInner.y + myOuter.x); }}
TYPES;
1)Private inner class=If you try to access a private inner class from an outside class, an error occurs
2)Static inner class=An inner class can also be static, which means that you can access it without creating an object of the outer class
************INTERFACE
Its a static class i.e it holds all methods without body(as they all are
static from inside)
interface Animal {
enum Level {
switch(myVar) {
}}}
enum moods{
Main(){
********************TIME IN JAVA
Import java.time.*; // package
}}
Other classes;
******************ARRAYLIST
We can change its size later as well after defining unlike an array.
System.out.println(i);
************************LINKEDLISTS
Both are same but linked is used to manipulate data and arraylist is
used to store data
********************HASHMAPS
Int it things go upside down with a logic similar to 2D array. But with key/value concept
capitalCities.put("England", "London");capitalCities.get("England");capitalCities.remove("England");
capitalCities.clear(); capitalCities.size();
******************HASHSETS
import java.util.HashSet; // Import the HashSet class
System.out.println(i); }
**************************ITERATOR
An Iterator is an object that can be used to loop through collections
Note: Trying to remove items using a for loop or a for-each loop would not work correctly because the collection is changing size at
the same time that the code is trying to loop
cars.add("Volvo");cars.add("BMW");cars.add("Ford");cars.add("Mazda");
Iterator<Integer> it = numbers.iterator();
while(it.hasNext()) { i = it.next();
System.out.println(numbers);
*****************FILE HANDLING
CREARTING A FILE
WRITING TO A FILE
myWriter.close();
try {
while (myReader.hasNextLine()) {
System.out.println(data);
myReader.close();
if (myObj.delete()) {
Say if a class N extends M and they have same method ONO() where
their body is different. So an obj of N with keyword super will
implement method body in M.
Void namemethod () ; }
BELOW SHOWS THAT WE NEED TO USE THE THREAD OBJ TO INITIALIZE A THREAD
}
main(){
MyThread thread1 = new MyThread(name "Thread1 "); thread1.t.start();
MyThread thread2 = new MyThread(name "Thread2"); thread2.t.start();
MyThread thread3 = new MyThread(name "Thread3"); thread3.t.start();
}
Synchronized Threads
THREADS THEORY
public static void main(String[] args){ MyThread newobj = new MyThread(); newobj.start(); }
public static void main (String[] args){ Thread t = new Thread(new MyThread()); t.start();}
MUTLITHREADING –
class Printer{
Printer pRef;
MyThread(Printer p){
pRef =p;
}
@Override
public void run() {
pRef.printDocument(10, "Alice");
}
}
class YourThread extends Thread{
Printer pRef;
YourThread(Printer p){
pRef =p;
}
@Override
public void run() {
synchronized(pRef) {
pRef.printDocument(10, "Chris");
}
}
Main()=
System.out.println("START");
So we made a class called Printer and gave it a method call printDocument. Then we
made two child classes of class Thread(i.e basically thread classes) and filled
them with constructors that take in a Printer object as arg.For each thread class
we made a Printer object that takes the given parameter to the constructor then
implies that in the run() method.In run() method we are impling the printDocument
method using this given parameter.
Now in main we made a Printer object and objects of other two classes.While making
so we gave in the Printer object as the parameter for the constructor of those two
classes. Then we implemented start() method for those two objects of thread class
to make them run but we got jumbled output so we used something called join()
method // NOTE; use try catch for join(),sleep() and so on // This will help in
synchronization or else go to the Printer class and put ‘ synchronized ‘ that will
do same.
THEAD POOL –
LAMBA EXPRESSION IN JAVA
FUNTIONALAL INTERFACE
MAIN=
LAMBA PARAMETERS
interface cab2 {
double bookCab(String source,String destination);
MAIN=
STATIC
OUTSIDE MAIN =
interface calculator{
void add (int a ,int b);
}
class calc{
public static void addsomething(int a,int b){
System.out.println(a+b);
}
interface Messanger{
Message getMessage(String mgs);
}
class Message{
Message(String msg){
System.out.println(msg);
}
}
main=
//Ref to a constructor
Messanger mref = Message::new; //Method ref
mref.getMessage("sdf");
PARSING XML USING DOM,SAX AND StAX PARSER IN JAVA
XML =
In above we made a file obj first giving the path inside the
constructor. Then we made a method cuz that’s a good
programming practise and also bcuz of that we can use that method
next time for other file obj as well. Now in method we supplied the
file object as parameter as we wonna work with that and we wrote
a try catch thing cuz this process gives errors by def in java. Now
we made an obj of PrintWriter out of try catch as we would have to
use it in finally as well(since we wonna close the file ). In try we
used this obj and passed the method parameter into the PrintWriter
constructor(which is gonna work on the original file we wonna workl
with). El fin.
This is reading from a file.We use scanner.
Part 1 expl
Part 2 explanation
class generic<T1,T2>{
int val;
T1 a;
T2 b;
generic(T1 a,T2 b){
this.a=a;
this.b=b;
System.out.println(a +" " + b);
}
MAIN =
2) explicit;
EX double a 34.34; int b (int) a; means double got converted into int
TYPE WRAPPERS
AUTOBOXING
ENUMS
Its a special type of class that lets u make a new data type and
then you can decide what data that data type will take(we don’t
have to deal with new keyword) enums can inherit and extends.
enum Days{
Monday, Tuedays, WED // they are static and final and constant by default}
MAIN =
2] xml tags are case sensitive 3] close tag and open tag bt exist.
//RECURSION
// ITS A METHOD THAT CALLS ITSELF
/* PUBLIC STATIC LONG FACT(INT A) {
IF(A<=1)
RETURN 1;
ELSE RETURN A*FACT(A-1);
}
COLLECTION ;
ITERATOR
editlist(la,lb);
LINKED LIST
// METHOD addAll()
// adds one collection data into other collection
// this is ognan add elements of stuff to list2
//Collections.addAll(list2, stuff);
//FREQUENCY
//frequency gives output of how many times an element appear in
list
// Collections.frequency(list, "FI");
// DISJOINT
// boolean t = Collections.disjoint(list, lst);
// True is no items in common
// False if items in common
*/
STACKS
q.offer("sd");
q.offer("asd");
q.offer("gd");
System.out.printf("%s", q); // prints whole queue at once
// 1) HashSet
String[] stuff = {"a","s","a"};
List<String> li = Arrays.asList(stuff); // we made array into list n got it
System.out.printf("%s", li); // print
RANDOM CLASS
MAIN=
HASHMAPS = KEY/DATA