This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

When is an int not an int?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hope no-one minds the playful title, but I thought it's an intriguing phrase that kinda describes my question.

I found some code that I then tweaked to achieve my requirement.  
I'm happy that it worked but there's one part of it that I can't get my head round and I do like to try and understand code I use.

I've commented (mostly for myself really) what the code does.  
The only item not declared or instantiated in the method below is an ArrayList called usableLetters.
This simply holds a collection of 12 Strings which each happen to be a single letter of the alphabet.

Here's the code:



So the bit that's puzzling me is that the variable temp is an Integer.
And yet in the first line of that for loop it appears to be accessing and getting a String (the name of a TextView).

It's not a big deal but if anyone has the time and patience to explain what I'm missing here, I'd appreciate it.


 
Saloon Keeper
Posts: 5878
147
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not retrieving a String - it is very much retrieving an int (the ID of the TextView, which is what getIdentifier returns). Likewise, findViewById takes an int parameter, not a String - what makes you think otherwise?
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:It is not retrieving a String - it is very much retrieving an int (the ID of the TextView, which is what getIdentifier returns). Likewise, findViewById takes an int parameter, not a String - what makes you think otherwise?



I thought it was a String based on this example:

1.  Define some TextViews


2.  In OnCreate instantiate them  e.g.:


where the id in R.id.usable(n)  appears to be a String  - ie the name of the TextView.

From what you've said, presumably java takes the usable1 in R.id.usable1 as an identifier and finds the index of that object  (ie an int)?
 
Tim Moores
Saloon Keeper
Posts: 5878
147
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Resource identifiers (those R.id.xyz thingies) are ints. They may look look like strings because the "usable1" designation is a string in your layout file, but the Android build system automagically translates that to be an int you can use in your source code. "R" is actually an auto-generated class, and "usable1" is an int field in that. You can see the source code to that class in build/generated/not_namespaced_r_class_sources/debug/r/your/package/name/goes/here/R.java (replace your actual package name at the hopefully obvious spot :-). The directory name may differ depending on your Gradle and Android plugin version, but it's somewhere in the "build" directory.
 
Rancher
Posts: 3523
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the generated R,java class definition to see how the R.id.usable1 variable is defined.
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, I see it now.  All those TextViews listed as ints in the id class of R.

Thanks, guys.



(Thank Goodness for Notepad++   I opened the file up in Windows Notepad first - Spaghetti Code Central  :-)
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!