permaculture playing cards*
The moose likes Beginning Java and the fly likes == vs = Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "== vs = Question" Watch "== vs = Question" New topic
Author

== vs = Question

Kevin Lamb
Greenhorn

Joined: Dec 12, 2004
Posts: 3
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
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
'=' 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?


I've heard it takes forever to grow a woman from the ground
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
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


Francis Siu
SCJP, MCDBA
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Oct 27, 2004
Posts: 6
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


dazler
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]
Rajith Vidanaarachchi
Greenhorn

Joined: Nov 06, 2004
Posts: 11
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: == vs = Question