Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 30th, 2007, 08:24 PM
Registered User
 
Join Date: Aug 2007
Location: , , Spain.
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.
Reply With Quote
  #2 (permalink)  
Old September 6th, 2007, 11:24 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
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
Reply With Quote
  #3 (permalink)  
Old September 6th, 2007, 01:33 PM
Registered User
 
Join Date: Aug 2007
Location: , , Spain.
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.
Reply With Quote
  #4 (permalink)  
Old April 11th, 2009, 11:02 PM
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:20 PM.


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