473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Not copying a value, but rather pointing or referencing somehow... I need help

blazedaces
284 100+
Hey guys, I'll try and show a basic program and tell you what I wish it could spit out, and then maybe you'll understand what I want to do, since it's hard to explain.

Here's a simple public static void main(string args[]) {
String s = "hello";
String[] referenceTo = new String[1];

referenceTo[0]=s;
System.out.println(referenceTo[0]); // This will print hello

s="goodbye";
System.out.println(referenceTo[0]); // This will print hello, but I want it to print goodbye
}

Ok, so is there anything I can do so that if I change s, it would be reflected when I tried to call referenceTo[0]?

The only way I can thing of right now is if I have a set method that changes both values like this:
public String setS(String name) { s = name; referenceTo[0]=s; }

But that just seems redundant, I don't know, in C++ there's a simple way of using pointers and references to do exactly what I want, I think it would go something like this:

String s = "hello";

String *p;

p=&name;
value=*p;

cout << value; // prints hello to the screen
name = "goodbye";
cout << value; // prints goodbye to the screen

I could be wrong, but either way, anyone know what I'm talking about and can help me out? Every CS major friend I have seems to not like Java, so they don't know it well enough to answer a question like this...
May 18 '07 #1
4 1141
JosAH
11,448 Expert 8TB
Java passes parameters by value, always. Non-primitive variables are just
pointers to 'the real thing'. This means that this:
Expand|Select|Wrap|Line Numbers
  1. String s= "hello"; // s points to "hello";
  2. String as= { s }; // first element to as also points to "hello";
  3. s= "good bye"; // s points to "good bye now;
  4. System.out.println(as[0]); // as[0] still points to "hello";
.. never ever changes as[0] unless you change it explicitly.

kind regards,

Jos
May 18 '07 #2
blazedaces
284 100+
Java passes parameters by value, always. Non-primitive variables are just
pointers to 'the real thing'. This means that this:
Expand|Select|Wrap|Line Numbers
  1. String s= "hello"; // s points to "hello";
  2. String as= { s }; // first element to as also points to "hello";
  3. s= "good bye"; // s points to "good bye now;
  4. System.out.println(as[0]); // as[0] still points to "hello";
.. never ever changes as[0] unless you change it explicitly.

kind regards,

Jos
Thanks for your help.

I guess what I'll do is just have an initiateAS() method which sets all the AS[i] values to what strings I want them to be every time before i use it... best thing I could come up with...
May 18 '07 #3
prometheuzz
197 Expert 100+
Thanks for your help.

I guess what I'll do is just have an initiateAS() method which sets all the AS[i] values to what strings I want them to be every time before i use it... best thing I could come up with...

Have a look at this snippet:

Expand|Select|Wrap|Line Numbers
  1. String[] array = new String[10]; 
  2. java.util.Arrays.fill(array, "hello");
  3. System.out.println(java.util.Arrays.toString(array));
  4. java.util.Arrays.fill(array, "goodbye");
  5. System.out.println(java.util.Arrays.toString(array));
Note that you need Java 1.5+ for this to compile.
May 18 '07 #4
blazedaces
284 100+
Have a look at this snippet:

Expand|Select|Wrap|Line Numbers
  1. String[] array = new String[10]; 
  2. java.util.Arrays.fill(array, "hello");
  3. System.out.println(java.util.Arrays.toString(array));
  4. java.util.Arrays.fill(array, "goodbye");
  5. System.out.println(java.util.Arrays.toString(array));
Note that you need Java 1.5+ for this to compile.
Thank you, I mean, I realize since I've shown you very little you would have had no way of knowing, but I actually am using like 30+ string values, not one, so fill isn't too helpful. Thanks anyways though... :)
May 18 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: DamonChong | last post by:
Hi, I am still struggling to master C++ and is trying to understand how to achieve passing arguments using pointers. I got some questions that I like to post to the experts here, hope you can...
3
by: radioman | last post by:
Hi all, I would appreciate some help please. I just need pointing in the right direction as I am at a loss. Basically I have a form (frmAddMaster) which displays two subforms "Master Stock...
8
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
10
by: Jon Noring | last post by:
Out of curiosity, may a CDATA section appear within an attribute value with datatype CDATA? And if so, how about other attribute value datatypes which accept the XML markup characters? To me,...
13
by: Kirk McDonald | last post by:
Say I have a database containing chunks of Python code. I already have a way to easily load, edit, and save them. What is slightly baffling to me is how I can effectively pass this code some...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.