# == vs = Question

Kevin Lamb

Greenhorn

Posts: 3

Nick George

Ranch Hand

Posts: 815

posted 11 years ago

'=' assignes one value to be another. For instance, if I say,

x=5;

that says "Se x to be 5."

'==' is a test. x==5 means "is x equal to 5?" not "make x=5."

so, if you want to have an if condition be if x is 5, you would say,

if(x==5)

so,

means, "if x is equal to 5, set x to be equal to 6."

dig?

x=5;

that says "Se x to be 5."

'==' is a test. x==5 means "is x equal to 5?" not "make x=5."

so, if you want to have an if condition be if x is 5, you would say,

if(x==5)

so,

means, "if x is equal to 5, set x to be equal to 6."

dig?

I've heard it takes forever to grow a woman from the ground

Francis Siu

Ranch Hand

Posts: 867

posted 11 years ago

Hi Kevin

The sign = is equal to the mathematics = , for example

x=1, y=2

z=x+y //The sum assign to the varaible z, and z store the value of sum

then z=3

if x=x+y

then x=3 //The same idea of previous example.

why? In mathematics, x is not equal to x=x+y, you can think the variable x is a box that you can use it to do calculation.

And how about y=x=z=1? please think about it

The sign = is equal to the mathematics = , for example

x=1, y=2

z=x+y //The sum assign to the varaible z, and z store the value of sum

then z=3

if x=x+y

then x=3 //The same idea of previous example.

why? In mathematics, x is not equal to x=x+y, you can think the variable x is a box that you can use it to do calculation.

And how about y=x=z=1? please think about it

Francis Siu

SCJP, MCDBA

Layne Lund

Ranch Hand

Posts: 3061

posted 11 years ago

I would disagree with this statement. I view == as equivalent to = in mathematics since they both test for equality. The = in Java is very different since it assigns the value of an expression to a variable, as you have illustrated:

I'm not exactly sure what you are trying to say here. In mathematics, x = x + y if and only if y = 0!

Hi Kevin

The sign = is equal to the mathematics = ,

I would disagree with this statement. I view == as equivalent to = in mathematics since they both test for equality. The = in Java is very different since it assigns the value of an expression to a variable, as you have illustrated:

for example

x=1, y=2

z=x+y //The sum assign to the varaible z, and z store the value of sum

then z=3

if x=x+y

then x=3 //The same idea of previous example.

why?

In mathematics, x is not equal to x=x+y,

I'm not exactly sure what you are trying to say here. In mathematics, x = x + y if and only if y = 0!

you can think the variable x is a box that you can use it to do calculation.

And how about y=x=z=1? please think about it

posted 11 years ago

I think this is a misleading comparison. In mathematics, the equals sign denotes a relationship. But in Java, both "=" and "==" are

*operators.*"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

Daksh Sharma

Greenhorn

Posts: 6

posted 11 years ago

Well ,

Khalid A Mughal's Book A programmers guide to Java Certification States Clearly that

== is called as a equality operator

What i want to say is that for comparison b/w 2 operands like x and y u will use a == operator rather than = operator.

Example has been shown in above posts also

1 if(x===y)

Consider this

x=3 and x==3 are not same

First statement x=3 means that the value of x is 3

x==3 is like checkinh whether x==3 or not

We can also compare objects using references with this == operator

The assignment operator (=) is used to assign a value to a variable, element of an array, or property of an object. Here are a few examples of using the assignment operator:

The equality operator (==) is used to compare two values or expressions. It is used to compare numbers, strings, Boolean values, variables, objects, arrays, or functions. The result is TRUE if the expressions are equal and FALSE otherwise. How items are compared depends on their data type

Khalid A Mughal's Book A programmers guide to Java Certification States Clearly that

== is called as a equality operator

What i want to say is that for comparison b/w 2 operands like x and y u will use a == operator rather than = operator.

Example has been shown in above posts also

1 if(x===y)

Consider this

x=3 and x==3 are not same

First statement x=3 means that the value of x is 3

x==3 is like checkinh whether x==3 or not

We can also compare objects using references with this == operator

The assignment operator (=) is used to assign a value to a variable, element of an array, or property of an object. Here are a few examples of using the assignment operator:

The equality operator (==) is used to compare two values or expressions. It is used to compare numbers, strings, Boolean values, variables, objects, arrays, or functions. The result is TRUE if the expressions are equal and FALSE otherwise. How items are compared depends on their data type

dazler

posted 11 years ago

Keep in mind that the boolean operator "==" provides a simple bitwise comparison of

(I'm not even going to mention Strings...)

[ December 16, 2004: Message edited by: marc weber ]

*values.*With primitives, this generally gives straight forward results. But applied to objects, this is only a "shallow" comparison of*references.*For a "deep" comparison of the objects themselves, an overridden "equals" method should be used.(I'm not even going to mention Strings...)

[ December 16, 2004: Message edited by: marc weber ]

*~Joe Strummer*

sscce.org

Rajith Vidanaarachchi

Greenhorn

Posts: 11

posted 11 years ago

Hi,

I think you asked the diference between the =(assignment operator)and the ==(boolean equality operator). = is used to say that the left most operand to get the value of the rigth most one.

eg:- int x=1;

int y=2;

int x=y;

this code makes the integer x to get the value of y.Now x is 2.

Now lets get to the ==. This is used to check the equality.For primitives it checks the value. For object references it checks the referenced object.

eg:- int x=1;

int y=1;

if(x==y)

System.out.print("x and y are equal");

//here it prints "x and y are equal"

I think you can understand this explanation of mine.

Thanks.

[ December 19, 2004: Message edited by: Rajith Vidanaarachchi ]

I think you asked the diference between the =(assignment operator)and the ==(boolean equality operator). = is used to say that the left most operand to get the value of the rigth most one.

eg:- int x=1;

int y=2;

int x=y;

this code makes the integer x to get the value of y.Now x is 2.

Now lets get to the ==. This is used to check the equality.For primitives it checks the value. For object references it checks the referenced object.

eg:- int x=1;

int y=1;

if(x==y)

System.out.print("x and y are equal");

//here it prints "x and y are equal"

I think you can understand this explanation of mine.

Thanks.

[ December 19, 2004: Message edited by: Rajith Vidanaarachchi ]