Thursday, 7 December 2017

What is Java List and List Iterator?

Learning Java Programming Made Easy

List And List Iterator


Java List Interface:

List Interface is the subinterface of Collection.It contains methods to insert and delete elements in index basis.It is a factory of ListIterator interface.

Java List Example

  1. import java.util.*;  
  2. public class ListExample{  
  3. public static void main(String args[]){  
  4. ArrayList<String> al=new ArrayList<String>();  
  5. al.add("Amit");  
  6. al.add("Vijay");  
  7. al.add("Kumar");  
  8. al.add(1,"Sachin");  
  9. System.out.println("Element at 2nd position: "+al.get(2));  
  10. for(String s:al){  
  11.  System.out.println(s);  
  12. }  
  13. }  
  14. }


Java ListIterator Interface:

ListIterator Interface is used to traverse the element in backward and forward direction.

Example of ListIterator Interface

  1. import java.util.*;  
  2. public class TestCollection8{  
  3. public static void main(String args[]){  
  4. ArrayList<String> al=new ArrayList<String>();  
  5. al.add("Amit");  
  6. al.add("Vijay");  
  7. al.add("Kumar");  
  8. al.add(1,"Sachin");  
  9. System.out.println("element at 2nd position: "+al.get(2));  
  10. ListIterator<String> itr=al.listIterator();  
  11. System.out.println("traversing elements in forward direction...");  
  12. while(itr.hasNext()){  
  13. System.out.println(itr.next());  
  14. }  
  15. System.out.println("traversing elements in backward direction...");  
  16. while(itr.hasPrevious()){  
  17. System.out.println(itr.previous());  
  18. }  
  19. }  
  20. }




What is Encapsulation in Java Programming?

Learning Java Programming Made Easy

Encapsulation in Java

Encapsulation in java is a process of wrapping code and data together into a single unit, for example capsule i.e. mixed of several medicines.


We can create a fully encapsulated class in java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it. The Java Bean class is the example of fully encapsulated class.


Advantage of Encapsulation in java

By providing only setter or getter method, you can make the class read-only or write-only. It provides you the control over the data. Suppose you want to set the value of id i.e. greater than 100 only, you can write the logic inside the setter method.


  1. //save as Student.java  
  2. package com.funlearn;  
  3. public class Student{  
  4. private String name;  
  5.    
  6. public String getName(){  
  7. return name;  
  8. }  
  9. public void setName(String name){  
  10. this.name=name  
  11. }  
  12. }  



  1. //save as Test.java  
  2. package com.funlearn;  
  3. class Test{  
  4. public static void main(String[] args){  
  5. Student s=new Student();  
  6. s.setName("vijay");  
  7. System.out.println(s.getName());  
  8. }  
  9. }  

Wednesday, 6 December 2017

How Sting Joiner Class Works in JAVA Programming

Learning Java Programming Made Easy


Java StringJoiner



Java added a new final class StringJoiner in java.util package. It is used to construct a sequence of characters separated by a delimiter. Now, you can create string by passing delimiters like comma(,), hyphen(-) etc. You can also pass prefix and suffix to the char sequence.



Java StringJoiner Example


  1. // importing StringJoiner class  
  2. import java.util.StringJoiner;  
  3. public class StringJoinerExample {  
  4.     public static void main(String[] args) {  
  5.         StringJoiner joinNames = new StringJoiner(","); // passing comma(,) as delimiter   
  6.           
  7.         // Adding values to StringJoiner  
  8.         joinNames.add("Rahul");  
  9.         joinNames.add("Raju");  
  10.         joinNames.add("Peter");  
  11.         joinNames.add("Raheem");  
  12.                   
  13.         System.out.println(joinNames);  
  14.     }  
  15. }  


Java StringJoiner Example: adding prefix and suffix



  1. // importing StringJoiner class  
  2. import java.util.StringJoiner;  
  3. public class StringJoinerExample {  
  4.     public static void main(String[] args) {  
  5.         StringJoiner joinNames = new StringJoiner(",""[""]");   // passing comma(,) and square-brackets as delimiter   
  6.           
  7.         // Adding values to StringJoiner  
  8.         joinNames.add("Rahul");  
  9.         joinNames.add("Raju");  
  10.         joinNames.add("Peter");  
  11.         joinNames.add("Raheem");  
  12.                   
  13.         System.out.println(joinNames);  
  14.     }  
  15. }  


StringJoiner Example: Merge Two StringJoiner


  1. // importing StringJoiner class  
  2. import java.util.StringJoiner;  
  3. public class StringJoinerExample {  
  4.     public static void main(String[] args) {  
  5.   
  6.         StringJoiner joinNames = new StringJoiner(",""[""]");   // passing comma(,) and square-brackets as delimiter   
  7.           
  8.         // Adding values to StringJoiner  
  9.         joinNames.add("Rahul");  
  10.         joinNames.add("Raju");  
  11.   
  12.         // Creating StringJoiner with :(colon) delimiter  
  13.         StringJoiner joinNames2 = new StringJoiner(":""[""]");  // passing colon(:) and square-brackets as delimiter   
  14.           
  15.         // Adding values to StringJoiner  
  16.         joinNames2.add("Peter");  
  17.         joinNames2.add("Raheem");  
  18.   
  19.         // Merging two StringJoiner  
  20.         StringJoiner merge = joinNames.merge(joinNames2);   
  21.         System.out.println(merge);  
  22.     }  
  23. }

Monday, 4 December 2017

How to use URLConnection class in Java Programming

Learning Java Programming Made Easy

Java Connection Class:

The Java URLConnection class represents a communication link between the URL and the application. This class can be used to read and write data to the specified resource referred by the URL.

The openConnection() method of URL class returns the object of URLConnection class. Syntax:

public URLConnection openConnection()throws IOException{}

Example:


  1. import java.io.*;  
  2. import java.net.*;  
  3. public class URLConnectionExample {  
  4. public static void main(String[] args){  
  5. try{  
  6. URL url=new URL("http://www.javatpoint.com/java-tutorial");  
  7. URLConnection urlcon=url.openConnection();  
  8. InputStream stream=urlcon.getInputStream();  
  9. int i;  
  10. while((i=stream.read())!=-1){  
  11. System.out.print((char)i);  
  12. }  
  13. }catch(Exception e){System.out.println(e);}  
  14. }  
  15. }

How Does URL Programming works in Java Development

Learning Java Programming Made Easy


Java URL:

The Java URL class represents an URL. URL is an acronym for Uniform Resource Locator. It points to a resource on the World Wide Web. For example:


A URL contains many information:
  1. Protocol: In this case, http is the protocol.
  2. Server name or IP Address: In this case, www.javatpoint.com is the server name.
  3. Port Number: It is an optional attribute. If we write http//ww.javatpoint.com:80/sonoojaiswal/ , 80 is the port number. If port number is not mentioned in the URL, it returns -1.
  4. File Name or directory name: In this case, index.jsp is the file name.
MethodDescription
public String getProtocol()it returns the protocol of the URL.
public String getHost()it returns the host name of the URL.
public String getPort()it returns the Port Number of the URL.
public String getFile()it returns the file name of the URL.
public URLConnection openConnection()it returns the instance of URLConnection i.e. associated with this URL.


  1. //URLDemo.java  
  2. import java.io.*;  
  3. import java.net.*;  
  4. public class URLDemo{  
  5. public static void main(String[] args){  
  6. try{  
  7. URL url=new URL("http://www.javatpoint.com/java-tutorial");  
  8.   
  9. System.out.println("Protocol: "+url.getProtocol());  
  10. System.out.println("Host Name: "+url.getHost());  
  11. System.out.println("Port Number: "+url.getPort());  
  12. System.out.println("File Name: "+url.getFile());  
  13.   
  14. }catch(Exception e){System.out.println(e);}  
  15. }  
  16. }

How Does Java Socket programming works

Learning Java Programming Made Easy


Java Socket Programming:

Java Socket programming is used for communication between the applications running on different JRE.Java Socket programming can be connection-oriented or connection-less. Socket and ServerSocket classes are used for connection-oriented socket programming and DatagramSocket and DatagramPacket classes are used for connection-less socket programming. The client in socket programming must know two information:
  1. IP Address of Server, and
  2. Port number.
A socket is simply an endpoint for communications between the machines. The Socket class can be used to create a socket.

Server-Side Program:

MyServer.java

  1. import java.io.*;  
  2. import java.net.*;  
  3. public class MyServer {  
  4. public static void main(String[] args){  
  5. try{  
  6. ServerSocket ss=new ServerSocket(6666);  
  7. Socket s=ss.accept();//establishes connection   
  8. DataInputStream dis=new DataInputStream(s.getInputStream());  
  9. String  str=(String)dis.readUTF();  
  10. System.out.println("message= "+str);  
  11. ss.close();  
  12. }catch(Exception e){System.out.println(e);}  
  13. }  


Client-Side Program:

MyClient.java

  1. import java.io.*;  
  2. import java.net.*;  
  3. public class MyClient {  
  4. public static void main(String[] args) {  
  5. try{      
  6. Socket s=new Socket("localhost",6666);  
  7. DataOutputStream dout=new DataOutputStream(s.getOutputStream());  
  8. dout.writeUTF("Hello Server");  
  9. dout.flush();  
  10. dout.close();  
  11. s.close();  
  12. }catch(Exception e){System.out.println(e);}  
  13. }  



Charactor_At_Position

public class Char_At_Position { public static void main(String[] args) { String str = "Wankhede"; String rev=""...