package packA;

public class sam1 
{
 public static final double DIAMETER = 12756.32; // kilometers
 
 public static void hithere()
 {
	 System.out.println("Hi There");
 } 
}


package packB;
import packA.sam1;

public class sam2 
{
	public static void main(String args[])
	{
		sam2 objsam2 = new sam2();
		objsam2.halfway();		
	}
	
	public void halfway()
	{ 
		sam1.hithere();
		System.out.println(sam1.DIAMETER/2.0);
	}
}

-Two Packages – packA and packB – While importing static from packA to packB you should either use import packA.sam1; or import static packA.sam1.*;

Using import static packA.sam1; will not allow to access elements in package

If you use import packA.sam1;
className.StaticVariableName

If you use import static packA.sam1.*;
StaticVariableName

The below code generates no compilation error in eclipse but throws error during Runtime.

public class Animal
{
  public void eat(){}
}
public class Dog extends Animal
{
  public void eat(){}
  public void main(String[] args)
  {
    Animal animal=new Animal();
    Dog dog=(Dog) animal;
  }
}

Output

Exception in thread "main" java.lang.ClassCastException: com.mugil.wild.Animal cannot be cast to com.mugil.wild.Dog
	at com.mugil.wild.Dog.main(Dog.java:12)

By using a cast you’re essentially telling the compiler “trust me. I’m a professional, I know what I’m doing and I know that although you can’t guarantee it, I’m telling you that this animal variable is definitely going to be a dog

Because you’re essentially just stopping the compiler from complaining, every time you cast it’s important to check that you won’t cause a ClassCastException by using instanceof in an if statement.

Generally, downcasting is not a good idea. You should avoid it. If you use it, you better include a check:

Animal animal = new Dog();

if (animal instanceof Dog)
{
Dog dog = (Dog) animal;
}

public class ClassA 
{	
	public void MethodA()
	{
		System.out.println("This is Method A");
	}
}

public class ClassB extends ClassA
{	
	public void MethodB()
	{
		System.out.println("I am Method in Class B");
	}
}


public class ClassC 
{
	public static void main(String[] args) 
	{
		ClassA objClassA1 = new ClassA();
		ClassB objClassB2 = new ClassB();
		
		//Child Class of Parent Type can be Created  
		ClassA objClassB1 = new ClassB();
		
		//Assigning a Parent class Type to Child Class is Not Allowed  
		//Casting Should be Carried out
		ClassB objClassA2 = (ClassB) new ClassA();
		
		objClassA1.MethodA();
		objClassB2.MethodA();
		objClassB2.MethodB();
		
		objClassB1.MethodA();
	}
}

The following short quiz consists of 4 questions and will tell you whether you are qualified to be a professional. The questions are NOT that difficult.

1. How do you put a giraffe into a refrigerator?

Correct Answer: Open the refrigerator, put in the giraffe, and close the door. This question tests whether you tend to do simple things in an overly complicated way.

2. How do you put an elephant into a refrigerator?

Did you say, Open the refrigerator, put in the elephant, and close the refrigerator?

Wrong Answer.

Correct Answer: Open the refrigerator, take out the giraffe, put in the elephant and close the door. This tests your ability to think through the repercussions of your previous actions.

3. The Lion King is hosting an animal conference. All the animals attend…. except one. Which animal does not attend?

Correct Answer: The Elephant. The elephant is in the refrigerator. You just put him in there. This tests your memory.

Okay, even if you did not answer the first three questions correctly, you still have one more chance to show your true abilities.

4. There is a river you must cross but it is used by crocodiles, and you do not have a boat. How do you manage it?

Correct Answer: You jump into the river and swim across. Have you not been listening? All the crocodiles are attending the Animal Meeting. This tests whether you learn quickly from your mistakes.

In java String Builder Should be Used in case you need to perform concatenate more string together.

i.e

 public String toString()
 {
    return  a + b + c ;
 }

For the above code using + will be converted to

a = new StringBuilder()
    .append(a).append(b).append(c)
    .toString();

For the above case you can use concat as below but since + will be converted as String Builder its better to use + rather than concat.

 public String toString()
 {
    return  a.concat(b).concat(c);
 }

The key is whether you are writing a single concatenation all in one place or accumulating it over time.

There's no point in explicitly using StringBuilder.

But if you are building a string e.g. inside a loop, use StringBuilder.

To clarify, assuming that hugeArray contains thousands of strings, code like this:

...
String result = "";
for (String s : hugeArray) {
    result = result + s;
}

It should be as below

 ...
StringBuilder sb = new StringBuilder();

for (String s : hugeArray) {
    sb.append(s);
}
String result = sb.toString();

Common elements in two arrays javascript

arrNum1 = new Array(1,2,3);
arrNum2 = new Array(2,3,4,5);
arrNum3 = new Array();

arrNum3 =  intersect_safe(arrNum1, arrNum2);

for(i=0;i<arrNum3.length;i++)
 alert(arrNum3[i]);

function intersect_safe(a, b)
{
  var ai=0, bi=0;
  var result = new Array();

   while(ai<a.length && bi < b.length )
  {
     if(a[ai] != b[bi])
     { 
       bi++; 
     }
     else
     {
       result.push(a[ai]);
       ai++;
       bi++;
     }
  }

  return result;
}
var arr = new Array();
arr[0] = 100;
arr[1] = 0;
arr[2] = 50;

Array.prototype.max = function() {
  return Math.max.apply(null, this)
}

Array.prototype.min = function() {
  return Math.min.apply(null, this)
}

var min = Math.min.apply(null, arr),
    max = Math.max.apply(null, arr);

To find the Maximum value in Array Use Below

 var max_of_array = Math.max.apply(Math, array);