This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

Hey guys/gals. I have another very simple question. What is the difference between the ==, and the =. I think I understand the == to mean, literally "equal to" but the single = sign has me stumped. Thanks again all.

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

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

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

Keep in mind that the boolean operator "==" provides a simple bitwise comparison of 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 ]

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 ]