Posts

Showing posts from July, 2016

HTTPURLConnection WebService Client

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.ProtocolException; import java.net.URL; import java.net.URLConnection; public class Test1 { private static final String serviceUrl = "http://www.webservicex.net/stockquote.asmx"; public static void main(String args[]) { try { final URL url = new URL(serviceUrl); StringBuilder message = new StringBuilder(" "); message.append(" "); message.append(" "); message.append(" "); message.append(" ").append("@").append(" "); message.append(" "); message.append(" "); message.append(" "); // Create the connection where we're going to send the file. URL...

Difference between abstract and interface

 interfaces when you want a full implementation and use abstract classes when you want partial pieces for your design (for reusability) 1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. 2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance. 3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables. 4) Abstract class can have static methods, main method and constructor. Interface can't have static methods, main method or constructor. 5) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class. 6) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface. 7) Example: public abstract class Shape{ public abstract void draw(); } Example: public interface Drawable{ void draw(); } Simply, abstract class ...

Difference between comparable and comparator

java.util.Comparator  java.lang.Comparable Some time you write code to sort object of a class for which you are not the original author, or you don't have access to code. In these cases you can not implement Comparable and Comparator is only way to sort those objects. 1) Comparable provides single sorting sequence. In other words, we can sort the collection on the basis of single element such as id or name or price etc.        Comparator provides multiple sorting sequence. In other words, we can sort the collection on the basis of multiple elements such as id, name and price etc. 2) Comparable affects the original class i.e. actual class is modified.                          Comparator doesn't affect the original class i.e. actual class is not modified. 3) Comparable provides compareTo() method to sort elements. Comparator provides compare() method to sort elements. 4) Comparable is found in jav...

Difference between collection and collections

Image
Collection is a top level interface of java collection framework where as Collections is an utility class. Collection : The root interface of Java Collections Framework. Collection is an interface containing List, Set and Queue. Collections : A utility class that is a member of the Java Collections Framework. Collections is a class containing useful methods like Collections.sort() and Collections.synchronizedlist(), etc. Collection is a root level interface of the Java Collection Framework. Most of the classes in Java Collection Framework inherit from this interface. List, Set and Queue are main sub interfaces of this interface. Map interface, which is also a part of java collection framework, doesn’t inherit from Collection interface. Collection interface is a member of java.util package. Collections is an utility class in java.util package. It consists of only static methods which are used to operate on objects of type Collection. Collections.max() This method returns maximum elemen...

Java 7 Features

Binary Literals Strings in switch Statement Try with Resources or ARM (Automatic Resource Management) Multiple Exception Handling underscore in literals Type Inference for Generic Instance Creation using Diamond Syntax Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods Try-with-resources statement this: BufferedReader br = new BufferedReader(new FileReader(path)); try {    return br.readLine(); } finally {    br.close(); } becomes: try (BufferedReader br = new BufferedReader(new FileReader(path)) {    return br.readLine(); } You can declare more than one resource to close: try (    InputStream in = new FileInputStream(src);    OutputStream out = new FileOutputStream(dest)) {  // code } Underscores in numeric literals int one_million = 1_000_000; Strings in switch String s = ... switch(s) {  case "quux":     processQuux(s);     // fall-through   case "fo...

My Lab Manual

import java.io.*; class AppendFile {   public static void main(String[] args) {     try {       FileOutputStream out = new FileOutputStream(new File("new.txt"),           true);       String str = "There is always a way-if you're committed.\n";       //while(){ out.write(str.getBytes()); //}     out.close();       System.out.println("String is appended.");     } catch (IOException e) {     }   } }/* ----------------------------------------------------------------------------- (#)Vowels file.java (#)5 aug (#) */ // importing packages import java.io.FileReader; import java.io.BufferedReader; public class lab$1{ public static void main(String args[])throws Exception{ System.out.println("vowels program"); FileReader fr= new FileReader("lab$1.java"); BufferedReader br= new BufferedReader(fr); int c; int count=0; while((c=br.read())!=-1){ char temp=(char)c; Sy...

Array and Object Creation - JavaScript

var arr =[1,2,3]; var arr = new Array(1,2,3); var arr = new Array(); //empty Array arr.push(10); arr.pop(); var obj = new Object(); obj.test ="hello"; var obj= { "test" : "hello"}; function myFunction() {     alert('It works!'); } var name = 'myFunction'; window[name].call(); var myObject = {     firstName:"John",     lastName: "Doe",     fullName: function(i) {         return this.firstName + " " + this.lastName+i;     } }; document.getElementById("demo").innerHTML = myObject.fullName(1); //John Doe1 //Passing value via constructor function myFunction(arg1, arg2) {     this.firstName = arg1;     this.lastName  = arg2; } alert(JSON.stringify(new myFunction("John","Doe"))) var x = new myFunction("John","Doe") //{"firstName":"John","lastName":"Doe"} alert(JSON.stringify(new myFunction())) //{} alert(JSON.stringify(new myFunc...