programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# == vs = Question

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
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.

Kevin

Ranch Hand
Posts: 815
• Number of slices to send:
Optional 'thank-you' note:
'=' 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?

Ranch Hand
Posts: 867
• Number of slices to send:
Optional 'thank-you' note:
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.

Ranch Hand
Posts: 3061
• Number of slices to send:
Optional 'thank-you' note:

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.

Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:
I think this is a misleading comparison. In mathematics, the equals sign denotes a relationship. But in Java, both "=" and "==" are operators.

Greenhorn
Posts: 6
• Number of slices to send:
Optional 'thank-you' note:
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

marc weber
Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:
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 ]

Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
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 ]

 Don't get me started about those stupid light bulbs.