Let's learn java programming language with easy steps

Friday, 18 August 2017

Star Pattern Programs In Java


Java Star Pattern Programs

Star Pattern In Java

Here we will discuss some star pattern programs in java which will help you in java programming questions in an interview.

There can be any kind of pattern programs in java like number pattern programs in java, alphabet programs in java, etc. But here we will discuss only star(*) pattern programs in java.

so let's start with it.

Star Pattern 1:

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *

Solution:

class Demo1
{
public static void main(String args[])
{
for(int i = 1; i<=8; i++)
{
for(int j = 1; j<i; j++)
{
System.out.println("*");
}
System.out.println();
}
}
}

Star Pattern 2:

* * * * * * * *
* * * * * * *
* * * * * * 
* * * * *
* * * *

* * * 
* *
*

Solution:

class Demo2
{
public static void main(String args[])
{
for(int i =8; i>=1; i--)
{
for(int j = 1; j<=i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Start Pattern 3 :

                  *
                ***
              *****
            *******
          *********
        ***********
      *************

Solution:

class Demo3
{
public static void main(String args[])
{
int number = 7;
int count = number - 1;
for(int k = 1; k<=number; k++)
{
for(int i = 1; i<=count; i++)
System.out.print(" ");
count--;
for(int i = 1; i<=2*k-1; i++)
System.out.print("*");
System.out.println();
}
}
}

Star Pattern 4:

                *
              **
            ***
          ****
        *****

Solution:

class Demo4
{
public static void main(String args[])
{
for(int i = 5; i>=1; i--)
{
for(int j = 1; j<i; j++)
{
System.out.print(" ");
}
for(int k = 5; k>=i; k--)
{
System.out.print("*");
}
System.out.println("");
}
}
}

Star Pattern 5:


*****
  ****
    ***
      **
        *

Solution:


class Demo5
{
public static void main(String args[])
{
for(int i = 5; i>=1; i--)
{
for(int j = 5; j>i; j--)
{
System.out.print(" ");
}
for(int k = 1; k<=i; k++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

Star Pattern 6:

                   *
                 ***
               *****
             *******
           *********
             *******
               *****
                 ***
                   * 

Solution:

class Demo6
{
public static void main(String args[])
{
for(int i = 1; i<=5; i++)
{
for(int j = i; j<5; j++)
{
System.out.print(" ");
}
for(int k = 1; k<(i*2); k++)
{
System.out.print("*");
}
System.out.println("");
}
for(int i = 4; i>=1; i--)
{
for(int j = 5; j>i; j--)
{
System.out.print(" ");
}
for(int k = 1; k<(i*2); k++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

Star Pattern 7:

*********
 *******
   *****
     ***
       * 

Solution:

class Demo7
{
public static void main(String args[])
{
for(int i = 5; i>=1; i--)
{
for(int j = 5; j>i; j--)
{
System.out.print(" ");
}
for(int k = 1; k<(i*2); k++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

I hope, this star pattern in java will help you to increase your logical skill so that you can make your own desired star pattern programs in java.


Share:

Wednesday, 16 August 2017

Serialization In Java


What is Serialization In Java?

Serialization In Java

Serialization in java is a mechanism to convert an object into a byte stream so that we can send over the network or save it as file or store into a database. Java serialization was introduced in JDK 1.1 and it is the most important feature of java.

Deserialization in java is just opposite of serialization in java. 

The main purpose of serialization in java is to converting an state of an object into a stream of bytes.

Here we will learn serialization and deserialization in java with example so that you can understand better.

Serialization is used in RMI, JPA, Hibernate etc.


Advantages of Serialization In Java

  1. It is mainly used to travel the object on the network.
  2. The entire process is JVM independent because object serialized on one platform and deserialized on an entirely different platform.


java.io.Serializable Interface

A class must implement java.io.Serializable interface to be eligible for serialization.

For example:

import java.io.Serializable;
class Student implements Serializable
{
int rollno;
String name;
Student(int rollno, String name)
{
this.rollno = rollno;
this.name = name;
}
}

In the above example, Student class implements Serializable interface . Now the object of the Student class can be converted into stream.

In java ObjectOutputStream and ObjectInputStream both are high-level stream that contains methods for serialization and deserialization of an object.


Java ObjectOutputStream Class

It is used to write primitive data types and objects to an output stream. Only objects that supports Serializable interface.

Methods of ObjectOutputStream

There are some methods of this class.

(1) public final void writeObject(Object obj)throws IOException 

This method writes the specified object to the ObjectOutputStream.

(2) public void flush() throw IOException

This method flushes the current output stream.

(3) public void close() throw IOException

This method closes the current output stream.



Java Serialization Example

In this class, we are going to serialize the object of Student class by the help of writeObject() method of ObjectOutputStream class because it provides the functionality to serialize the object. We will save the data of an object in a particular file name anu.txt.

import java.io.Serializable;
import java.io.*;
class Sample
{
public static void main(String args[])throws Exception
{
Student s = new Student(32, "amir");
FileOutputStream fout = new FileOutputStream(" anu.txt ");
ObjectOutputStream out = new ObjectOutputStream(fout);

out.writeObject(s);
out.flush();
System.out.println("task complete");
}
}

Output: task complete


Deserialization In Java

Deserialization in java is used to converting stream to object. Java deserialization is opposite of serialization.

Java ObjectInputStream Class

This class is used to deserialized the object of a class.

Methods of ObjectInputStream

There are some methods of output stream class which will help in deserializing an object.

(1) public final void readObject()throws IOException, ClassNotFoundException

This method read the object from the input stream.

(2) public void close()throws IOException

This method close object input stream.


Java Deserialization Example

This is simple example of deserialization of an object.

import java.io.Serializable;
import java.io.*;
class Sample1
{
public static void main(String args[])throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("anu.txt"));
Student ss = (Student)ois.readObject();
System.out.println(ss.rollno+" "+ss.name);
ois.close();
}

}

Output: 32 amir


Share:

Tuesday, 15 August 2017

What is Servlet In Java


Java Servlet

Here we will cover in detail what is servlet in java and why we used java servlet and what is the features of the servlet. So let's start

Java servlet is a technology which is used to create a web application. Web application means a collection of web pages e.g websites.

Servlet is server side programming or java program which is executed in JVM on the web server, In other words, it executes on the web server.

Servlet technology is used to create dynamic web pages. Dynamic web pages means, which is not same for all users e.g suppose there are 100 students and they want to see their marks online. Every student will get different-different marks according to their filling enrollment numbers on dynamic web pages.

There are two types of pages :

Dynamic Web Page: Which is not same for all users.

Static Web Page: Which is same for all users.

You can define servlet in many ways and these are :

  • Servlet is a JEE technology.
  • Servlet is a technology which is used to create a web application.
  • Servlet is an API in java i.e java servlet API provides some classes and interfaces for generating dynamic web pages or create a web application.
  • Servlet is an interface, if you want to create servlet then you have to implement this servlet interface.
  • Servlet is a class that extends the capabilities of servers and respond to the incoming request.

          
                                                   

                                                                                
There are some other technologies which is used to create a web application and make dynamic web pages e.g JSP(Java Server Page) but there is some difference between Servlet and JSP. We will learn in later, what is jsp in java and what is jsp life cycle etc.


Common Gateway Interface - CGI

Common gateway interface is a specification for transferring information between a world wide web server and CGI programs. A CGI program is any program designed to accept and return data that conforms to the CGI specification.


The program could be written in any programming language including perl, java, c, or visual basic.

Common gateway interface programs are the most common way to interact dynamically with the user.


Disadvantages of CGI

  • For each request, it starts a process and the web server is limited to start processes.
  • Common gateway interface uses platform dependent language e.g c, c++, etc.

Advantages of Servlet Over CGI

  • For handling multiple requests the web container creates the threads and we know very well that thread is light weight, share common memory areas whereas process is heavy weight and take separate memory area.

Features of Servlet

Servlet in java has many features e.g
  • Good Performance: Good performance of servlet is it creates a thread for handling each request not process(like CGI).
  • Portability: It has great portability because it uses java programming language and java is a robust language.
  • Secure : It uses java and java is a secure language.
  • Using oops: Servlet can take advantage of the object oriented programming Because servlet uses java 

I hope this introduction to java servlets will help you. In next java servlet tutorials, we will learn what is servlet life cycle and servlet methods etc. 
Share:

Sunday, 13 August 2017

Difference Between String and StringBuffer


Difference Between String and StringBuffer with Example

Difference Between String and StringBuffer

Here we will discuss what is the difference between String and StringBuffer in java. This is the most important topic for java interview.

So let's start with java String.

Java String

  • If the data which enclosed within double quote(" ") is by default treated as a String class.
  • String class object is immutable i.e after creating a string object, we cannot modify or update this string object.
  • The performance of String is slow because it creates a new instance every time when we concat a string.
  • String class consumes more memory when you concat too many string because every time it creates a new instance.
  • By default, no additional character memory space is created when we created String class object.
  • String class overrides the equals() method of Object class.

Java String Example

This the simple example of String class. Java String class belongs to the java.lang package.

class StringExample
{
public static void main(String args[])
{
String s = "India is a nice country";
System.out.println(s);
}
}

Output: India is a nice country 

Another Example of String with concat() Method

As we know that String is immutable i.e it cannot be modified or changed after it created, but if we will concat string(add another string) then it will make a new instance but still original string will not change.

class ConcatExample
{
public static void main(String args[])
{
String s = "Micro";
s.concat("soft");//concat() combines specified string at the end 
System.out.println(s);
}
}

Output: Micro

In the above example, Here s variable will print Micro not Microsoft but here it will make another new instance "Microsoft" in constant pool memory.



Java StringBuffer

  • If the data which enclosed within double quote(" ") is not by default treated as StringBuffer class.
  • StringBuffer class object is mutable i.e it can be modified or update after it created.
  • The performance of StringBuffer class is fast in comparison of String because it can be modified or change in existing string buffer object.
  • It consumes less memory because it doesn't create a new instance when we append another string.
  • By default, it takes 16 characters memory space when we create StringBuffer object.
  • StringBuffer class does not override the equals() method of Object class.

There are some StringBuffer methods in java e.g append() method, insert() method, replace() method etc.

There are given below some StringBuffer examples.

Java StringBuffer class is thread safe, StringBuffer is thread safe means, multiple threads cannot access it simultaneously.

StringBuffer append() Example

The append() method of StringBuffer class is used to add the new string to the original string.

class Demo
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("hello");
sb.append(" rahul");
System.out.println(sb);
}
}

Output: hello rahul


StringBuffer insert() Example

This is another StringBuffer example where we will use insert() method.

class Test
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("hello");
sb.insert(1,"world");
System.out.println(sb);
}
}

Output: hworldello

So there are many differences between String and StringBuffer and StringBuilder class in java.

I hope you have learned well StringBuffer vs String in this post.


Share:

Tuesday, 8 August 2017

Number Pattern Programs In Java


Java Number Pattern Programs

Number Pattern Programs

This article is all about of number pattern programs in java. This number pattern program is also a very important from the interview point of view.

Here we will see many java number pattern programs examples.

So let's start with it.

Number Pattern 1 :

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Solution :

class Demo1
{
public static void main(String args[])
{
for(int i = 1; i<=5; i++)
{
for(int j = 1; j<=i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

Number Pattern 2 :

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Solution :


class Demo2
{
public static void main(String args[])
{
for(int i = 1; i<=5; i++)
{
for(int j = 1; j<=i; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}

Number Pattern 3 :

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

Solution :


class Demo3
{
public static void main(String args[])
{
for(int i = 1; i<=5; i++)
{
for(int j = i; j>=1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}


Number Pattern 4 :

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Solution :


class Demo4
{
public static void main(String args[])
{
for(int i = 5; i >= 1; i--)
{
for(int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

Here we are performing number pattern programs in java using for loop. In other words, here we are using nested loop i.e loop inside another loop.

Number Pattern 5 :

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Solution :


class Demo5
{
public static void main(String args[])
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <=i; j++)
{
System.out.print(j+" ");
}
for(int j = i-1; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

Number Pattern 6 :

1
2 6 
3 7 10 
4 8 11 13
5 9 12 14 15

Solution :


class Demo6
{
public static void main(String args[])
{
for(int i = 1; i <= 5; i++)
{
int num = i;
for(int j = 1; j <=i; j++)
{
System.out.print(num+" ");
num = num+5-j;
}
System.out.println();
}
}
}

here another number pattern program in java which will help you in a java programming interviews.

Number Pattern 7 :

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Solution :


class Demo7
{
public static void main(String args[])
{
//upper half of the pattern
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <=i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}

//lower half of the pattern
for(int i = 5-1; i >= 1; i--)
{
for(int j = 1; j <=i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

Number Pattern 8 :

8 7 6 5 4 3 2 1
8 7 6 5 4 3 2
8 7 6 5 4 3
8 7 6 5 4  
8 7 6 5
8 7
8

Solution :


class Demo8
{
public static void main(String args[])
{
for(int i = 1; i <= 8; i++)
{
for(int j = 8; j >=i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

Number Pattern 9 :

1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8

Solution :


class Demo9
{
public static void main(String args[])
{
for(int i = 8; i >= 1; i--)
{
for(int j = 1; j <=i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
for(int i = 2; i <= 8; i++)
{
for(int j = 1; j <=i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

Number Pattern 10 :

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1

Solution :


class Demo10
{
public static void main(String args[])
{
for(int i = 1; i <= 8; i++)
{
for(int j = i; j >=1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}

}
}

Here above all the java program to print of number are quite useful and all the above number pattern programs increase you logical capability.







Share:

Facebook Page Likes

Popular Posts

Translate