This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String.java deprecated Package private constructor for speed  RSS feed

 
Mandar Khire
Ranch Hand
Posts: 573
Android Java Linux
  • Mark post as helpful
  • send pies
  • 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?
 
Roel De Nijs
Marshal
Posts: 10994
161
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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...
 
Campbell Ritchie
Sheriff
Posts: 53734
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 573
Android Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • 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: 573
Android Java Linux
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!