Core Java Material
Core Java Material
Source code:
Java Features:
Robust simply means strong. Java is robust because It uses strong memory management.
6.Portable:
Java is portable because it allows to carry the Java bytecode to any platform.
7.Architecture Neutral:
Java is architecture neutral because the size of primitive types is fixed for both 32 bit and 64
bit operating systems
8.Dynamic:
Java is a dynamic language. It supports dynamic loading of classes from other programmes.
It also supports functions from its native languages, i.e., C and C++.
9.Interpreted:
Java byte code is translated to machine code during run time and is not stored anywhere.
Important Points:
JAVA is a case sensitive programming language, meaning capital letters and small letters are
different in java programme.
Names used for classes, variables, and methods are called identifiers
Data types specify the different sizes and values that can be stored in the variable
Example:
public class Hello
{
Operators
Java provides operators to manipulate variables
Arithmetic Operators: Arithmetic operators are used for mathematical expressions, they
are :
+ - * / % ++ --
Example:
public class Hello
{
The Relational Operators: Relational operators are used for relational expressions, they
are:
== != > < >= <=
Example:
public class Hello
{
public static void main(String args[])
{
int val1=10;
int val2=20;
System.out.println(val1>val2);
}
}
The Logical Operators: Logical operator works for logics , they are:
&& || !
Example:
public class Hello
{
public static void main(String args[])
}
}
The Assignment Operators: Assignment operators are used for assignation, they are:
= += -= *= /= %= &= !=
Example:
public class Hello
{
public static void main(String args[])
{
int val1=10;
int val2;
val2+=val1;
System.out.println("the value of val2 is"+val2);
}
}
}
}
Conditional Statements:
1. If statement
Syntax:
if(condition)
{
}
Example:
public class Hello
{
public static void main(String args[])
{
int val1=10;
int val2=10;
if(val1==val2)
{
System.out.println("inside if block");
}
System.out.println("this is last statement");
}
}
2. If- else statement
Syntax:
if(condition)
{
Syntax:
while(condition)
Example:
int aa=0;
while(aa<=2)
aa++;
Syntax:
do
While(condition)
Example:
do
aa++;
while(aa<=2);
3.for loop
Syntax:
Example:
for(int i=0;i<=5;i++)
Arrays:
An array is a data structure, which allows to store fixed-size data and similar kind of data
Syntax:
1.Abstraction:
It is an act of representing the essential features hiding the background details
2.Encapsulation:
It is a mechanism of combining the data and code in to a single unit
Class:
Class is a collection of data and methods
Syntax:
Class classname
{
…….
}
Method:
Method is a set of instructions defined inside a class and performs some operations on the
data
Synatax:
Method name
{
…….
}
Object:
Object is an instance of a class. Once object is created, memory is allocated to the data.
Synatax:
classname objectname=new classname();
Example:
class First
{
int aa=50;
public void demo()
{
System.out.println("executing demo method");
}
}
public class Hello
{
public static void main(String args[])
{
First ff=new First();
ff.demo();
System.out.println("the value of aa is "+ff.aa);
}
void keyword:
If a method does not return any value, we need to use void keyword for that method in
method signature
return keyword:
If a method returns any values, we need to use return keyword in the method definition
Example:
class One
{
public void show()
{
System.out.println("this is show method");
}
public int dummy()
{
int ff=34;
ff++;
return ff;
}
}
public class ThreadPrior
{
public static void main(String args[])
{
One oo=new One();
oo.show();
int val=oo.dummy();
System.out.println("the int value is"+val);
}
}
Call by value:
If we call(invoke) any method by passing a value we call it as call by value.
Example:
class One
{
public void dummy(int val)
{
val++;
System.out.println(val);}
}
public class ThreadPrior
{
public static void main(String args[])
{
One oo=new One();
oo.dummy(23);
}
}
Class A
{
…….
}
Class B extends A
{
…….
}
Types of Inheritance:
1.Single Inheritance 2.Multi level Inheritance 3.Hierarchial Inheritance
1.Single Inheritance:
In this inertance there will be one parent class and one child class
Example:
class First
{
public void demo()
{
System.out.println("executing demo method");
}
}
class Second extends First
{
public void demo1()
{
System.out.println("executing demo1 method");
}
}
public class Hello
{
public static void main(String args[])
{
Second ss=new Second();
ss.demo();
ss.demo1();
}
}
Example:
class First
{
public void demo()
3.Hierarchial Inheritance:
In this inheritance, there will be one parent class and more than one child class
Example:
class First
{
public void demo()
{
System.out.println("executing demo method");
}
}
class Second extends First
{
public void demo1()
{
System.out.println("executing demo1 method");
}
}
class Third extends First
{
public void demo2()
{
System.out.println("executing demo2 method");
}
3.Polymorphism:
Polymorphism is the ability of an object to take on many forms.
Types of Polymorphism:
1.Static polymorphism 2.Dynamic polymorphism
1.Static polymorphism:
This is done at compile time through method overloading concept
2.Dynamic polymorphism:
This is done at run time through method overriding concept
Method overloading:
Two or more methods can have same name but they should differ in their parameters list in
terms of number of parameters ,datatypes of parameters
Example:
class First
{
public void demo()
{
System.out.println("executing demo method");
}
public void demo(float ff)
{
System.out.println("executing demo method and value of ff is "+ff);
}
}
public class Hello
{
public static void main(String args[])
{
First ff=new First();
ff.demo();
ff.demo(34.5f);
}}
Method overriding:
Example:
class First
{
public void demo()
{
System.out.println("executing demo method of super class");
}
}
class Second extends First
{
public void demo()
{
System.out.println("executing demo method of sub class");
}}
public class Hello
{
public static void main(String args[])
{
Second ss=new Second(); ss.demo();
}}
Constructor:
Types of Constructors:
1.Default constructor 2.Parameterised constructor
1.Default constructor:
In this constructor there will not be any parameters
Example:
public class Hello
{
public static void main(String args[])
{
Hello1 one=new Hello1();
}
}
class Hello1
{
public Hello1()
{
System.out.println("executing constructor code");
}
}
2.Parameterised constructor:
Example:
public class Hello
{
public static void main(String args[])
{
Hello1 one=new Hello1(56.2d);
}
}
class Hello1
{
public Hello1(double dd)
{
System.out.println("the value of dd inside constructor is "+dd);
}
}
Constructor overloading:
Two or more constructors can have same name but they should differ in their parameters in
terms of number of parameters ,datatypes of parameters
Example:
public class Hello
{
public static void main(String args[])
{
Hello1 one=new Hello1(56.2d);
Hello1 two=new Hello1(67);
}
}
class Hello1
{
public Hello1(double dd)
{
System.out.println("the value of dd inside constructor is "+dd);
}
public Hello1(int kk)
{
System.out.println("the value of kk inside constructor is "+kk);
}
}
super:
The super keyword in Java is a reference variable which is used to refer immediate parent
class object.
Example:
packages:
A javapackage is a group of similar types of classes, interfaces
Advantages:
Package is used to categorize the classes and interfaces so that they can be easily
maintained
Syntax:
package packagename;
import packagename.classname;
Types of packages:
1.pre defined packages 2.user defined packages
2. user defined packages –These are our own packages created and contains the user
defined classes and user defined interfaces
Example:
package javaexamples;
public class Hello
{
public void demo()
{
System.out.println("executing demo code");
}
}
import javaexamples.*;
public class Hello1
{
public static void main(String args[])
{
Hello hh=new Hello();
hh.demo();
}
}
Sub package --A package inside another package is called sub package
String class:
String is a pre defined class and is a non primitive data type to store more than one
character value
Important methods:
equals()
toUpperCase()
toLowerCase()
concat()
if(ss.equals(ss1))
{
System.out.println("values are same");
}
else
{
System.out.println("values are not same");
}
System.out.println(ss.toUpperCase());
System.out.println(ss.toLowerCase());
System.out.println(ss.concat(ss1));
}
}
if(aa.equals(bb))
{
System.out.println("values are equal");
}
else
{
System.out.println("values are not equal");
}
}
}
Important methods:
append() --this method will joins two string values permanently
insert(index,string value) --- this method will insert string values in specified location
reverse() --- this method will reverse the string value
Example
public class Hello
{
public static void main(String args[]) {
//String aa="hyd";
Example:
class One
{
private int age=10;
public void demo()
{
System.out.println("the value age is "+age);
}
}
class Two extends One
{
}
public class Hello
abstract: abstract keyword is used for classes and methods only. If a method does not have
any body ,it is called as abstract method. If a class contains at least one abstract method it
is called as abstract class
Example:
abstract class One
{
int kk=45;
The keyword ‘implements’ will be used for a class to provide interface definitions
Example:
interface One
{
public void demo();
}
class Two implements One
{
public void demo()
{
System.out.println("executing demo method");
}
}
public class Hello
{
public static void main(String args[])
{
Two oo=new Two();
oo.demo();
}
}
Example:
Integer.parseInt(),Float.parseFloat() ---- these method are used to convert String objects into
primitive data types
Exception Handling:
Exception Handling is a mechanism to handle runtime errors
try: In try block we need to place the code which might generate exception
throw: throw keyword is used to explicitly throw an exception(creating our own exception)
Exception class :This is the parent class for all the exception classes
Example1:
public class Hello
{
public static void main(String args[])
{
try
{
System.out.println("the passed argument is "+args[0]);
}
catch(Exception e)
{
System.out.println("please pass the arguments from command prompt");
}
finally
{
System.out.println("executing finally code");
}
}
}
Example2:
Class One
{
Public void display()
Example3:
Import java.io.*;
Class One
{
Public void display() throws IOException
{
Int age=50;
If(age<21)
{
System.out.println(age);
}
}
}
Multi Tasking:
Execution of several tasks at the same time is multi tasking
In multitasking OS will allocate separate memory and resources to each program(task) that
CPU is executing
2.Runnable:In this state the thread instance is invoked with a start method and thread will
start its operation
3.Running:When the thread starts executing, then the state is changed to "running" state
5.Dead: once the processing is over , the thread will be in dead state
Thread Scheduler: Thread scheduler in java is the part of the JVM that decides which
thread should run.
Important classes ,methods:
Methods:
1.start() ---this method is used to start the thread
3.sleep(time in milliseconds) ---this method causes a thread to sleep for specified time
4.join() ---this method will make other threads to wait till the current thread completes its
operation
Example1:
Example2:
Example3:
Methods:
add(object);
remove(index)
get(index)
import java.util.*;
public class One
{
public static void main(String aa[])
{
ArrayList l=new ArrayList();
l.add("hyderabad");
l.add("chennai");
System.out.println(l);
//l.remove(0);
System.out.println(l);
System.out.println(l.get(0));
}}
Vector:
It is similar to ArrayList but Vector is by default synchronized one. It accepts duplicates.
Methods:
addElement(object);
removeElement(object);
get(index);
import java.util.*;
public class Two
{
public static void main(String aa[])
{
Vector l=new Vector();
l.addElement("hyderabad");
l.addElement("chennai");
l.addElement("mumbai");
Methods:
addFirst()
addLast():
getFirst()
getLast()
removeFirst()
removeLast()
import java.util.*;
public class Three extends LinkedList
{
public static void main(String aa[])
{
Three l=new Three();
l.addFirst("hyderabad");
l.addFirst("chennai");
//System.out.println(l);
l.addLast("mumbai");
l.addLast("pune");
//System.out.println(l);
String ss=(String)l.getFirst();
//System.out.println(ss);
String sg=(String)l.getLast();
//System.out.println(sg);
l.removeFirst();
System.out.println(l);
}
}
HashSet
In HashSet data is stored using hash table(key-value pair) . No duplicates are allowed
Objects are stored not in order
Methods:
add(object);
remove(object);
import java.util.*;
public class Four
{
public static void main(String aa[])
{
Set l=new HashSet();
l.add("rahul");
l.add("priya");
l.add("sumit");
//l.add("sumit");
System.out.println(l);
l.remove("priya");
System.out.println(l);
}
}
TreeSet
In TreeSet objects are stored in the form of tree .No duplicates are allowed,
access and retrieval time of TreeSet is quite fast.
The objects in TreeSet stored is in ascending order.
Methods:
add(object);
remove(object);
HashMap
In HashMap data is stored using hash table(key-value pair) .In this, Keys also we need to
provide.
Methods:
put(key,value);
get(key);
import java.util.*;
public class Five
{
public static void main(String aa[])
{
Map l=new HashMap();
l.put("1","java");
l.put("2","dot net");
String ss=(String)l.get("2");
Integer a=new Integer(10);
l.put("key",a);
System.out.println(l);
}
}
TreeMap
In TreeMap data is stored using hash table(key-value pair). In this Keys we need to provide.
No duplicates are allowed, Objects are stored in order
Methods:
put(key,value);
get(key);
Iterator
Iterator allows to traverse a collection. It can be used for any collection framework
Methods:
iterator()---to get iterator object.
hasNext()-to check if elements are available
next()-to move to next objects
remove()-to remove objects
import java.util.*;
public class Six
{
Methods:
elements()
hasMoreElements()
nextElement()
import java.util.*;
public class Seven extends Vector
{
public static void main(String aa[])
{
InputStream:
Stream Types:
1.Byte oriented streams 2.Character Oriented streams
1.Byte oriented streams: Byte streams perform the read write operation byte by byte(8-bit)
Java.io.*;
InputStream classes
DataInputStream
FileInputStream
BufferedInputStream
SequenceInputStream
ObjectInputStream
OutputStream classes
DataOutputStream
FileOutputStream
ObjectOutputStream
InputStream classes
DataInputStream: This class is used to read the data from the keyboard
import java.io.*;
public class One
{
public static void main(String aa[])throws IOException
{
DataInputStream di=new DataInputStream(System.in);
System.out.println("enter ur name");
String name=di.readLine();
System.out.println("hello "+name);
di.close();
}
}
BufferedInputStream: This class will read the bytes fast and increases the performance of
the application
import java.io.*;
public class Three
{
public static void main(String aa[])throws Exception
{
FileInputStream fi=new FileInputStream("Two.java");
BufferedInputStream br=new BufferedInputStream(fi);
long t=System.currentTimeMillis();
int i=br.read();
while(i!=-1)
{
System.out.print((char)i);
i=br.read();
}
long t1=System.currentTimeMillis();
System.out.println("time taken is"+(t1-t)+"milliseconds");
br.close();
fi.close();
}
}
SequenceInputStream: This class will read more from multiple streams. It read data
sequentially
import java.io.*;
public class Four
OutputStream classes:
DataOutputStream: This class is used to writing the java data types to a stream
write()
close()
import java.io.*;
public class Dataout
{
public static void main(String[] args) throws IOException
{
int aa=66610855;
import java.io.*;
class Student implements Serializable
{
int id;
String name;
Student()
{
id=5;
name="manoj";
}
void display()
{
System.out.println("id is"+id);
System.out.println("name is"+name);
}
}
Reader
FileReader
BufferedReader
InputStremReader
Writer
FileWriter
RandomAccessFile
1.FileReader : This class is used for reading the data from the file in the form of
characters(ASCII format)
Methods:
read()
close()
import java.io.*;
public class FR
{
public static void main(String aa[])throws Exception
{
FileReader fi=new FileReader("Two.java");
int i=fi.read();
while(i!=-1)
{
System.out.print((char)i);
i=fi.read();
}
fi.close();
}
}
Methods:
close()
readLine()
3.InputSteamReader : This class is a bridge from byte streams to character streams. It reads
bytes and converts them into characters
Methods:
close()
read()
import java.io.*;
public class eight
{
public static void main(String aa[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the name");
String ss=br.readLine();
System.out.println("ur name is"+ss);
br.close();
}
}
4.FileWriter : This class is used for writing the data to the file
Methods:
close()
read() ---to read the file
import java.io.*;
public class FW
{
public static void main(String aa[])throws Exception
{
String val="34";
FileWriter fos=new FileWriter("Student1.txt");
5. RandomAccessFile: This class is used for accessing a particular file randomly and
perform read /write operations
Methods:
seek() ---placing the file pointer at the required location
writeUTF() --to write to the file
length() ---returns the size of the file
getFilePointer() --this will return the pointer location
close()
Example1:
import java.io.*;
public class Eighty
{
public static void main(String aa[])throws Exception
{
RandomAccessFile ra=new RandomAccessFile("zero.txt","rw");
ra.seek(ra.length()-1);
String ss="java class data";
ra.writeUTF(ss);
System.out.println("written");
ra.close();
}
}
Example2:
import java.io.*;
public class Nine
{
public static void main(String aa[])throws Exception
{
RandomAccessFile ra=new RandomAccessFile("zero.txt","rw");
ra.seek(Integer.parseInt(aa[0]));
int i=ra.read();
while(i!=-1)
{
System.out.print((char)i);
i=ra.read();
}
long ii=ra.getFilePointer();
System.out.println("location is"+ ii);
ra.close();
}
}