Java
Java
Ex : windows + Intel,
Linux + atom.
Java process – java compiler produced from source code to byte code,
but machine can read only machine code, that’s why java developers
developed by one virtual machine that machine called JVM(Java
Virtual Machine).
When you installed jdk(java development kit) that includes java virtual
machine
Editor – To type your program into a notepad or IDE (eclipse, net beans)
Compiler – To convert your high level language program into native
machine code
Linker – To combine different program files reference in your main
program together
Loader – to load the files from your secondary storage device like HDD,
CD, and Flash drive into RAM for execution, the loading is automatically
done when you execute your code.
Execution – Actual execution of the code, which is handled by your OS
and Processor with this background.
Types of languages:
/* public - Access specifies , Static - Access modifier (equally sharing memory for main),
Void – return type, does not return anything for main, Main – first compiler search and
start with main method, String args[] – storing array, this is no appear nothing print
in console. */
System.out.println(“ Helloworld”);
/* System – Source file, Out – variable, Println – printing the given line, “ Helloworld”
– storing array and saving string datatype */
}
SCION RESEARCH AND DEVELOPMENT Sign in Shine out
Creating own method: (User defined method)
18
void print()
System.out.println(“ Helloworld”);
void print() {
System.out.println(“ Helloworld”);
void print() {
System.out.println(“ Helloworld”);
h.print();
/* Helloworld- Class name, h – object, new – is a keyword, allocate memory for object at
runtime, Hellworld – constructor */
Another way
public class Helloworld {
void print(int a, int b) {
int c = a + b;
System.out.println(“ sum”+ “ “ + c);
}
}
public static void main(String args[]) {
Helloworld h = new Helloworld();
h.print(10,20);
}
Public void showdemo() ad.x=50; // private variable can’t be accessed (not visible)
Class accessex {
-instance of the class or objects sharing the memory System.out.println(“roll no” + “ “ +”name”+ “ “ +
int roll no; // instance or global variable Public static void main(String args[])
String name; // instance or global variable // college = “SASTRA”; /*re assign possible in the
} }
void display() { }
variable – do not reassign, do not change the value System.out.println(“roll no” + “ “ +”name”+”
Public class fin{ // college = “SASTRA”; /*does not support reassign the
Int roll no; // instance or global variable final variable, static reference only support static*/
final string college = “ PREC”; // instance or global final fin s2 = new fin(222, “ajay”);
variable s1.display();
int a =0;
}
Conditional stmt.docx
{ }
if(a<=18) }
{ }
system.out.println(“he is a adult”);
else
if – if(10===10)
{ system.out.println(“\n”);
{ }
int x =10; }
while (x<20) {
system.out.println(“value of x “ + x);
{ }
int x =10;
system.out.println(“value of x “ + x);
x++;
system.out.println(“\n”);
Usage:
void display() }
{ }
System.out.println(speed);
class person { }
System.out.println(“welcome”); {
} s1.display();
void display() { }
System.out.println(“welcome”); }
} {
System.out.println(“welcome to java”); }
} }
void display() {
method */
{ }
Bike() {
class defaultcons {
defaultcons() {
System.out.println(“Bike is created”);
}
public static void main(String args[])
defaultcons b = new defaultcons();
}
}
void display() { }
System.out.println(id+“ ”+name);
class paramcons { }
id = i ; paramcons s2 = new
name = n; paramcons(222,”armada”);
} s1.display();
System.out.println(id+“ ”+name); }}
types:
single
multilevel
hierarchy
} example:
System.out.println(“programmer
Class A {
}
Class B extends class A {
}
Class C extends class B {
}
Class D extends class C {
}
} p.bonus);
} }
Types:
Error occur in compile time, easy to find, that’s why called compile time
System.out.println(“No arguments”); {
} obj.test(10,20);
difficult to find the error, difficult to solve the error, so JVM handle this
type of error, that’s why it’s called runtime
class Animals { }
no using implementation
Child class derived properties from ancestor class- it is confused take path then
escape from the program, that’s why developed interface.
Example:
Interface Printable {
void hai()
System.out.println(“ I am a print”); }
Abstraction – elimination
arguments }
// check - create another abstract class public static void main(String args[])
} // obj.print();
void run() { }
immutable – String object getting only one reference(Assignment), again does not
change the values.
ex:
public class Stringimmutable {
public static void main(String args[]) {
String s = new String(“host name”);
s.concat(“come”);
System.out.println(“Value is” + s);
}
}
Why immutable:
Java uses the string literal concept, suppose there are 5 reference
variable, all the refers to a one object “scion”, if one reference
variable changes the value of object, it will be affected to the entire
reference variable.
How to overcome immutable:
stack memory raises when string is immutable because lot of objects
using in string program
using stringbuffer and stringbuilder overcome the immutable.
slow process
best quality
s.append(“hai”);
s.append(“come”);
System.out.println(“ “ + s);
fast process
low quality
s.append(“hai”);
s.append(“come”);
System.out.println(“ “ + s);
package string;
Class Stringmani {
System.out.println(“String1” + v1);
System.out.println(“String2” + v2);
System.out.println();
System.out.println(“Trim” + (v1.trim()).concat(v2));
System.out.println(“Strlength” + v2.length());
Using more than one function using a program, any one function
Exception syntax:
try {
catch
try { }
ex.
con.close();
throw
but here explicitly written throw for how this function performing
throws
there main method create a single try block and multiple catch block for solving the
exception
try {
{ demo();
} catch(NullPointerException e) {
throw e; }
I/O is used to process the input and produce the output based on
the input.
java.io package contains all the classes required for I/P and O/P
operations.
The bulk data processing through file input streaming and file
output streaming.
{ catch (Exception e) {
try { System.out.println(e);
fout.write(b);
{ catch(Exception e) {
try { System.out.println(e);
Unused objects (no one use) are removed from memory using garbage
collector
When unused objects comes to live that time garbage collector allocate
memory for unused objects
When garbage collector runs that time regular works stop that means
stop the world event (STW), all application stop.
Ex: Anna University marks uploaded
Time taken is more for older generation compare than younger
generation, because older generation remove and search large amount
of data.
Overcome the world event (STW)
Developer wants garbage collector running at the same time program
also running
Ex: Sweep in road
like a container
Methods:
Iterated Interface
Hashmap hm = new Hashmap(); // Hashmap- this is java function map.Entry me = (map.entry) i.next();
// multiple (,) not possible in java API that means multi keymap System.out.println();
hm.put(101, “Vinoth”); }
hm.put(102, “Saran”); }
hm.put(103, “Ravi”);
hm.put(103, “R”); // Duplicate key value, Print this value only. Hash- Not maintain order
//SCION
using iterated
RESEARCHfunction
ANDonlyDEVELOPMENT
possible in Set so get the permission Sign in Shine out
from Set
Another way possible using this type
- For each loop method
98
Not synchronized
Maintain order
Random Access
package.com.datadotz.collection.arraylist; al.add(“Ravi”);
} System.out.println(st.rollno+ “ ” + st.name + “ “
al.add(“sonoo”); }
al.add(“jay”);
al.addAll(al2);
public static void main(String args[]) { elements after removing the elements
al.add(“scion”); while(itr.hasNext()) {
al.add(“armada”); System.out.println(itr.next());
al2.add(“Ravi”); }
al2.add(“scion”); }
al.removeAll(al2);
public static void main(String args[]) { after removing the elements of al2….”);
al.add(“Ravi”); while(itr.hasNext()) {
al.add(“scion”); System.out.println(itr.next());
al.add(“armada”); }
al2.add(“Ravi”); }
al2.add(“scion”);
al.retainAll(al2);
not synchronized
no random access
simply it is a collection
numbers = { 1,12,33,43}
distinct elements
set have elements only but map have key and value (two elements)
{ al.add(“armada”);
add(object)
offer(object)
remove()
poll()
element()
peek()
size()
ArrayDequeue<String>(); // get the values does not remove the element from
queue.add(“C”); // get the first value and remove that object from
queue.add(“D”); Dequeue
if(obj1.equals(obj2))
// if(obj1 == obj2)
else
}}
catch(FileNotFoundException e) { }
e.printStackTrace(); }
} System.out.println(“Completed”);
catch(IOException e) { }
e.printStackTrace(); }
} I/P:
finally { create text file
if(br!=null) { “1.0.0.0”,”1.0.0”, “121”,
try { “167”,”IND”, “INDIA”
br.close();
}
catch(IOException e) {
e.printStackTrace();
}
data get from XML using one programming languages (like C,C++,Java)
both java and lib(DOM, SAX, JDOM, JAXB) files get data from XML
Simple
Install JDOM
package com.jdomparser; }
import java.io.file; }
// getting root element from XML document // reading attribute from Element using JDOM
+ root.getname()); System.out.println(“Author:” +
root.getChildren().Size()); System.out.println(“Title:” +
System.out.println(“Pages:” +
?> <author>armada</author>
<books> <title>Software</title>
<author>SCION</author> <language>English</language>
<title>Java</title> </book>
<pages>180</pages> </books>
<language>English</language>
</book>