File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== vs = Question

 
Kevin Lamb
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 815
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'=' 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?
 
Francis Siu
Ranch Hand
Posts: 867
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is a misleading comparison. In mathematics, the equals sign denotes a relationship. But in Java, both "=" and "==" are operators.
 
Daksh Sharma
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic