r/javahelp • • Jan 30 '23

Explain me = and ==

Guys, I am learning Java for some time and have grasp of basics already. But one thing, which seems pretty simple hurts my brain. Please explain like you would explain it to a kid. When you use = and when you would use ==.

PS. I watch online videos and when I am sure to use =, I see them use ==. And whenever I am sure that == is needed I see them use =.

0 Upvotes

14 comments sorted by

•

u/AutoModerator Jan 30 '23

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

22

u/dionthorn this.isAPro=false; this.helping=true; Jan 30 '23 edited Jan 30 '23

= is the assignment operator, you use it to make the left side operand equal to the right side operand.

Java Language Specification (JLS) Chapter 15.26.1. Simple Assignment Operator =

https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.26.1

// Example:
int x = 10; // assign x the value of 10

== is the equality operator, you use it to compare the two operands.

JLS Chapter 15.21. Equality Operators

https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.21

// Example:
int x = 10;
int y = 10;
if(x == y) { // compare the two values for equality
    System.out.println("X and Y are equal");
}

Do note when comparing objects, like String, you should use the o.equals(o2) method like so:

String test = "hello world";
String test2 = "hello world";
if(test.equals(test2)) { // object equality comparison
    System.out.println("both strings are the same.");
}

18

u/richkirkpa Jan 30 '23

I tell my students; == asks a question, = does a thing.

-5

u/MapsCharts Jan 30 '23

How about === in JavaScript 😈

6

u/richkirkpa Jan 30 '23

=== still asks a question, but (basically) the types must match also…an int can only be equal to an int with the same numeric value.

10

u/rodneytheturtle Jan 30 '23

Maybe it could help if you think of it like english sentences.

If we have two ints x and y, and we say "x==y" that is like asking the question:

"is x the same as y?"

Suppose now we say "x=y", that is like making a demand:

"make x have the same value as y"

5

u/Small_Physics_38 Jan 30 '23

Idk its pretty simple, = is used when you want to assign a variable, and == used in 'if clauses, while loops.. etc"

1

u/[deleted] Jan 31 '23

Yeah one assigns and the other is a check. But everyone starts somewhere when starting out.

3

u/Ok-Novel-1427 Jan 31 '23

You haven't grasped any basics if you don't know the difference between assignment and comparison operators. This is a very basic concept, and without it, there's really no way you could do anything without brute force or copying code since you would not be able to explain to me what your code is doing.

As many have said, in its most basic terms, x = 21 would assign x the value of 21.

x == 21 will return true or false if x is equal to 21. The value of x here does not change to 21, it only compares it.

This is why alot of basic "IF" statements will have some sort of comparison denoted with == or != (not equals).

I suggest going through a textbook or something amd really taking time to understand what is being told and not just doing it for completion.

2

u/Alexandrite2000 Jan 31 '23

= is an initializer, == is a Boolean operator used for address comparison.

In other words: = is used to give something a value while == compares the stuff in an Object.

4

u/dionthorn this.isAPro=false; this.helping=true; Jan 31 '23 edited Jan 31 '23

When comparing objects == is not what most learners expect. It is used to see if the object is the same object in memory terms, not if the value is the same. So it is most commonly used for comparing primitives, not objects.

Whereby when comparing objects you should always use .equals() method.

1

u/hellsense Jan 31 '23

= is for assigning a value for a field and == is for comparing two objects.

1

u/Dougolicious Jan 31 '23

there's a george orwell quote: "some pigs are more equal than others"

this operator will let you test that!

0

u/Orangutanion Jan 31 '23
// = sets variables
int i = 1;
int j = 2;

// == compares two things and returns true or false
boolean maybe = i == j;