Forums Register Login

Instances of Objects in Java versus other languages

+Pie Number of slices to send: Send
Hello All,

I am learning JAVA and understanding some of its specific oddities. For example I notice that JAVA loves instances but not variables.

In Python:
x = 1
y = 2
print(x,y)
yields (1,2)
then I do y = x and print
getting (1,1)

This makes sense to me since x and y are variables and thats how variables work.

In JAVA I notice this script:


This yields:
x = 0, y = 0
x = 9, y = 0

So I get that JAVA doesn't understand variables? Or only takes instances of variables? Is there any way to yield a true variable or you always have to update variable relationships?
1
+Pie Number of slices to send: Send
Your Python and Java examples are not equivalent which is making it difficult to understand the distinction you're trying to make.

I really don't understand your question.
1
+Pie Number of slices to send: Send
Java does understand variables.
y = x means:
Take the value stored in variable x and assign that value to variable y.

It does not mean:
Assign variable y do x so they are the same variable.
1
+Pie Number of slices to send: Send
 

So I get that JAVA doesn't understand variables?



I don't know what you mean by this.

Or only takes instances of variables?



The type int in Java is a primitive. There is no instance of it.

Is there any way to yield a true variable or you always have to update variable relationships?



Java has a boolean type for true/false values.
+Pie Number of slices to send: Send
 

Paweł Baczyński wrote:Java does understand variables.
y = x means:
Take the value stored in variable x and assign that value to variable y.

It does not mean:
Assign variable y do x so they are the same variable.




Sorry all I really phrased this question oddly looking back. Also my examples aren't the same and when I do the same code I did in JAVA in Python that yields the same result.

I guess what I'm really try to find out is that when you do the following:

x = 1
y = x
x = 2
print y

When and how can y be the new value of x = (2).

Or is this how all programming languages work?
+Pie Number of slices to send: Send
Please check very carefully what you are printing in that first Python example. I think (x, y) looks like an ordered pair. I think you are using the () and , to create an ordered pair and what you are printing is not x and y at all. I think it is x goesTo y.

I suspect you have different operators and Java® (not “JAVA”) does not support that particular type of operator.
+Pie Number of slices to send: Send
 

Shaman Dasuta wrote: . . .
When and how can y be the new value of x = (2).
. . .

What does the operator () mean in that expression?
+Pie Number of slices to send: Send
 

Please check very carefully what you are printing in that first Python example. I think (x, y) looks like an ordered pair.



I think that may depend on the version of python. In python 2.x print is a keyword, but I believe in python 3.x it is a global function.
1
+Pie Number of slices to send: Send
As already noted by others, Java (not all caps, BTW) does understand variables as well as objects. The example you gave are primitive variables of type int. Here's how they work in Java and most other languages.

In languages that have pointers, like C and C++, I suppose you could get the behavior you were alluding to by using pointers and dereferencing pointers, like declaring something as an int* i:

I think with the above, whenever you change the value of the foo variable, the value you get by dereferencing foo_ptr would be the new value as well. My C is a little rusty so others can correct me if I got this wrong.
1
+Pie Number of slices to send: Send
Python doesn't behave differently to Java when it comes to assigning to variables. Each variable is distinct from the next. Two variables can reference the same object, but if one of those variables is assigned a new object it doesn't affect any other variable.



The only example I can think of where two variables are the same are references. My C++ is rusty though.
+Pie Number of slices to send: Send
if you had references to objects, you would see the behavior you are talking about.

+Pie Number of slices to send: Send
 

Junilu Lacar wrote:As already noted by others, Java (not all caps, BTW) does understand variables as well as objects. The example you gave are primitive variables of type int. Here's how they work in Java and most other languages.

In languages that have pointers, like C and C++, I suppose you could get the behavior you were alluding to by using pointers and dereferencing pointers, like declaring something as an int* i:

I think with the above, whenever you change the value of the foo variable, the value you get by dereferencing foo_ptr would be the new value as well. My C is a little rusty so others can correct me if I got this wrong.



My original post I realize now was quite confusing but I'm glad somehow you got to EXACTLY what I was thinking lol. Yup Pointers is what I was trying to describe and if there is something similar in Java.

Btw as some have pointed out whats the difference between JAVA and Java?
+Pie Number of slices to send: Send
 

Shaman Dasuta wrote:Btw as some have pointed out whats the difference between JAVA and Java?


The name of the language is "Java", and there is no such thing as "JAVA".

Apparently some people assume that it's an acronym and write it with all capitals. But it's not.
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 557 times.
Similar Threads
stars
the methods do not print
Regarding SCJP1.5 (foramt and printf)
clone() shallow copy in arrays?
Gomoku game Question (given at hackerrank.com)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:12:08.