Posts

Showing posts from December, 2020

Variables in java

 Based on the type of value represented by the variable all variables are divided into two types : Primitive variables: can be used to represent primitive values. Example: int x =10; Reference variables: can be used to refers to objects Example: Studen s = new Studen(), s is pointing to object. Based on the position of declaration and behavior variables are divided into three types: Instance : the value of the variable is varied from object to object such type of variable is called instance variables. for every object separate copy of the instance, a variable will be created. Instance variable should be declared within a class directly but outside of any method or block or constructor. instance variable will be created at the time of object creation and destroyed at the time of object destruction hence the scope of the instance variable is exactly the same as the scope of the object. The instanced variable will be stored in the heap memory as part of the object. we cant access th...

System.out.println("Hello") - Full Explanation

 System.out.println("Hi Java"); this is used for printing value on console. in the below example System is a class that is present in java.lang package. out is a static variable in the System class that the reason we can access out variable by dot operator. println is a method that is defined in printStream class. For Example: System: System in class present in java.lang package out: is a static variable present in System class println : this is a method present in PrintStream class. class System{       static PrintStream out;         ..... }    

public static void main(String [] args) - Full Explanation

Why JVM Call main() method? JVM is a program in which the main() method is configured that is the reason JVM called the main method. we can change the name of the main() method but we need to change the method calling in JVM. Explanation of the main method : public static void main(String [] args) is a method that is called by JVM.  Public : Public is an access modifier of the main method. the main method is public because the JVM can call this method from anywhere. static : without creating an object JVM can call this method that's why the main method is static. without an existing object also JVM has to call this method and the main method is not related to any Object. void:  void is a return type of the main method, the main method not returning any value to JVM. main : this is the method name that is configured in JVM.  String[] args : these are command-line arguments. The above syntax is very strict if we performed any change we will get a Runtime exception i.e No su...

Diffrence between String and StringBuffer

String : String Objects are immutable. when we create a String object and we performed some operation on String the new reference is created. For Example :  String s1 = new String("Hello"); s1.concate("Java");/now original string is not changed. new reference is created in memory.  System.out.println("String Value"+ s1); In the above example output is "Hello" because s1 is immutable. StringBuffer : StringBuffer is mutable. when we create a StringBuffer object and we performed some operation on StringBuffer then it changes in the same object/Reference. StringBuffer sb=new StringBuffer("Hello ");   sb.append("Java");//now original string is changed   System.out.println(sb);//prints Hello Java   In the Above Example output is hello Java because sb is mutable.

Difference between final, finally, finalize

Final : final is an access modifier which can be used with class, method and variable. Variable : if you declared an variable with final than it will become a constant.you can not reassigned the value in final variable. Method : if you declared a method with final  then you can not overide this method in child class. Class : if you create a class with final then you can not extend that class.i. e we can not create a child class for final class. finally : finally is block of which is always associated whit try..catch block and its used for code cleanup. That mean clearing the object.if some database or file operation is performed in try block then the final. Block is used for cleaing i.e closing that database, file objects. finalize : finalize is an method which used for cleanup objects. The gaebage collector call finlize() method before destroying the object for clean up of that object.

Binary Search - Algorithm

Image
 Binary search is a fastest search algorithm with o(log n) time complexity.this algorithm is based on divide and conquer.the searching technique required collection in sorted meaner. this is biggest disadvantage of binary search. suppose we need to search on array then first we need to sort array and the perform search operation on that array. the solution in this case is first sort array and the use Binary search on the array. Algorithm : Step 1: initialize low = 0, high = size of collection step 2 : while low <= high then calculate the mid i.e mid = low + high/2 step 3 : if(searchValue == array[mid]){print Element found} step 4 : if(searchValue > array[mid]){low = mid + 1} otherwise high = mid -1 step 5 : Exit Programmatic Representation : while(low <= high){     mid = low+high/2;     if(x== a[mid]){          Print : Element Found.          break;      }  ...

Liner Search - Algorithm

Image
Liner search is an simple and  Sequential searching technique. this search, a sequential search where every item is checked and if match is found then it return the element other wise the search continues till the end of the collection i.e size of element.it check all the item one by one.the complexity of liner search is O(n). Algorithm : Linear Search (A,x,i,n) Step 1: Set i to 0 Step 2: if i > n then Print Element Not found and Go to step 6 Step 3: if A[i] = x then Print Element is not found and Go to step 6 Step 4: Set i to i + 1 Step 5: Repeat 2,3,4 till n or element not found Step 6: Exit Example : public class LinerSrarchExample { private static void search(int[] numberArray,int searchNumber) { int n= numberArray.length; for(int i=0;i<n;i++) { if(i>n) { System.out.println("Element not found."); } if(numberArray[i]==searchNumber) { System.out.println("Element found at "+ i +" Position"); break; } } }...