• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String.java deprecated Package private constructor for speed

 
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to understands/brushup/clear some basics of java.
I compare Oracle JDK 6 & 7 source code.
In java source code, i found that in String.java in 7 as follows:-


in Oracle Java 6


For understand this i try to use code as follows:-


when i compile & debug this code in Eclipse with Java 6 & 7, then i observe after step into(F5) that
when Java 6 then we can see following Package private constructor calls:-
String(int offset, int count, char value[])

When java 7 we can see following Package private constructor calls:-
String(char[] value, boolean share)

But as in comment inside the string.java found in Java 7 there is mention reason behind deprecation is a speed.

I think when we call Integer.toString(i) for lots of time(really lots of time) then might be 'speed' comes in picture.
1. Can anybody clear my thoughts about it?
i try to find out on internet also but fails to get proper guidance.
2. As above small exercise of study, will it helpful for clear ideas for become successful Java developer?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a Java developer for 10 years and I don't know which package-private constructor is invoked when a new String is created. I don't care if one of the package private constructors was changed in Java 7, I assume the Java API developers know what they are doing...
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it is package‑private in a package which lesser mortals cannot change, why not change it. It no more forms part of the public interface of that class than a private field would, so it counts as a private implementation detail.
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie & Roel De Nijs,
My question remains with unclear answers.
When in Java6 constructor 'String(int offset, int count, char value[])' have comment as follows:-

In java 7, what the reason so people deprecate it & write new constructor 'String(char[] value, boolean share)'?
Is it something related to speed which they mention in both cases? What speed?
I am not trying to find something wrong in this..but try to learn things with 'interpretation and justification'.

If it is package‑private in a package which lesser mortals cannot change, why not change it. It no more forms part of the public interface of that class than a private field would, so it counts as a private implementation detail.


Sir, can you explain little simpler language? I doesn't understand whole meaning!
But 'package‑private' is interesting concept i learn new thing (or old thing again comes front)!
I read following related to this concept.
Controlling Access to Members of a Class
Many others which by Google.
But by one question many new questions arises in my mind & i am trying to answering it to myself for satisfaction.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mandar Khire wrote:
My question remains with unclear answers.


In other words, it's an internal implementation detail with which you should not be concerned.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, this change is one of the few times where I think it does matter how it is implemented. The thing is, String's behaviour regarding memory changed quite a bit in Java 7. In Java 6 and before, the String returned by substring shared the same char[] as its "parent" String. This could cause a large memory leak - if you had a large String, then use substring to get a small portion, the entire char[] of the large String is used and therefore prevented from being garbage collected. I've seen people use new String(s.substring(...)) just to prevent this. However, since Java 7, creates another (short-lived) String. In other words, a fix to prevent memory leaks all of a sudden caused more (short-term) memory consumption.
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear Bibeault & Rob Spoor,
As Rob Spoor writes:-

The thing is, String's behaviour regarding memory changed quite a bit in Java 7.


I try to find about substring. & memory leak also.
But my point of view is different.

I found difference in String.java.
But before that i found difference in Integer.java
In JDK 6

in JDK 7


When i debug my example which i posted in first message of this topic, i got knowledge about difference in String.java.
So as in Integer.Java has just change return statement, will it make impact on Memory leak problem?
In String.java, particular constructor related to speed! What speed?
How toString(i) concept co-relate with substring() concept?

Though

In other words, it's an internal implementation detail with which you should not be concerned.


Sorry sir may be my thinking is rude/wrong way but as follows i think:-
As by reading FAQ, i can not find restriction on studying of source of java, so no specific limitation on 'concern'.
If every open source project's knowledgeable people say...'its an internal implementation detail with which you should not be concerned.', then how will people become Kernel Developer, Device Driver developer, u-Boot Developer, Ethical Hacker etc in these topics which i know(just names) needs internal things only. If i try to go 'internal things' in my known area like java, then did i make mistake?
 
I knew I would regret that burrito. But this tiny ad has never caused regrets:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic