Tuesday, 29 September 2015

Program to concatenate two strings and prove staring object is immutable (it also displays Hashcode)

/**
 *
 */

/**
 * @author U0123154
 *
 */
public class Stringcmp3 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "Data";
System.out.println("The hashcode of s1 is " +s1.hashCode());
String s2 = "Base";
System.out.println("The hashcode of s2 is " +s2.hashCode());
s1 = s1 + s2;
System.out.println("The hashcode of s1 is "+s1.hashCode());

System.out.println("The value of s1 is " +s1);

}

}


Output:-
The hashcode of s1 is 2122698
The hashcode of s2 is 2063089
The hashcode of s1 is 1853155771
The value of s1 is DataBase

Java Program to Comparing two strings using equals operator

/**
 *
 */

/**
 *
 *
 */
public class Stringcmp2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = new String("Test");
String s2 = new String("Test");
if(s1.equals(s2))
System.out.println("Equal");
else
System.out.println("Not Equal");
}

}


Output:-
Equal

Java Program to Comparing two strings using == operator

/**
 *
 */

/**
 *
 *
 */
public class Stringcmp1 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = new String("Test");
String s2 = new String("Test");
if(s1==s2)
System.out.println("Equal");
else
System.out.println("Not Equal");

}

}

output:-
Not Equal

Java Program to Comparing two strings using == Operator

/**
 *
 */

/**
 *
 *
 */
public class Stringcmp {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = new String("Test");
String s2 = "Test";
if(s1==s2)
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}

output:-
Not Equal

Java Program to output starts in a pattern


public class Stars {

public static void main(String[] args) {
// TODO Auto-generated method stub
//int i,j;
for(int i=1;i<5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}

}


Output:-
*
**
***
****

Java Program to Splitting a String

/**
 *
 */

/**
 *
 *
 */
public class Splitstring {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = new String("This is Sai from Bangalore");
String a[] = s.split(" ");
for(int i=0;i<5;i++){
System.out.println(a[i]);

}
}

}


Output:-
This
is
Sai
from
Bangalore

Java Scanner class example

import java.util.*;

public class Demoo{

public static void main(String args[])
{
 System.out.println("Enter the Employee Name ,Employee ID and Employee Salary");
 Scanner sc = new Scanner(System.in);
 String s = sc.next();
 int i = sc.nextInt();
 float j = sc.nextFloat();
 System.out.println("The employee name is "+s);
 System.out.println("The employee Employee ID is "+i);
 System.out.println("The employee Salary is "+j);
 }
}

Output:-
Enter the Employee Name ,Employee ID and Employee Salary
sai 01675 100000
The employee name is sai
The employee Employee ID is 1675
The employee Salary is 100000.0


Java Constructor example


//Main Program

public class Sample {

public static void main(String[] args) {
// TODO Auto-generated method stub
int i=200;
First Obj = new First(23);
boolean a = Obj instanceof First;
System.out.println("The value of a is " +a);
System.out.println("The value of i is " +i);
if(i==200)
{
System.out.println("The value is " +i);
}
else
{
System.out.println("The value is greater than (or) less than 200");
}
Obj.good();
}
}


//Constructor

public class First {
int age;
First(int age)
{
this.age=age;
System.out.println("The Age is " +age);
}
void good(){
System.out.println("Good");
}
}

Output:-
The Age is 23
The value of a is true
The value of i is 200
The value is 200
Good

Java Linear Search Program

/**
 *
 */
import java.io.*;
import java.util.*;
/**
 *
 *
 */
public class Linearsearch {

/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
String s[] = new String[5];
boolean found = false;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<5;i++)
{
s[i]=br.readLine();

}
System.out.println("Enter the String to search");
String Search = br.readLine();
for(int k=0;k<5;k++)
{
if (Search.equals(s[k]))
{
System.out.println("String found and it is " +Search);
found = true;
}
}
if(!found){
System.out.println("No search item found");
}
}

}

Output:-
sai
nagendra
bhavani
prasad
battipati
Enter the String to search
battipati
String found and it is battipati


Java Scanner Class Example

/**
 *
 */
import java.io.*;
import java.util.*;
/**
 * @author U0123154
 *
 */
public class DynamicArray {

/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i = Integer.parseInt(br.readLine());
int array[] = new int[i];
//Accept the values dynamically
Scanner sc = new Scanner(System.in);
for(int k=0;k<i;k++){
array[k] = sc.nextInt();
System.out.println("The values are " +array[k]);
}
}

}


Output:-
5
1 2 3 4 5
The values are 1
The values are 2
The values are 3
The values are 4
The values are 5

Monday, 28 September 2015

Java Program to Check Whether a given year is a leap year or not

/**
 *
 */
import java.io.*;
import java.util.*;
/**
 *
 *
 */
public class Leap {

/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
System.out.println("Enter the year to check whether it is leap year or not..!");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int Year = Integer.parseInt(br.readLine());
if((Year%400 == 0) && (Year%100 == 0)){

System.out.println("It is leap Year");

}
else if((Year%100 != 0) && (Year%4 == 0)){

System.out.println("It is leap Year");

}

else{

System.out.println("It is Not leap Year");

}
}
}


Output :-
Enter the year to check whether it is leap year or not..!
2009
It is Not leap Year

For Each loop Example in Java


public class Foreach {

public static void main(String[] args) {
// TODO Auto-generated method stub
int k;
int array[] = {1,2,3,4,5};
for(int j:array){
//for(int i=0;i<=4;i++){
System.out.println();
System.out.println("The value of elements in the array before adding + is" +j);
k=j+1;

System.out.println("The value of elements in the array after adding + is" +k);
//}
//System.out.println("The value of the j is " +j);
}
}

}

Output:-

The value of elements in the array before adding + is1
The value of elements in the array after adding + is2

The value of elements in the array before adding + is2
The value of elements in the array after adding + is3

The value of elements in the array before adding + is3
The value of elements in the array after adding + is4

The value of elements in the array before adding + is4
The value of elements in the array after adding + is5

The value of elements in the array before adding + is5
The value of elements in the array after adding + is6

Demo on for,while and do..while loops


public class First {
public static void main(String args[]){
int i,j=0,k=0;
for(i=0;i<=10;i++){
System.out.println("The value of i is " +i +"\n");
}
while(j<=10){
System.out.println("The value of J is " +j +"\n");
j++;
}
do{
System.out.println("The value of k is " +k +"\n");
k++;
}while(k<=10);
}
}


Output:-
The value of i is 0

The value of i is 1

The value of i is 2

The value of i is 3

The value of i is 4

The value of i is 5

The value of i is 6

The value of i is 7

The value of i is 8

The value of i is 9

The value of i is 10

The value of J is 0

The value of J is 1

The value of J is 2

The value of J is 3

The value of J is 4

The value of J is 5

The value of J is 6

The value of J is 7

The value of J is 8

The value of J is 9

The value of J is 10

The value of k is 0

The value of k is 1

The value of k is 2

The value of k is 3

The value of k is 4

The value of k is 5

The value of k is 6

The value of k is 7

The value of k is 8

The value of k is 9

The value of k is 10

Febonic Series Program in Java

/**
 *
 */
import java.io.*;
//import java.util.*;
/**
 *
 *
 */
public class Fibonacci  {

/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int f1=0,f2=1,f=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the febonic series...till the numbers you are looking at .... !");
int i = Integer.parseInt(br.readLine());
System.out.println("The value of the i is " +i);
while(f<i){
f=f1+f2;
f2=f1;
f1=f;
System.out.println("The value of f is " +f);
//++i;
}
}

}

Out put:-

Enter the febonic series...till the numbers you are looking at .... !
10
The value of the i is 10
The value of f is 1
The value of f is 1
The value of f is 2
The value of f is 3
The value of f is 5
The value of f is 8
The value of f is 13

Java Program to verify positive (or) negative number

import java.io.*;
public class Demo {

public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
System.out.println("Enter a number");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
/*char ch = (char)br.read();
System.out.println("The value of Ch is " +ch);*/
long i = Long.parseLong(br.readLine());
//System.out.println("The value of i is "+i);
if(i>0)
System.out.println("The value is +Ve");
else
System.out.println("The value is -Ve");
}
}

Out put:-
Enter a number
20
The value is +Ve

Program to print Strings and Integers from an array


public class Arry {
public static void main(String args[])
{
int ar[] = {1,2,3,4,5};
String str[] = {"Sai","Nagendra","Bhavani","Prasad","Battipati"};
for (int i=0;i<5;i++){
System.out.println("The value of the elements are " +ar[i]);
System.out.println("The value of the elements are " +str[i]);
}
}
}

Out put :-

The value of the elements are 1
The value of the elements are Sai
The value of the elements are 2
The value of the elements are Nagendra
The value of the elements are 3
The value of the elements are Bhavani
The value of the elements are 4
The value of the elements are Prasad
The value of the elements are 5
The value of the elements are Battipati

Program to Perform Arithmetic Operation in Java

import java.io.*;
import java.util.*;
public class Accept {
public static void main(String args[]) throws IOException{
int k;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter two integers numbers to perform arithmetic operation");
String str = br.readLine();
StringTokenizer st = new StringTokenizer(str," ");
String s1 = st.nextToken();
String s2 = st.nextToken();
int i = Integer.parseInt(s1);
int j = Integer.parseInt(s2);
System.out.println("The addition of the two numbers is " +(i+j));
System.out.println("The Subtraction of the two numbers is " +(i-j));
System.out.println("The Multiplication of the two numbers is " +(i*j));
System.out.println("The Division of the two numbers is " +(i/j));
}
}



Output :-
Enter two integers numbers to perform arithmetic operation
20 5
The addition of the two numbers is 25
The Subtraction of the two numbers is 15
The Multiplication of the two numbers is 100
The Division of the two numbers is 4

Friday, 25 September 2015

Difference Between Two-Tier and Three-Tier Architecture?

Based on the Architecture, Applications were classified into two types as below:-

1) Two Tier architecture

2) Three Tier architecture


Let us discuss about 2 tier and 3 tier architecture in detail with example.
Two-Tier Architecture:-
The two-tier is based on Client Server architecture. The two-tier architecture consist of a client and server. There will be direct communication between client and server. There is no intermediate server between client and server. Both client and server are tightly coupled which causes the application to run faster.
Few examples of client / server communication are:-
  • Communication between the desktop application to database server.
  • Communication between the browser to web server.
  • Communication between the FTP client to FTP server.
The Two-tier architecture is divided into two parts:
1) Client Application (Client Tier)
2) Database (Data Tier)
Advantages:
1.    Easy to maintain and modification.
2.    Communication is bit faster.
Disadvantages:
1.    In two tier architecture application performance of the application degrade upon increasing the number of users/load.
2.    Cost-ineffective compared to other.
Three-Tier Architecture:-
Three-tier architecture typically consists of presentation tier, a business /data access tier, and a data tier. Three layers in the three tier architecture are as below:-
1) Client layer
2) Business layer
3) Data layer
1) Client layer:-
It is known as Presentation layer that consist of the User Interface of the application, example is User registration form which contains text box, label, button.
2) Business layer:-
All business logic is written like data validation, inserting data…e.t.c. It acts as the intermediary layer between Client layer and Data layer.
3) Data layer:
Data Access Layer contains methods to connect with database and perform database operations like insert, update, delete, get data from database based on our input data.
Advantages
1.    High performance and lightweight persistent objects
2.    Scalability –Tier can individually scale horizontally
3.    Performance – Because the Presentation tier can cache requests, network utilization is minimized, and the load is reduced on the Application and Data tiers.
4.    High degree of flexibility in deployment platform and configuration
5.    More Re-use
6.    Increases more Data Integrity
7.    Improves Security – Client do not have direct access to database.
8.    Easy to maintain and modification, will not affect other modules
9.    Performance is good in three tier architecture application.
Disadvantages
1.    Increase Complexity.

Thursday, 24 September 2015

Naming Conventions in Java



1. Class and Interface: - Each word of the class and interface name should start with capital letter as “String”,”DataInputStream”…e.t.c.

2. Packages: - All are written in small letters as
a)java.io
b) java.awt
c) java.swing

3. Variables and Methods: - First word with small letter.then second word should start with capital letter as bellow:-

Methods:-
a)readLine()
b)getNumberInstance()

Variables:-
            a)age
            b)empName
Note:- Here we are differentiating both with the braces “()” and maintaining the same naming convention for both.

4. Constants: - All Constants are represented in CAPS as PI,MAX…e.t.c.


5. Keywords: - All Keywords are represented in lower case letters as public,void,static….e.t.c.

All about "public static void main(String args[])"

JVM is the program written by javasoft people. If we do not write "public" keyword in front of the main then it will not be available to JVM and Main method will not be executed.

Only after calling the main method an object is created but to create an object we should first execute main method. ,So, We should call the main method with out creating the Object and such methods are called as static methods.


Static method is the one which is called without creating an object but calling with the classname.method().So that is the reason why main method is declared as static and JVM calls the main method as classname.main()

since main does not written any thing we specify "void" keyword.

also,we should pass String type array for the main method.

JVM Architecture

JVM Architecture
The internal architecture of the JVM is as below:-


1) Class loader:- It loads the .class file into the memory. Then it verifies all the class code and if it finds any suspicious, then the execution is rejected.
2) Class (Method) Area:-It stores all the class code ,code of variables and methods and also all static variables are created in this area.
3) Heap:-It stores all the Objects.
4) Java Thread/Stack:-As we know that method code is stored in method area but while executing the method to store intermediate results it uses stack. Java uses separate thread to execute each method.
A new frame is created each time a method is executed destroyed when its method execution is completes.
5) Program Counter Register:-PC (program counter) register contains the address of the Java virtual machine instruction currently being executed. If there are 3 methods executing then there will be 3 PC registers maintained.
6) Native Method Stack:-Like Java methods execute in java stack, similarly all the native methods execute in native method stack. Example for Native lib is C, C++ functions. The native method lib is connected to JVM through a program called Native method interface.
7) Execution Engine:-
It contains of below components:-
1) Interpreter
2) Just-In-Time(JIT) compiler


The java code is executed by both interpreter and JIT compiler simultaneously so that the execution time is reduced and performance is increased. The code that is executed by JIT compiler is called as HOTSPOTS (company).