• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instances of Objects in Java versus other languages  RSS feed

 
Shaman Dasuta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paweł Baczyński
Bartender
Posts: 2084
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shaman Dasuta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shaman Dasuta wrote: . . .
When and how can y be the new value of x = (2).
. . .
What does the operator () mean in that expression?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you had references to objects, you would see the behavior you are talking about.

 
Shaman Dasuta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!