Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 30th, 2007, 08:24 PM
Registered User
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Number of variables referencing an Object

Hello everyone,
I'm on chapter 4 (Arrays and Strings) and I'm playing around a bit with what I know so far. I made a program to return all the repeated words in a text and the number of times that these words were repeated. I got the program working now.

I thought of an easier way of doing it with a different method. Rather than having to use all the indexes, loops and messy code I wrote maybe I could intern() all the strings in the array containing the split()ted text and then find out whether a String is referenced by more than one variable and if so how many times. Is this possible? Can you count how many variables are referencing an object (String in this case)?

I'm starting programming so I don't know any of the Array classes utilities, lists, etc that are on the book further on. So code using those won't be very helpful.

Thank you.
 
Old September 6th, 2007, 11:24 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey, no you can't do that AFAIK. Each string in your array is a seperate object, rather than a reference to an object. So if I have the word "house" three times, that means I have three strings not three pointers to the same string.

The nicest way to solve this problem involves you using a hash table with the split() strings as its keys. But I won't confuse you with that now.

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
 
Old September 6th, 2007, 01:33 PM
Registered User
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'll keep that in mind for when I know a
bit more of programming.
Thanks Charlie.

  --------------------------------------------------------------------
Remember, remember the 5th of November. The gunpowder, treason, and plot. I know of no reason why the gunpowder treason should ever be forgot.
 
Old April 11th, 2009, 11:02 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Quote:
Originally Posted by ciderpunx View Post
Each string in your array is a seperate object, rather than a reference to an object. So if I have the word "house" three times, that means I have three strings not three pointers to the same string.
This is not true. If you have three String and all of them have the word "house", java only keeps one instance of "house", and all three objects reference the same instance.

Actually this is the same in C#.

But you are right, what the OP suggested is not doable.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation For Phone Number and Mobile Number dhruthi.ram99 Javascript How-To 12 October 30th, 2011 07:24 AM
Referencing Unbound fields with variables TomW Access VBA 2 November 12th, 2006 11:55 AM
referencing an object in an edit item template dan195 ASP.NET 1.0 and 1.1 Basics 3 September 25th, 2006 07:15 AM
Referencing the object that invoked a method benr Classic ASP Professional 0 March 12th, 2006 01:55 PM
Referencing an object from within a procedure mike123abc Beginning VB 6 5 February 3rd, 2004 02:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.