Why are Java substrings bad? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have recently started using Java for the first time (I used to primarily use C, C++ or Assembly before this) and ran into substrings. I know that Java characters and strings take up at least double the space the character or string should take. But why are substrings bad? I have been advised by a lot of people to avoid them if possible on processing intensive platforms but Strings are used everywhere in web services which can be very processing intensive, so I am curious as to why so many people have this opinion.

This may be related to how substring() was previously implemented. In earlier Java versions calling substring() on a long String would keep the original String in memory (they would share the internal char[]). This can cause memory issues if the original Strings are kept around in memory unnecessarily.
In Java 8 this is no longer the case (the internal char[] is copied) and you can freely take substrings of even long Strings.

Related

Creating a simple calculator using strings in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to learn Java. My current assignment is to build a simple four function calculator..... this would be easy given if/else and/ or switch statements, but I'm supposed to build this using methods.
The original input has to be put in as a single string, so, in my mind, I'm going to have to take the single string and create substrings, then somehow convert these substrings into double values, while deleting whatever whitespace could possibly be between characters. My current idea is to somehow identify the "+,-,*, or /" within the string and divide into substrings before and after these values, using the appropriate defined method for whichever operator to do the calculations....
The problem is that I can't see a good way to divide these up into substrings or how to convert the numbers involved into double values. Anyone got any advice for me? Keep in mind, what we have gone through is pretty limited and I feel like I'm missing something REALLY simple out there.
You can split a string based on a particular character using str.split("\\+"), for example. You can convert the split pieces of the string to doubles by using Double.parseDouble(str);

Java two collections contains any [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Java two collections contains any
Large two collections A and B contain best best approach in these
1) Collections.disjoint(A, B)
2) org.springframework.util.CollectionUtils
CollectionUtils.containsAny(A,B)
Looking at their respective source codes, it looks like Collections.disjoint is smarter about when its arguments are Sets and have fast contains implementations, and CollectionUtils is a little smarter about checking if either collection is empty, but that's generally a smaller win.
Based on that, I'd use Collections.disjoint, which I'd tend to do anyway just because it's built-in.

Why didn't they design array index to start from 1? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
In many programming languages, the array index begins with 0. Is there a reason why it was designed so?
According to me, it would have been more convenient if the length of the array was equal to the last index. We could avoid most of the ArrayIndexOutOfBounds exceptions.
I can understand when it comes to a language like C. C is an old language and the developers may have not thought about the issues and discomfort. But in case of modern languages like java, they still had a chance to redefine the design. Why have they chosen to keep it the same?
Is it somehow related to working of operating systems or did they actually wanted to continue with the familiar behaviour or design structure (though new programmers face a lot of problems related to this)?
An array index is just a memory offset.
So the first element of an array is at the memory it is already pointing to, which is simply
*(arr) == *(arr+0).

String.substring() making a copy of the underlying char[] value [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
A question relating to performance considerations for String.substring. Prior to Java 1.7.0_06, the String.substring() method returned a new String object that shared the same underlying char array as its parents but with different offset and length. To avoid keeping a very large string in memory when only a small substring was needed to be kept, programmers used to write code like this:
s = new String(queryReturningHugeHugeString().substring(0,3));
From 1.7.0_06 onwards, it has not been necessary to create a new String because in Oracle's implementation of String, substrings no longer share their underlying char array.
My question is: can we rely on Oracle (and other vendors) not going back to char[] sharing in some future release, and simply do s = s.substr(...), or should we explicitly create a new String just in case some future release of the JRE starts using a sharing implementation again?
The actual representation of the String is an internal implementation detail, so you can never be sure. However according to public talks of Oracle engineers (most notably #shipilev) it's very unlikely that it will be changed back. This was done not only to fight with possible memory leak, but also to simplify the String internals. With simpler strings it's easier to implement many optimization techniques like String deduplication or Compact Strings.

Data types usage in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am not new in java but when I write program I always use int Type of my variables. I want to know when I need to use int data, when byte, when long and so on... Can U explain me this with examples please.
If you are asking when to use float, double and long etc. This document can help you to understand.
For example, int is 32 bit but long is 64 bit. If you need to set a value over 32 bit you should use long to store data.
Good luck.
It basically depends on how you wanna use them and what considerations would you have (size, data type,..etc). I would recommend going throw Oracle's docs.
The types used should be the product of a deep thought about various of things, including (but not limited to) int over long (32bit vs 64bit), char over byte (user friendliness vs performance) , complex data structures over simple ones (Performance), backwards compatibility (JRE version), platforms the program's gonna run on (Windows? Unix? Mac OS?), readability of the code (Sometimes writing "byte x = 0xFF; char ch = (char)x; is worse than char ch = 'a' and the list goes on... of course some of the stuff I mentioned fit into more than one category.
This usually comes with experience. The more you code, the more platforms you want to support, the faster you want your program to respond etc...
You should always have a plan regarding your program:
What platforms will I support?
Is the task more important than performance?
...
...
I'm not saying you should carefully consider every type you choose, I'm saying you should always make the effort to tick all the V's and be satisfied about it, accomplishing everything you wanted.

Categories