February 06, 2016

Android Dev Tips: How to replace Enum by int or String efficiently?

There's a good topic on developer.android.com about how to manage app's memory. I want to touch specific paragraph about enums:
Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.
Lot of developers don't want to follow this rule, saying this is inconvenient, hard to navigate through code, hard to debug, blah-blah-blah... 

Android guys want to help developers with this. They provided android.support.annotation package with lot of powerful annotations for Android Studio and Lint. I want to explain more about IntDef and StringDef. There two classes may help to replace enums efficiently for both: performance and convenience.

Let's look at two typical enum classes:


two enum declarations

Nothing special, short set of logically named constants. If we replace them by static final variables, then we'll automatically loose completeness of these declarations. It will be set of unlinked constants. Here is good tutorial how to use android annotations. It says:

The @IntDef annotation lets you basically create a "typedef", where you create another annotation which represents the valid integer constants that you expect, and then you decorate your API with this typedef annotation.

It's okay, good idea. But what if we'll go even further? We will place our constants directly in the annotation class, like this:

two annotations with constants inside

It looks a bit more complicated then enum declaration, but let's check how it works in code. Here is the simplest class declaration with two fields representing our enums. All we need to do is just mark both: fields and accessors with the annotations.

simple class with two fields of types int and String marked with annotations declared above

And now let's try to assign some values out of declared constants set. You can see error messages directly in editor.

example of error messages

It also works for switch-statement.

example of using switch-statement

Thus we can replace enum classes by simple constants without losing comfort of using strictly declared types. Beside that we improve performance of our apps.

Source code of this example is here.

253 comments:

  1. Just seen your Article, it amazed me and surpised me with god thoughts that eveyone will benefit from it. It is really a very informative post for all those budding entreprenuers planning to take advantage of post for business expansions. You always share such a wonderful articlewhich helps us to gain knowledge .Thanks for sharing such a wonderful article, It will be deinitely helpful and fruitful article.
    Thanks
    DedicatedHosting4u.com

    ReplyDelete
  2. The article is very interesting and very understood to be read, may be useful for the people. I wanted to thank you for this great read!! I definitely enjoyed every little bit of it. I have to bookmarked to check out new stuff on your post. Thanks for sharing the information keep updating, looking forward for more posts..
    Kindly visit us @
    Madurai Travels | Travels in Madurai
    Best Travels in Madurai
    Cabs in Madurai | Madurai Cabs
    Tours and Travels in Madurai

    ReplyDelete
  3. I have read your blog and I gathered some needful information from your blog. Keep update your blog. Awaiting for your next update. Thanks
    DedicatedHosting4u.com

    ReplyDelete
  4. Great. It is good to constantly coming up with creative ideas. Provides much needed knowledge. goal oriented blog posts and always tried to find creative ways to meet goals.

    Thanks
    Online affiliates

    ReplyDelete
  5. Your post is very good. I got to learn a lot from your post. Thank you for sharing your article for us. it is amazing post
    what is seo
    types of seo

    ReplyDelete
  6. This is a fantastic idea! I like it a lot because it's super easy for the audience to see the value of opting in. wonderful and amazing post very use full your post thanks for sharing your article
    Android Application development
    Web application


    ReplyDelete

  7. This is a fantastic idea! I like it a lot because it's super easy for the audience to see the value of opting in. wonderful and amazing post very use full your post thanks for sharing your article
    Android Application development
    Web application

    ReplyDelete
  8. Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading.
    Continue

    ReplyDelete
  9. Replies
    1. This is Very very nice article. Everyone should read. Thanks for sharing. Don't miss WORLD'S BEST GAMES

      Delete
  10. GBWhatsApp is a far more convenient and modified version of WhatsApp – available for use by everyone on the Internet. Currently, the application only operates on Android smartphones and is not supported by iOS devices.for more Updates Visit Our click here

    ReplyDelete
  11. Soma pill is very effective as a painkiller that helps us to get effective relief from pain. This cannot cure pain. Yet when it is taken with proper rest, it can offer you effective relief from pain.
    This painkiller can offer you relief from any kind of pain. But Soma 350 mg is best in treating acute pain. Acute pain is a type of short-term pain which is sharp in nature. Buy Soma 350 mg online to get relief from your acute pain.

    https://globalonlinepills.com/product/soma-350-mg/


    Buy Soma 350 mg
    Soma Pill
    Buy Soma 350 mg online



    Buy Soma 350 mg online
    Soma Pill
    Buy Soma 350 mg

    ReplyDelete
  12. Dr Driving is one of the my favourite game ever and today I am going to share Dr Driving Mod Apk
    https://www.drdrivingmodapk.xyz/

    ReplyDelete
  13. Whatsapp Group Link - 5000+ Updated Whatsapp Groups
    whatsapp group link Click The link And Join Whatsapp New Groups, Dating, Business promositions Groups

    ReplyDelete
  14. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete
  15. keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
    digital marketing training in bangalore | https://www.excelr.com/digital-marketing-training-in-bangalore

    ReplyDelete
  16. Really great article, you guys are doing a great work, keep posting good stuff, If your audience is into Tech do share 30 MiUi 11 Tips and Tricks
    Gizdoc Tech Paradise

    ReplyDelete
  17. this blog is very nice and share good information with us i love it tweakbox for android

    ReplyDelete
  18. Hello Admin!

    Thanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuffs like this. If you are looking for the Advertising Agency in Chennai | Printing in Chennai , Visit Inoventic Creative Agency Today..

    ReplyDelete

  19. You write this post very carefully I think, which is easily understand to me. Not only this, other post is also good. As a newbie this info is really helpful for me. Thanks to you.
    tally training
    Tally Training in Chennai
    Tally ERP 9 Training
    Tally Course
    tally classes
    Tally institute in Chennai
    Tally Training institute in Chennai
    Tally course in Chennai

    ReplyDelete
  20. This content of information has
    helped me a lot. It is very well explained and easy to understand.
    seo training classes
    seo training course
    seo training institute in chennai
    seo training institutes

    ReplyDelete
  21. Hello Admin!

    Thanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuffs like this. If you are looking for the Advertising Agency in Chennai | Printing in Chennai , Visit Inoventic Creative Agency Today..

    ReplyDelete

  22. You write this post very carefully I think, which is easily understandable to me. Not only this, but another post is also good. As a newbie, this info is really helpful for me. Thanks to you.
    Tally ERP 9 Training
    tally classes
    Tally Training institute in Chennai
    Tally course in Chennai

    ReplyDelete
  23. Great efforts put to find the list of articles that are very useful to know. I’m thoroughly enjoying your blog. And Good comments create relations. You’re doing great work. Keep it up.

    Magento Development Training Course in Chennai Zuan Education

    Selenium Training Course in Chennai Zuan Education

    ReplyDelete
  24. I feel satisfied to read your blog, you have been delivering a useful & unique information to our vision.keep blogging.
    Digital Marketing Course In Kolkata

    ReplyDelete
  25. Business Analytics Certification Course Overview
    The Professional Certification in Business Analytics is a foundation course for students and professionals who want to develop niche data skills for their chosen industry domain or function area. Become a Business Intelligence and Data Visualisation expert and surge ahead in your career. The nine-day Business Analytics certification course covers all the essential Analytical and Statistical techniques for effective business decision making. This programme introduces the student to the basic concepts of Python language. https://360digitmg.com/business-analytics-training-in-hyderabad

    ReplyDelete
  26. Great Article. Thank you for sharing! Really an awesome post for every one.
    Digital Marketing Course In Kolkata
    Web Design Course In Kolkata

    ReplyDelete


  27. This post is really nice and informative. The explanation given is really comprehensive and informative. I also want to say about the complete digital marketing course

    ReplyDelete
  28. Great post, Thanks for such cool pots.
    Checkour whatsapp Tricks Here 18+ Whatsapp Group Links | Girls Whatsapp Group Links

    ReplyDelete
  29. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading ExcelR Digital Marketing Class In Pune topics of our time. I appreciate your post and look forward to more.

    ReplyDelete
  30. I liked the way you posted here. Thankful for the presentation. Keep it up...satta king

    ReplyDelete
  31. Your style is unique compared to other people I have read stuff from. Many thanks for posting when you've got the opportunity, pro Guess I'll just book mark this site.

    ReplyDelete
  32. Thanks for sharing information. Choosing computer accessories from leading IT store offer great discount and value for your money Computer Store Australia | All in One Pc Australia

    ReplyDelete
  33. Hi, I do think this is a great website. I stumbledupon it ;) I am going to come back once again since i have saved as a favorite it. Money and freedom is the greatest way to change, apps may you be rich and continue to help other people.

    ReplyDelete
  34. Hello Admin!

    Thanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuffs like this. If you are looking for the Advertising Agency in Chennai | Printing in Chennai , Visit Inoventic Creative Agency Today..

    ReplyDelete
  35. This comment has been removed by the author.

    ReplyDelete


  36. I found a lot of information here to create this actually best for all newbie here. Thank you for this information.

    Artificial Intelligence Training In Hyderabad

    ReplyDelete
  37. Hi, Thanks for sharing such a huge content...

    For More:

    AI Training In Hyderabad

    ReplyDelete
  38. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    microservices online training
    best microservices online training
    top microservices online training

    ReplyDelete
  39. Thanks for Sharing This Article.It is very so much valuable content.
    AWS Training in Hyderabad

    ReplyDelete
  40. Awesome blog, I enjoyed reading your articles. This is truly a great read for me.
    DevOps Training
    DevOps Online Training

    ReplyDelete
  41. It's a very attractive and useful blog .I has found the solution, Thanks for the share.
    Data Science Training in Hyderabad

    ReplyDelete
  42. Reading this article gave me many things to think about. You have some quality information here that any reader would enjoy. I share many of your views in this article.
    Best Data Science training in Mumbai

    Data Science training in Mumbai

    ReplyDelete
  43. This is an awesome motivating article. I am practically satisfied with your great work. You put truly extremely supportive data. Keep it up. Continue blogging. Hoping to be perusing your next post
    Courses in Digital Marketing in Pune

    ReplyDelete
  44. Thanks for sharing this wonderful informations.
    android training institutes in coimbatore

    amazon web services training in coimbatore

    big data training in coimbatore

    C and C++ training in coimbatore

    Blue prism training in coimbatore

    artificial intelligence training in coimbatore

    RPA Course in coimbatore

    ReplyDelete
  45. Great blog. it was so Nice to read and useful to improve my knowledge as updated one,
    Machine Learning Training in Hyderabad

    ReplyDelete
  46. thanks for sharing nice information. its Very use full and informative and keep sharing.
    more : https://www.analyticspath.com/artificial-intelligence-training-in-hyderabad

    ReplyDelete
  47. Wonderful post, i loved reading it.
    Share more
    Bluecoinsapp
    Otomachines
    Fairvote

    ReplyDelete
  48. thanks for sharing nice information. its Very use full and informative and keep sharing.
    more : https://www.kellytechno.com/Hyderabad/Course/AI-Training-In-Hyderabad

    ReplyDelete
  49. Hey, i liked reading your article. You may go through few of my creative works here
    Riosabeloco
    Systemcentercentral

    ReplyDelete
  50. I had to read this three times because I wanted to be sure on some of your points. I agree on almost everything here, and I am impressed with how well you wrote this article.
    SAP training in Kolkata
    SAP training Kolkata
    Best SAP training in Kolkata
    SAP course in Kolkata
    SAP training institute Kolkata

    ReplyDelete
  51. I agree that a second pair of eyes is absolutely essential to avoid rejections. Through WordsRU.com I was able to get top class editing and proofreading, manuscript critique. thanks
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  52. Great article like this require readers to think as they read. I took my time when going through the points made in this article. I agree with much this information
    DevOps Training in Chennai | DevOps Training in anna nagar | DevOps Training in omr | DevOps Training in porur | DevOps Training in tambaram | DevOps Training in velachery

    ReplyDelete
  53. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete
  54. I like your post. Everyone should do read this blog. Because this blog is important for all now I will share this post. Thank you so much for share with us.
    DevOps Training in Hyderabad
    DevOps Course in Hyderabad

    ReplyDelete

  55. Great Article
    Cloud Computing Projects


    Networking Projects

    Final Year Projects for CSE


    JavaScript Training in Chennai

    JavaScript Training in Chennai

    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    ReplyDelete
  56. DigiPeek Provide Domain Rating & Domain Authority Increasing Service. DR 70+ & DA 50+ With Money Back Guaranteed.

    Grab your Ahrefs Domain Rating 70+ & Moz Domain Authority 50+ here!

    Does domain authority matter?

    Yes, Your Domain Authority is important because it is representative of how you rank on search engine. It helps you better understand your site's credibility in the eyes of the search engines and you can see how you compare to your competition.

    Thank you!
    DigiPeek

    ReplyDelete
  57. Nice article very helpful and informative thanks for sharing this information Full post
    Jio Missed call alert

    ReplyDelete

  58. Read your blog, Excellent content written on Android Dev Tips: How to replace Enum by int or String efficiently?

    If you are looking for RPA related job with unexpected Pay, then visit below link


    RPA Training in Chennai
    RPA Course in Chennai
    RPA Training
    RPA Course
    Robotic Process Automation Training in Chennai
    Robotic Process Automation Training
    RPA Training Institute in Chennai
    RPA Training in Velachery
    Robotic Process Automation Certification

    ReplyDelete
  59. I have read your blog and I gathered some new information through your blog. Thanks for sharing the information
    Data Science Training in Hyderabad

    ReplyDelete
  60. Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

    Rowe Rowe
    Rowe Rowe
    Rowe Rowe
    Rowe Rowe

    Thank you..

    ReplyDelete
  61. Forex Signals, MT4 and MT5 Indicators, Strategies, Expert Advisors, Forex News, Technical Analysis and Trade Updates in the FOREX IN WORLD

    Forex Signals Forex Strategies Forex Indicators Forex News Forex World

    ReplyDelete
  62. Freelance maintenance technicians also benefit from establishing relationships with multiple clients. Working as a freelancer, you’ll gain employment experience with a variety of businesses, boosting your overall skill set and providing you with more work opportunities in the future.

    maintenance technician

    ReplyDelete
  63. Thanks for posting useful information.You have provided an nice article,

    AWS Training in Hyderabad

    ReplyDelete
  64. I have read your blog and I gathered some new information through your blog. Thanks for sharing the information
    Data Science Training in Hyderabad

    ReplyDelete
  65. Thank you so much for this incredible guide. This has given me so much information
    AI Training in Hyderabad

    ReplyDelete
  66. I was following your blog regularly and this one is very interesting and knowledge attaining. Great effort ahead. you can also reach us for web development company in chennai website design company in chennai

    ReplyDelete
  67. Thanks for sharing this information. I really Like Very Much.
    top devops online training

    ReplyDelete