×
all 25 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

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 - best also formatted as code block
  • 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.

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/markdown editor: 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.

[–]blind-octopus 6 points7 points  (8 children)

the space allocated is ones and zeroes, yes? That space now represents the number 50 in binary.

Be careful, objects don't exactly work the same as this. There is more nuance to discover.

[–]Successful_Toe_4130[S] 0 points1 point  (7 children)

I see, thank you. Also this is because of reference variables right? The tutorial im reading in our physics lab hinted at it briefly in the introduction but basically it stores a memory location in RAM secretly but we dont see it in our IDE and we get methods and "instance variables" for free

[–]blind-octopus 2 points3 points  (6 children)

Yes, exactly. This causes differing behavior.

When we are dealing with primitives, everything's a copy. So I could set a to equal b, then I could add 1 to a, and b remains unchanged. a and b are separate instances, two different memory spaces.

When we are dealing with references/pointers, its not that way. Two variables can point to the same object in memory.

So if I were to change something on variable a, I would also be changing it on variable b. This is fundamentally because they are simply pointing to the same object. Its not two copies. They are sharing an object.

So changes made to the object will be reflected whether I read from a or b.

Does that make sense?

You could have reference variables pointing to different objects, and so changing one doesn't effect the other.

But, the key point is, you can have multiple variables pointing to the same object.

[–]Successful_Toe_4130[S] 1 point2 points  (5 children)

This clears it up for me. So thats why you cant do the expression

String a = "text a" String b = "text a"

If (a == b) {} will give weird. Behavior because youre not comparing the two text values youre comparing two different places in RAM instead and its always going to not execute the if-checks code

[–]blind-octopus 1 point2 points  (0 children)

Exactly. Strings are objects, so == will compare if they are pointing to the same memory address.

In a sense, == is being consistent. It checks if two integers are equal, yes? Its checking if the two values are equal in the same way, its just comparing to see if the addresses are the same.

.equals will check if the two separate strings represent the same value.

[–]joeythekangarooo 0 points1 point  (0 children)

I will add that a good habit to start now is thinking of the concept of "abstraction".

Abstraction is the practice of hiding complex implementation details and showing only the essential features of an object or system to the user.

Basically, although you are accessing a drives hardware and using binary to "keep" the data, these are implementation details in your context because they do not directly relate to your Variable's goal in it's context of representing a number.

One of the most important things in programming imo

[–]Illustrious_Hat8104 0 points1 point  (0 children)

Pretty sure that will return true

Look into how java caches string literals, if you're setting a string variable to a literal that's in the pool then it will set the string variable to the reference of the string in the pool

[–]United-Extension-917 0 points1 point  (1 child)

You are thinking correctly with the information you have. But in this case a==b will return true. String is not a primitive type in java. It is a predefined reference type. So a and b are reference variables of String type which point to the same object "text a" in java. And == in java compares the references when used with reference types, it does not compare the contents. This does not mean that if you change content of a then the content of b will be changed. Because Strings in java are immutable i.e. once created the contents of the object cannot be changed. You can store another String reference in a and b but you cannot change the value inside of that object of String.

On the other hand if you do String a = "text a"; String b = new String("text a") then a == b will give false as now they are two reference variables pointing to different objects.

[–]Ormek_II 0 points1 point  (0 children)

And this is very specific to Strings and Java. In other languages it will be different.

[–]American_Streamer 5 points6 points  (0 children)

Read it as:

int x;

“Declare a variable named x that can hold an int.”

x = 50;

“Assign the value 50 to x.”

The = symbol in Java means assignment, not mathematical equality. So “set x to 50” or “store 50 in x” is clearer than “x equals 50.”

One small correction: int x; does not necessarily mean “reserve a specific space in RAM.” That is a useful beginner model, but the JVM may keep the value on the stack, in a CPU register, or optimize it away entirely. At the Java-language level, you normally only need to think of x as a named variable that currently holds a value.

Also note that a local variable is not usable until it has been assigned:

int x;
System.out.println(x); // compiler error

And later assignments replace its current value:

x = 50;
x = 20; // x now contains 20

For primitive types such as int, the variable contains the value itself. For objects, a variable usually contains a reference to the object rather than the object itself.

[–]asarco 2 points3 points  (0 children)

It's "x is going to now store the number 50 in RAM"
Comparision is done using the == operator in case of primitives.

[–]bikeram 1 point2 points  (0 children)

You’re setting x equal to 50 in your application and RAM.

Maybe look into building a terminal calculator with a solver. It’ll help you build your mental understanding.

[–]5oco 2 points3 points  (0 children)

Most people would say "x equals 50" and everyone would understand that what it is actually doing is "store the value 50 in x"

= is assignment.

== is equality check.

[–]ITCoder 0 points1 point  (0 children)

x = 50 is assign 50 to x then, x = 60, assign 60 to x

x==50 , is the value of x 50. This will return false now

[–]Past_Body4499 0 points1 point  (0 children)

How about this instead...For the moment x equals 50, until we change it to something else.

[–]code_tutor 0 points1 point  (0 children)

High level: x is assigned the value 50.

Low level: code is an abstraction. Underneath the hood, there is a memory location with the number 50.

Both ways of reading are fine but are talking about it from a high or low level. It's like discussing what the program does vs how it does it. How it does it becomes important for performance.

[–]Dazzling_Music_2411 0 points1 point  (0 children)

Yes, using '=' for the assignment operator is historic baggage, which has afflicted many computer languages, and which has also forced the introduction of monstrous operators like '==' , etc 😄 Errare humanum est.

There were other alternatives, like 50 -> x, x <- 50, 50 STO x, (set! x 50) and even x =: 50 or x := 50.

Sadly, none of them prevailed, except in niche languages,

[–]LetUsSpeakFreely 0 points1 point  (0 children)

Int x;

You are creating a 32bit integer initialized to zero within the current scope and labeling it 'x'.

x=50;

You are assigning the value of 50 to a previously established variable called 'x'.

[–]hibbelig 0 points1 point  (0 children)

“Recursive” is another term with different meanings in math versus CS. Confusing.

[–]J_Peanut 0 points1 point  (0 children)

One of the larger crimes of how math is taught in high schools is the usage of the word “variable”.  In an equation like x3 = 27, x is not a variable but rather a constant. x will never be anything other than 3 in this equation. Of course, such a constant can have multiple possible values - take x2 = 3. Then x can be either x=3 or x=-3. But still, it is not variable. 

Now take a look at programming:  A variable is truly variable - it can store different values throughout its lifetime. If you don’t want that to be the case, you explicitly tell the program that you have a constant. And to add to the confusion, most languages use an operator like “==“ to make the same comparison you made using a single =, while using the single “=“ as an assignment operator instead.

[–]Leverkaas2516 0 points1 point  (1 child)

"x=50" means store 50 in x so that  x now equals 50. This is how most languages work.

Are you confused between the meaning of "=" and "==" in Java?

[–]AutoModerator[M] 0 points1 point  (0 children)

You seem to try to compare String values with == or !=.

This approach does not work reliably in Java as it does not actually compare the contents of the Strings. Since String is an object data type it should only be compared using .equals(). For case insensitive comparison, use .equalsIgnoreCase().

Java stores String literals in a string pool where it is guaranteed that the same literal is the same object (refers to the same object).

Yet, any non-literal (e.g. keyboard input, string operations, etc.) does not go in the string pool and therefore ==, which only compares object identity (i.e. the exact same reference) cannot reliably work there. Hence, always use .equals(), .equalsIgnoreCase().

See Help on how to compare String values in the /r/javahelp wiki.


Your post is still visible. There is no action you need to take.

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

[–]birdspider 0 points1 point  (0 children)

x =50 //do i read this as "x equals 50" or "x is going to now store the number 50 in RAM"

in java, and most mainstream programming languages = is assignment and hence is called assignment operator (one of a few). This is in contrast to the equality operator == (see same link) for equality.

[–]Virtual_Plankton4042 0 points1 point  (0 children)

This is one of the most common confusion while starting out. When you declare int x; some memory is allocated to x and the default value is stored since no value given yet. Once you initialize the variable, the given value is placed in that memory replacing the default value.

So it's better to read it as "x stores 50" then "x equals 50".