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 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon Oct 03, 2005 11:25 am   Post subject: Java Trivia

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.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Mon Oct 03, 2005 11:40 am   Post subject: (No subject)

True. True. (although java references can contain null values and are re-assignable, it really depends on ur definition of a pointer/reference.. in my books a pointer is somthing to which u can directy assign a physical address as in C-pointers.)
wtd




PostPosted: Mon Oct 03, 2005 11:55 am   Post subject: (No subject)

False and false. Smile

Java:
public class Test
{
   public static void main(String[] args)
   {
      String str = "Foo";
      System.out.println(str);
      foo(str);
      System.out.println(str);
   }

   public void foo(String s)
   {
      s = "Hello";
   }
}


Compare to:

c++:
#include <string>
#include <iostream>

void foo(std::string& s)
{
   s = "Hello";
}

int main()
{
   std::string str("Foo");
   std::cout << str << std::endl;
   foo(str);
   std::cout << str << std::endl;

   return 0;
}


And pointers exist. Smile

See NullPointerException. Wink
rizzix




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

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
rizzix




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

it's funny but you are wrong both times Wink

(yes i know it appears to be soo but it isin;t) you just happened to pick a poor example (the java String object)

Explaination:

first let's look at your code
code:
public class Test
{
   public static void main(String[] args)
   {
      String str = "Foo";
      System.out.println(str);
      foo(str);
      System.out.println(str);
   }

   public void foo(String s)
   {
      s = "Hello";
   }
}


Now here's the deal..
when passing str to foo.. what actually happens inside foo is this:
Java:
String s = foo
Basically the reference s now points to foo.. so if u reassign s a new object i.e ("hello").. that does not mean u are infact re-assigning foo the object "hello".

a better example would be this (cuz strings are immutable objects):

code:
public class Test
{
   class A {
       private String str;
       public void setStr(String s) {
           str = s;
       }
       public String toString() {
           return str;
       }
   }

   public static void main(String[] args)
   {
      A obj = new A();
      a.setStr("foo");
      System.out.println(a);
      foo(a);
      System.out.println(a);
   }

   public void foo(A a)
   {
      a.setStr("Hello");
   }
}
wtd




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

Quote:
Both s1 and s2 are in fact pointers (Java goes to great pains to hide this fact), and both are pointing to the same value. Since both strings can be determined at compile-time to be identical in value, and since Java's strings are immutable, it only allocates that string once. Thus both pointers are pointing to the same location in memory.

If we were to manually construct the string through concatenation, the Java compiler becomes incapable of (or unwilling to) determine whether they contain the same value, and thus we end up with two different locations in memory allocated. Since == tests for referential equality, this will now fail.

It can easily be seen from this example that == is a highly unreliable test to use here and the code should be rewritten using the "equals" method.
rizzix




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

ditto.
wtd




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

rizzix wrote:
it's funny but you are wrong both times Wink

(yes i know it appears to be soo but it isin;t) you just happened to pick a poor example (the java String object)

Explaination:

first let's look at your code
code:
public class Test
{
   public static void main(String[] args)
   {
      String str = "Foo";
      System.out.println(str);
      foo(str);
      System.out.println(str);
   }

   public void foo(String s)
   {
      s = "Hello";
   }
}


Now here's the deal..
when passing str to foo.. what actually happens inside foo is this:
Java:
String s = foo
Basically the reference s now points to foo.. so if u reassign s a new object i.e ("hello").. that does not mean u are infact re-assigning foo the object "hello".

a better example would be this (cuz strings are immutable objects):

code:
public class Test
{
   class A {
       public String str;
   }

   public static void main(String[] args)
   {
      A obj = new A();
      a.str = "foo";
      System.out.println(a.str);
      foo(a);
      System.out.println(a.str);
   }

   public void foo(A a)
   {
      a.str = "Hello";
   }
}


You're still confused. When you pass "a" into "foo", you're copying the pointer value into a completely new variable. Of course, you then assign to a field of the object "a" is pointing to, which mutates that field. Sadly Java lacks a "const" context.

This has nothing to do with references.

c++:
#include <string>
#include <iostream>

struct A
{
   std::string str;

   A() : str("Foo") { }
};

void foo(A *a)
{
   a->str = "Hello";
} 

int main()
{
   A *a = new A;
   std::cout << a->str << std::end;
   foo(a);
   std::cout << a->str << std::end;

   return 0;
}
rizzix




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

No i'm not confused... You are... You're thinking too much in C++.
Java references are not like C++ references..


maybe this will explain it a bit better:
code:
String a = "abc";
System.out.println(a);
String b = a;
b = "xyz";
System.out.println(a);


notice just cuz i assigned b, a. does not mean a has changed to "xyz"


NOTE: i also updated my previous example to better represent what i was trying to say.
wtd




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

I'm not thinking in C++. It just happens to be convenient for this, having both pointers and references.
rizzix




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

I told you java reference are not like c++ reference.. they point to objects yet they can't point to a physical address.

fact is.. c++ references are more like name aliases.
wtd




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

rizzix wrote:
code:
String a = "abc";
System.out.println(a);
String b = a;
b = "xyz";
System.out.println(a);


notice just cuz i assigned b, a. does not mean a has changed to "xyz"


Yes, you have two pointers: "a" and "b". You start out with "a" pointing to "abc".

You then have "b" point to the same location in memory that "a" points to.

You then decide that "b" should point to "xyz" instead.

Naturally, "a" is unchanged.

c++:
std::string *a = new std::string("abc");
std::cout << *a << std::endl;
std::string *b = a;
b = new std::string("xyz");
std::cout << *a << std::endl;


That's what the Java example is basically doing, though its syntax does a poor job of reflecting that behavior.
wtd




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

rizzix wrote:
I told you java reference are not like c++ reference.. they point to objects yet they can't point to a physical address.


But they do point to a physical address. You don't see that because Java doesn't want you to see its frilly underthings, but that's what's going on. Smile
rizzix




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

*sigh* i'f you'd rather call it that..

Java references are very much liker perl references... they are logical.. they can't really point to anything physical.. it's all in the VM.

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




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

wtd wrote:
rizzix wrote:
code:
String a = "abc";
System.out.println(a);
String b = a;
b = "xyz";
System.out.println(a);


notice just cuz i assigned b, a. does not mean a has changed to "xyz"


Yes, you have two pointers: "a" and "b". You start out with "a" pointing to "abc".

You then have "b" point to the same location in memory that "a" points to.

You then decide that "b" should point to "xyz" instead.

Naturally, "a" is unchanged.



Ditto similarly in your example the reference s (the parameter) is assigned to foo. Wink Now do you see why reassigning s simply assigns s another object.... s is not an alias of foo!
Display posts from previous:   
   Index -> Java
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

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


Style:  
Search: