Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java Trivia
Index -> Java
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon Oct 03, 2005 12:59 pm   Post subject: (No subject)

rizzix wrote:
i can't really do this in java:
Java:
Object a = 0x1F23FA;
or something like that...


No, but that's an artificial limitiation, rather than a sign of how the language really works. It's doing that under the surface, it just isn't letting you see it.

In fact, all paremeters are passed by value. It just so happens, that in the case of objects, that value is the pointer to the object. Smile

And yes, true references are effectively "name aliases".

Perhaps you would have preferred an Ada example (ints, since strings can be a pain in Ada):

ada:
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;with Ada.Text_IO; use Ada.Text_IO;
procedure Test is
   A : Integer := 42;

   procedure Foo(Bar : out Integer) is
   begin
      Bar := 27;
   end Foo;
begin
   Put(A, 0); New_Line;
   Foo(A);
   Put(A, 0); New_Line;
end Test;
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Mon Oct 03, 2005 2:32 pm   Post subject: (No subject)

Quote:
In fact, all paremeters are passed by value. It just so happens, that in the case of objects, that value is the pointer to the object. Smile
you could very-well say that nevertheless objects in java are passed by reference alone.
rizzix




PostPosted: Mon Oct 03, 2005 2:52 pm   Post subject: (No subject)

wtd wrote:
No, but that's an artificial limitiation, rather than a sign of how the language really works. It's doing that under the surface, it just isn't letting you see it.
Wrong again. It's not an artificial limitation.. the jvm is designed from the ground up likewise..

how's does that make it artificial?

just because your conception of how the language works.. is different from reality.. dosen't mean the language actually works they way you think it does. the jvm never ever refers to the physical memory directy, but only if the method is labeled "native".

Java:
class test {
    public static void main(String[] args) {
        Object o = new Object();
    }
}

0 new #2 <Class java.lang.Object>
3 dup
4 invokespecial #1 <Method java.lang.Object()>
7 astore_1
8 return
notice the bytecode generated for the assignment.. there's no direct reference to a physical memory location whatsoever. the object is directly stored into the reference o.

Java references are not pointers, neither are they aliases to objects. They are pure references.
staticvoid




PostPosted: Thu Mar 20, 2008 6:47 pm   Post subject: But...

rizzix @ Mon Oct 03, 2005 11:02 am wrote:
I got a better one for you.. (everyone is free to answer.. the first one give the right answer will get 200 bits)

Now here's the question, explain me why the follow code results true:
Java:
String s1 = "hello world";
String s2 = "hello world";

s1 == s2; // true


...but string is a class, not a primitive type.

You could use

Java:
String s1 = "hello world";
String s2 = "hello world";
s1.equals(s2);
r691175002




PostPosted: Fri Mar 21, 2008 1:59 am   Post subject: Re: Java Trivia

wtd @ Mon Oct 03, 2005 11:25 am wrote:
Let's see how good the resident Java gurus really are. Smile
Two true or false tests.
In Java, all object types are passed by reference.
Java does not have pointers.

You can only pass values which happen to be references to the objects in question. Technically you are passing by value, in general it works out like you are passing by reference. This means that any changes made to the object will affect it, however changes made to the reference itself (Via = operator) will not affect the main program.

No, Java does not have pointers. You are going to need a stronger argument than the name of an exception (Probably a legacy from earlier versions to cater to C/C++ programmers). It doesn't matter what you think they are behind the scenes or how they work, in Java there is only one type of reference, and they decided to call it a reference. They could call it SignThingie and then it would be a SignThingie, because thats what they say it is.

Essentially:
1. False
2. True

As to staticvoid, his code is correct. In this case, the equality operator will return true as both references point to the same value, as explained in third or so post.
TokenHerbz




PostPosted: Thu Mar 22, 2012 11:02 pm   Post subject: RE:Java Trivia

don't rely on the == operator for string tho, because what it does under neath the surface, is checks does A == B MEMORY LOCATION kinda deal. it checks to see if they sit in the same spot so if your reading a file for value A and have a value already made for B in your program thats set and ready to go, thats in memory location X, and well X doesn't = Y(being the read file)... you should ALWAYS use the .equils which compares the actual values, not just the references to location of memory. because both A and B could be exactly the same, if you read one in via file and assign one in the program the memory allocations differentiate and == results a false.
md




PostPosted: Fri Mar 23, 2012 4:32 pm   Post subject: RE:Java Trivia

Holy necroposting batman!

Don't do it again.
yesir360




PostPosted: Tue Dec 24, 2013 10:36 pm   Post subject: (No subject)

rizzix @ Mon Oct 03, 2005 9:02 am wrote:
I got a better one for you.. (everyone is free to answer.. the first one give the right answer will get 200 bits)

Now here's the question, explain me why the follow code results true:
Java:
String s1 = "hello world";
String s2 = "hello world";

s1 == s2; // true


It's by default.
(I think)

"==" is used for numbers and such
while using strings, you should use "s1.equals(s2);"
otherwise, you will just get true as default.
Display posts from previous:   
   Index -> Java
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 23 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: