Question about Java and Android Development

+1 Abu Bakkar Siddique · June 16, 2015
public class Main {
    public static void main(String[] args){
        String newValue = "Hello Pakistan";
        String myValue = "Hello Pakistan";
        String Value1 = new String("Hello Pakistan");
        String Value2 = new String("Hello Pakistan");
        if (newValue == myValue) {
            System.out.println("They Match");
        } else {
            System.out.println("They don't match");
        if (Value1 == Value2) {
            System.out.println("They Match");
        } else {
            System.out.println("They don't Match");


Why first one says "They Match" and second one says "They Don't Match"
Can anyone explain what's going on. Thank you

Post a Reply


Oldest  Newest  Rating
+2 Shahraiz Rizwan · June 16, 2015
The first one matches because it is comparing the literal value of the string as well as the the object itself. Since the string value is the same and so is the object you get "They Match" as a return.

The second one doesn't match because the objects are not the same.... They are two different string objects. In essence the == operator first checks that the objects are the same before the actually string.

In order to get the second to return "They match" use:

System.out.println("They Match");
} else {
System.out.println("They don't Match");

(Also little advise Value1 should be written as value1. Also Value2 should be value2)

For more information about Strings check this link:
0 George Namachanja · June 17, 2015
Glad that your query has been handled satisfactorily. For more tutorials and guidelines on android development, feel free to visit, a great resource on the same.
0 Abu Bakkar Siddique · June 17, 2015
@Shahraiz Rizwan, Thanks Alot for your reply. It helped alot. :)
0 Abu Bakkar Siddique · June 23, 2015
Can someone explain in simple terms, What is interface in Java.? Thank You.
0 ashiq sultan · June 24, 2015
An interface is a class which contains fully abstract methods,
it is used to achieve multiple inheritance in java.

Real time example 
You can create an interface like BasicCar and set the basic things a car should possess and later users can create a class like Mycar implementing the interface Car and they would define the methods it contains

//---This is an Interface----
public interface BasicCar {
 int NoOfWheels();
 int NoOfSeats();
 double MaxSpeed();
 String GearType();

//--Note no method contains a method body its because that is called an interface
//and the class which will implement it will have to define these methods

Below is a class which is implementing the above interface

public MyCar implements BasicCar{

int mywheels = 4;
int mySeats = 2;
double myMaxSpeed=200;
String myGearType="Manual"

public int NoOfWheels(){
 return this.mywheels
 public int NoOfSeats(){
 return this.mySeats
 public double MaxSpeed(){
 return this.myMaxSpeed
 public String GearType(){
 return this.myGearType

//--Look all the methods above are the once which are declared in the interface

// it doesn't mean that you should only use those methods which are in the interface
//you can also create any no of methods like you normally do

public String EngineType(){

String myType="Muscle";

return myType;


then you would extend this class (MyCar) to do some other operation

Just Now think interface as a guide which gives you an idea on what are the basic things you should define and later you could carry on with your design. 

Note:You can implement any number of Interface.

Hope I'm Clear 

P.S:  I'm  just a student of Java so anyone finds mistakes in this kindly correct me.
0 Abu Bakkar Siddique · July 3, 2015
@ashiq sultan, Thank you for explaining me about interface in java. It helped alot.
0 Abu Bakkar Siddique · August 4, 2015
Interfaces are designed to support dynamic method resolution at run time. Normally,
in order for a method to be called from one class to another, both classes need to be
present at compile time so the Java compiler can check to ensure that the method
signatures are compatible. This requirement by itself makes for a static and nonextensible
classing environment. Inevitably in a system like this, functionality gets pushed up higher
and higher in the class hierarchy so that the mechanisms will be available to more and
more subclasses. Interfaces are designed to avoid this problem. They disconnect the
definition of a method or set of methods from the inheritance hierarchy. Since interfaces
are in a different hierarchy from classes, it is possible for classes that are unrelated in terms
of the class hierarchy to implement the same interface. This is where the real power of
interfaces is realized.

Can Any One explain above paragraph in simple words. Thanks.
0 Abu Bakkar Siddique · August 29, 2015

Please explain how
1. boolean sameTemp() is compatible with func() in Functional interface.
2. sameTemp() method got implemented on func() in Functional Interface.
3. count = counter(weekDayHighs, HighTemp::sameTemp, new HighTemp(89)); is working

Thank you
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator