vik-y
5/8/2015 - 7:16 AM

Java Exam minor things

Java Exam minor things

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.io.*;

import javax.naming.ldap.SortControl;

public class practice {

	/**
	 * @param args
	 */
	int val;
	
	practice(int a){
		val=a;
	}
	
	public static void print(Object a){
		System.out.println(a);
	}
	
	/*public static void print(int a){
		System.out.println(a);
	}*/
	
	@Override
	public String toString() {
		return ""+val;
	}
	
	public static <E> void printArray(E[] arr){
		for(E element: arr){
			System.out.printf("%s", element);
		}
	}//A single method to print different kinds of array
	
	public static void main(String[] args) throws FileNotFoundException {
		//main function is declared static becuase it has nothing to do with the class itself
		//all the members which are independent of the class and do not an instance of it to 
		//run should be declared static 
		// TODO Auto-generated method stub
		practice p = new practice(1);
		String s1= new String("vik");
		String s2 = "vik";
		
		if(s1==s2){
			print("true");
		} //This doesn't work 
		
		
		String s3 ="vik";
		String s4 = "vik";
		
		if(s3==s4){
			print("true");
		}//But this works perfectly fine, reason? 
		
		
		/*
		 * Array
		 * arr.length() 
		 * returns length of an array
		 * 
		 */
		
		final String test = new String("v");
		//here test+="something" won't because test is declared as a final variable
		//final is Similar to keyword const in C++
		//How to use String Buffer? 
		
		/*
		 * If a class is declared final 
		 */
		
		/*
		 * Abstract classes are classes that contain one or more abstract 
		 * methods. An abstract method is a method that is declared, but 
		 * contains no implementation. Abstract classes may not be instantiated, 
		 * and require subclasses to provide implementations for the abstract 
		 * methods.
		 */
		
		//Please note: Default variables and methods are not available to 
		//a inherited/derived class outside the current package.
		
		/*
		 * ABSTRACT 
		 * 
		 * Similar to pure virtual functions in C++
		 * When a abstract class is declared - it's not definied and left
		 * for the subclasses to write the definition 
		 */
		
		/*
		 * Difference between overloading and overriding 
		 * 
		 * Overloading is basically writing the same function 
		 * agian with different parameter values
		 * 
		 * Overoading functions can't have different values 
		 * 
		 * Overriding is writing a complete method again in a derived subclass 
		 * 
		 */
		
		B b = new B();
		b.show(); //b.show() is called
		
		A a = b; //Implicit type casting
		a.show(); //b.show() is called
		
		A a1 = (A)b; //Equivalent to 'A a = b'
		a1.show(); //b.show() is called
		
		//B b1 = a; //Erorr,caught at compile time
		B b1 = (B)a; //Not an error
		
		b1.show(); //b.show() is called
		A a2 = new A();
		
		//B b2 = (B)a2; //Erorr, caught at run time
		
		/*
		 * Autoboxing and unboxing in java 
		 * 
		 * in Java everything is an object. Even the primitives but
		 * just to make the code look clean they have used the concept of Autoboxing 
		 * and unboxing
		 * 
		 */
		
		/*
		 * NOTE: Java doesn't support multiple inheritence 
		 * Interfaces 
		 * A set of abstract methods 
		 * 
		 * An interface can extend another interface
		 * 
		 * By default whatever method you write inside an interface are abstract
		 * 
		 * An interface can extend more than one parent interfaces
		 */
		
		ArrayList<Integer> vik = new ArrayList<Integer>(100);
		Integer i1 = new Integer(10);
		
		i1=20; //Autoboxing and Unboxing 
		
		print(i1);
		vik.add(10);
		vik.add(20);
		vik.add(30);
		
		
		/*
		class ReverseSorter implements Comparator{
			@Override
			public int compare(Object obj1, Object obj2){
				
			}	
			
		}
		
		ReverseSorter c = new ReverseSorter();
		
		Collections.sort(vik, c);
		
		for(int i=0;i<vik.size();i++){
			print(vik.get(i));
			//Using array list
		}
		
	*/
		Scanner in = new Scanner(System.in);
		/*while(in.hasNext()){
			System.out.println(in.next());
		}
		in.next() String 
		in.nextInt() Next Integer
		in.nextDouble() Next double
		in.nextFloat() Next Float 
		*/
		
		BufferedReader br = null;
		 
		try {
 
			String sCurrentLine;
 
			br = new BufferedReader(new FileReader("/home/bobo/Desktop/MySafeRoad.java"));
 
			while ((sCurrentLine = br.readLine()) != null) {
				System.out.println(sCurrentLine);
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		}
		//Printing from a file done here
		
		/*Multithreading */
		
		ThreadTest newthreadtest = new ThreadTest();
		newthreadtest.start();
		
		//Write the code which runs on .start method in run() function 
		
		/*while(true){
			System.out.println("Printing in main");
			try{
				Thread.sleep(1000);
			}
			catch(InterruptedException e){
				e.printStackTrace();
			}
		}
		This verifies that multithreading is working here 
		*/
		
		
		//A sinlge method printArray is written using java generics to print various types of array
		Integer[] intArray = { 1, 2, 3, 4, 5 };
        Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
        Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };

        System.out.println( "Array integerArray contains:" );
        printArray( intArray  ); // pass an Integer array

        System.out.println( "\nArray doubleArray contains:" );
        printArray( doubleArray ); // pass a Double array

        System.out.println( "\nArray characterArray contains:" );
        printArray( charArray ); // pass a Character array
		
	}
	
}