Basic_of_JAVA
Basic_of_JAVA
JAVA
Q. what is JAVA?
JAVA is pure object oriented language
Q. what is object oriented?
Object oriented is a concept of programming and which based on four pillars and seven features.
There are two types of object oriented languages
1) Semi object oriented: Semi object oriented means those language can use OOP concept but
there is no compulsion to use object oriented every time called as semi object oriented.
Example of semi object oriented language is: CPP
2) Pure Object oriented: those languages must be use OOP concept compulsory called as pure
object oriented language means in java we cannot run program without class so java is pure object
oriented.
If we want to work with JAVA we have to know how to create program in java and execute it.
Steps to work with program in JAVA
1) Installed JDK: JDK stands for Java Development kit and it is Software or application which contains
some supporting application which is responsible to create enviourment for develop and execute JAVA
Application.
IT contain some software like as Compiler, JVM etc
if we think about above diagram we demonstrate we have JDK and JDK contain some supporting
software’s.
Q. what is Compiler?
Compiler is application or software which is used for convert your source code to Byte code in java
shown in following diagram.
Q. What is JVM?
JVM stands for java virtual machine and it is application which easily read byte code and generates
output.
Q. why use byte code?
Byte code is platform independent code of java means byte code can execute on any operating system
so JAVA is platform independent language.
Example: suppose consider if we compile your code on window operation then we can run this code on
Linux without taking support any third party software called as platform independent.
Following Diagram shows Platform Independency
In above diagram we demonstrate byte code and execute on any operation system so byte code is
platform independent code so java is also platform independent code.
2) Create Sample program: if we want to create any program in java we need to write minimum single
class so if we want to work with java we have some following standard and generalize syntax.
Generalize syntax is
access specifier class classname{
public static void main(String x[])
{ write here logics
}
}
Example: public class FirstDec
{
public static void main(String x[])
{ System.out.println(“good morning”);
}
}
Code Description
public class FirstDec: Here public is access specifier
Q. what is access specifier?
access specifier are some keywords which is used for apply restrictions on class and its member.
There are four types of access specifier?
private : private access specifier means we can cannot access class member outside of class.
public: public is access specifier which is used for access class member outside of class or outside of
package.
protected: protected class member is only accessible in child class
default : default class member can accessible outside of class but within same package.
class is keyword for class declaration
FirstDec is classname and user can give any name to his class.
public static void main(String x[]): it is main function of java same like as main function in C or CPP.
Interview questions on main method we will discuss later
i) why main method is static ii) can we overload main method iii) can we pass different parameter in
main method other than string etc
if we want to create Java program we have number of text editors as well as tool
3) Save Program: if we want to save java program then save in bin folder where JDK installed and give
classname and filename same with .java extension.
Example: our file name should be FirstDec.java
4) Compile Program:
Q. what is compilation?
Compilation is process where we convert your source code to byte code in java.
Q. How to compile java program when we create program using notepad?
Steps: start menu --- search --- command prompt --- go where java file save -- type command
javac filename.java.
5) Run Program: when we want to run java program we have command java filename
Here java work as JVM
Java FirstDec
If we think about above code we calculate addition of two numbers but we calculate fix number addition
and we want to accept input from keyboard and then calculate its addition
Means using command line argument we can accept n number of input or infinite input from keyboard
but the first input of command line argument is at position of zero because it is array and array start
from 0th index up to n-1.
Example
public class AddApp
{
public static void main(String x[])
{ int a,b,c;
a=x[0];
b=x[1];
c=a+b;
System.out.printf("Addition is %d\n",c);
}
}
if we think about above code we get compile time error in computable types.
because we have statement int a=x[0] and int b=x[1] here x[0] and x[1] is type of string but we want to
accept input of type integer and string cannot store in integer directly so compiler will generate error to
us.
so if we want to avoid this problem or solve this error we need to convert string value to integer value.
Example: WAP to input number and reverse it using command line argument
public class ReverseNum
{
public static void main(String x[])
{ int r=0;
int no=Integer.parseInt(x[0]);
while(no!=0)
{ int rem = no % 10;
no = no /10;
r=r*10+rem;
}
System.out.println("Reverse is "+r);
}
}
If we think about above code we accept input from keyboard but command line argument can accept
input from keyboard on single line.
if we want to accept input on new after program running statement for that we have Scanner class.
Example:
import java.util.*; //step1
public class AddUsingScannerApp
{ public static void main(String x[])
{Scanner xyz = new Scanner(System.in);//step2
int a,b,c;
System.out.println("Enter two values");
a=xyz.nextInt();
b=xyz.nextInt();
c=a+b;
System.out.printf ("addition is %d\n",c);
}
Example: WAP to input name id and salary of employee from keyboard and display it.
import java.util.*;
public class EmployeeApp
{
public static void main(String x[])
{
Scanner xyz = new Scanner(System.in);
int id,sal;
String name;
System.out.println("Enter name of employee");
name=xyz.nextLine();
System.out.println("Enter id of employee");
id=xyz.nextInt();
sal=xyz.nextInt();
System.out.printf("Name is %s\n",name);
System.out.printf("Id is %d\n",id);
System.out.printf("Salary is %d\n",sal);
}
}
Arrays in JAVA
___________________________________________________________________________________
Q. array is a collection of similar data type.
How to use array in java?
If we want to use array in java we have following syntax
1) Declare array: when we declare array then it is by default null means in java array variable work as
reference or pointer.
Syntax: datatype variablename[]; //this is reference variable and contain default value as null
e.g int a[]; //here a work as reference.
2) Allocate memory of array: if we want to allocate memory of array we have to use new keyword
means using a new keyword we can allocate memory of array.
variablename = new datatype[size];
e.g a = new int[5]; here we create new 5 block by using new keyword and store its base address in
reference variable a shown in following diagram.
Example: WAP to create array of size 5 and store values in it and display it.
import java.util.*; //step1
public class ArrayApp
{
public static void main(String x[])
{ Scanner xyz = new Scanner(System.in);
int a[]; //declaration
a = new int[5];
System.out.println("Enter values in array");
for(int i=0; i<a.length; i++)
{ a[i]=xyz.nextInt();
}
System.out.println("Display array values");
for(int i=0; i<a.length;i++)
{ System.out.printf("a[%d] ---->%d\n",i,a[i]);
}
}
}
Example: WAP to input five values in array and find maximum value from Array.
{ a[i]=xyz.nextInt();
}
int max=a[0];
System.out.println("Display array values");
for(int i=0; i<a.length;i++)
{ System.out.printf("a[%d] ---->%d\n",i,a[i]);
if(a[i]>max)
{ max=a[i];
}
}
System.out.printf("Maximum value is %d\n",max);
}
}
Q . what will be output of given code?
if we think about above code we have int a[]=new int[]{10,20,30,40,50} here we create array of size 5
internally and we store base address of array i.e 1000 in reference variable a and we have one more
statement int b[] means we declare reference of integer type of array and we have again one more
statement b=a; here we store base address array a i.e 1000 in reference variable b means reference
variable b and reference variable a points to same memory and again we have statement b[1]=200
means we store 200 on address location 1004 or a[1] reference variable a and reference variable b
points to same array so 200 get override on a[1] so when we print array then we get output 10 200 30
40 50
Output: 20 30 40 50 60
if we think about above code we have two array int a[]=new int[]{10,20,30,40,50} here we have array
with 5 values and we store its base address i.e 1000 in reference variable a and we have one more
function name as show(a,0) here we call show() function and pass base address of array to variable m
means m is array which contain base address of a means m contain 1000 so m points to variable a so we
have statement if(count!=m.length) so here initially count is 0 so the condition 0!=5 is true so we have
m[count]=m[count]+10 means m[0] =m[0]+10 means a[0]=a[0]+10 means m[0]=10+10 so this value
10+10 is 20 and it is stored on address location 0th or 1000 address where a and m points so previous
value of 1000 location get override and 20 value store on 1000 location means a[0]=20 again we have
statement show(m,++count) here we perform recursion again count is 1 and so on means we increase
ever value by 10 and store in array so we get final output 20 30 40 50 60
Example
import java.util.*;
public class MatrixApp
{ public static void main(String x[])
{ Scanner xyz = new Scanner(System.in);
int a[][]=new int[3][3];
System.out.println("Enter values in matrix");
for(int i=0; i<a.length;i++)
{
for(int j=0; j<a[i].length; j++)
{
a[i][j]=xyz.nextInt();
}
}
System.out.println("Display matrix");
for(int i=0; i<a.length;i++)
{
for(int j=0; j<a[i].length; j++)
{
System.out.printf("%d\t",a[i][j]);
}
System.out.printf("\n");
}
}
}
import java.util.*;
public class JaggedArray
{
public static void main(String x[])
{
Scanner xyz = new Scanner(System.in);
int a[][]=new int[3][];
a[0]=new int[3];
a[1] = new int[4];
a[2] = new int[2];
System.out.println("Enter values in matrix");
for(int i=0; i<a.length;i++)
{
for(int j=0; j<a[i].length;j++)
{
a[i][j]=xyz.nextInt();
}
}
System.out.println("Display matrix");
for(int i=0; i<a.length;i++)
{
for(int j=0; j<a[i].length;j++)
{
System.out.printf("%d\t",a[i][j]);
}
System.out.printf("\n");
}
}
}
if we think about the above code program compile successfully but not run
because in main method we pass int x[] so compiler think main may be overloaded by
user but JVM run program using main method in which contain string [] parameter.
If we think about above diagram then we have first component in JVM architecture is class loader.
What is purpose of class loader?
The major goal of class loader is read .class file generated by java compiler and perform three different
operations on it i.e. loading, linking and initialization of .class file.
Q. what is loading?
when compiler generate .class file then JVM class loader reads .class file and generate corresponding
binary data from .class file and save it in the method area and JVM store the following information in
method area
I) Fully Qualifier name of the loaded class and its immediate parent class
ii) Store information about interface, class or enum
iii) Store information related with modifier, variables and method information.
Example: suppose consider we have class name as
class ABC
{ private int a;
public void show()
{
}
}
when we compile this program then compiler generate file name as ABC.class and when we run this
program then JVM class loader read file name as ABC.class and store class name , private,public int a
and void show() its information in method area means it load all class details in method area.
Q. what is linking?
Linking is process to verify and prepare byte code as well as binary code which loaded by class loader.
means in short we can in the linking processes check .class file is created properly or not means in
linking we have BytecodeVerifier which is used for verify byte code generated property or not if byte
code not generated properly it will throw error at run time java.lang.VerifyError
as well as linking section JVM allocates memory for class static variable and initialization the default in it
means JVM allocate memory static variable before creating and give some default value to them means
if static variable is integer then 0 default if static variable is string then give null value etc.
Q. what is initialization?
after verify byte code and allocate memory to static variable after that JVM try to initialize code for
execution purpose from top to bottom.
There are three types of initialize in JVM
1) Bootstrap class loader 2) extension class loader 3) System class loader.
1) Bootstrap class loader: bootstrap class loader is used for load the core API classes present in
JAVA_HOME/lib folder.
2) Extension class loader : it is used for load classes present under JAVA_HOME/jre/lib/ext folder
3)System class loader or Application class loader : it is child of extension class loader it is responsible
load all classes from classpath.
JVM Memory?
JVM memory is distribution of memory management in ram means when your program running in
memory then JVM memory decides which content program store in which section of memory.
We have different types of section in memory
Method Area: in this method area all class level information like as class name, its immediate parent
class name , methods and variables and access specifier used with variable and method etc stored in
method area as well as store static variable and its default value stored in method area.
Heap Section: in this section memory all objects stored in heap section of memory as well as all shared
resources stored in heap section means if we perform any thing using new keyword in java it is stored in
heap section.
Stack Area: so JVM create separate stack memory for every thread in java.
PC Register: store address of current execution instruction of thread