# C++, Java, CGI

**URL:** <https://boards.straightdope.com/t/c-java-cgi/267831>\
**Category:** In My Humble Opinion\
**Created:** [October 6, 2004, 1:19pm UTC](https://boards.straightdope.com/t/c-java-cgi/267831 "2004-10-06T13:19:04Z")\
**Posts on this page:** 3\
**Page:** 3

<div class="post-metadata">

**Author:** ![Small\_Clanger](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Small\_Clanger](https://boards.straightdope.com/u/Small_Clanger)\
**Post date:** [October 8, 2004, 2:02pm UTC](https://boards.straightdope.com/t/c-java-cgi/267831/41 "2004-10-08T14:02:32Z")

</div>

Can’t resist, such a horrible collision of languages. . .

> [@Johnny L.A](#):
>
> What’s the difference between these two lines?  
> IF X = Y \* 3.14  
> PRINT X  
> ELSE PRINT ‘X IS FALSE’  
> END-IF
> 
> IF X == Y \* 3.14  
> PRINT X  
> ELSE PRINT ‘X IS FALSE’  
> END-IF

Say Y equals 2:

if ( X = Y \* 3.14 ) // X now equals 6.28, and if( 6.28 ) evaluates to ‘true’ (in C at any rate, dunno about Java )

if( X == Y \* 3.14 ) // is ‘true’ if X currently equals 6.28

For what I can make out from your EZ+ examples ‘=’ is used for either equality or assignment depending on context.

---

<div class="post-metadata">

**Author:** ![minor7flat5](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/minor7flat5/32/258_2.png) [@minor7flat5](https://boards.straightdope.com/u/minor7flat5)\
**Post date:** [October 8, 2004, 4:51pm UTC](https://boards.straightdope.com/t/c-java-cgi/267831/42 "2004-10-08T16:51:02Z")

</div>

Exactly what **Small Clanger** said.

Some languages (BASIC being one) allow you to use “=” with impunity everywhere.

a = 27

if a = 9 then  
'perfectly good syntax  
end if

These languages are smart enough to see the context of the single “=” and say “He is obviously _comparing_ these two things” or “He is obviously _assigning_ a new value to the thing on the left” This doesn’t mean that C++ and Java are backwards since BASIC got it right years ago; rather, introducing objects complicates issues somewhat.

Even BASIC, with its friendly context sensitive interpretation of “=”, used to require the developer to explicitly distinguish between the two purposes. Once upon a time, in order to assign a value in BASIC, one was required to use the “let” keyword:

**LET** a = 27

That was enough to make sure the compiler knew exactly what you intended. They added the context sensitive bit later on so that you didn’t need to type “let” all over the place.

FORTRAN also distinguishes between assignment and comparison:

Assignment: LET A = 10  
Comparison: A.EQ.10

The “==” is simply a modern replacement for FORTRAN’s “.EQ.” operator.

Now to revisit the Java string comparison problem…  
The problem here is that Java objects (and strings are objects) are simply references: a pointer to a place in memory where the thing lives.

Java interprets “==” comparison between two object references very literally: It says that the two things (object references) are the same if they point to the same memory location. Keep in mind that “==” works perfectly well for standard integers, booleans, bytes, floats and such.

This is a highly restrictive comparison. Consider my prior example of a “Car” object. The Java “==” comparison will say two cars are the same if and only if they have the same VIN. We really want a smarter kind of equality check that can say “these cars are the same since they have the same motor, make, model, color _even though_ they are distinct vehicles that live in different locations in memory”

In short:

[ol][li]"=" is for assignment only[/li][li]"==" is for very literal equality comparison. It works great for numbers and bytes, but it is usually wrong for objects (in Java)[/li][li]“myObject.equals(yourObject)” is the clumsy thing we are left with for comparing objects in a smart way.[/li]C++ has a much fancier “==” that we can override with our own code to skip the syntax nonsense of point 3.[/ol]

---

<div class="post-metadata">

**Author:** ![alimarx](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@alimarx](https://boards.straightdope.com/u/alimarx)\
**Post date:** [October 8, 2004, 6:01pm UTC](https://boards.straightdope.com/t/c-java-cgi/267831/43 "2004-10-08T18:01:47Z")

</div>

Cool thread! FWIW, Johnny LA, you’re already way ahead of some people trying to learn programming!

[Previous page](https://boards.straightdope.com/t/c-java-cgi/267831.md?page=2)
