Can Java ever be based on UTF-8, like 'Go' is? [closed] - java

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
Java is currently UTF-16 natively. I know there are ways to convert to UTF-8.
With Unix already being UTF-8 based (only reference it as Java is mostly run on 'nix), how difficult would it be for Java to get into the UTF-8 arena natively, like the rest of the world is leaning towards, for more efficiency?
Will it involve a total rewrite of the language ?

The problem with UTF-8 is that you cannot implement charAt method with O(1) performance. There are many code in the world which rely on this. Something like:
for(int i=0; i<string.length(); i++) {
char c = string.charAt(i);
...
}
If you switch to UTF-8, looking for i-th character will be O(n), thus such code will become O(n^2) which can become performance disaster.
As for efficiency there's a proposal to revive compressed strings in Java: strings consisting solely of ASCII-7 characters can be stored in byte[] array. As far as I know, this feature is being actively developed and the are chances that it will be included in JDK9.

Related

Is there a way to use python and java in the same program? [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 want to use both python and java in the same program. Since the print() function of python is better, but java's int variable; is more efficient.
If I'm interpreting correctly, you want to use to use both interchangeably in the same file, so you'd end up with code like:
def main():
int x = 5;
print(x)
This is impossible, because there would be ambiguity when trying to interpret code if you allowed constructs from both languages. For example, "X" + 1 is allowed in java, and would give you the string "X1". In python, it would give you an error because you can't add an int to a string. This would mean that there would be no way to know what your code should do because it's runnable in both languages.
This is a problem that all of us face, where we like some parts of some languages and other parts of other languages. The solution is pretty much just to decide what's most important, choose one language based on that, and then put up with the parts you don't like.
You can use Jython, which is a Python implementation based on the JVM/JDK. This allows calling between Java and Python code in both directions.

Java Lambdas and Streams: pass streams around [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
I have a fairly dumb question. We all know that stream can have a number of intermediate operations but the real computation is done only when we call some terminal operation. Is it common to pass streams around without calling terminal operation for a long time?
Let me explain what I mean. Consider the following example with Iterator/Iterable
Read file with buffered reader in lines, return Iterator with
next() overrided to call reader.readLine()
In the upper class use guava's Iterators.transform to say lowercase
everything.
In the upper class wrap with another Iterator which may be split's
the line from upstream iterator by coma and returning tuples of
words in the line
In the final class consume the iterator by iterating over it and
writing to some OutputStream.
With all that I have completely lazy computation done from the start till the end. No intermediate collections are used, etc.
If I wanted to do the same with streams I guess I should pass around the Stream object itself. Is it very common to do so? Can you share some links with me?

Take a result of two long numbers using an array in Java [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 7 years ago.
Improve this question
I want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm

Why doesn't java.lang.Short have a reverse() method similar to java.lang.Integer [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 8 years ago.
Improve this question
Why doesn't java.lang.Short (or Float or Double) class have a reverse method similar to java.lang.Integer ?
They both do have reverseBytes method though.
Why isn't the API list consistent ?
short can hold 2 bytes and it would have made sense to have a reverse method as well.
Wouldn't it ?
Thanks
While I agree on the API criticism, it's pretty simple to emulate:
short input = ...;
short reversed = (short)(Integer.reverse(input) >> 16);
So maybe the answer is:
Not enough people felt it was necessary
It's easy enough to simulate
Someone wanted to show off with the implementation of Integer.reverse()
Every line of code needs to maintained. Less code == less bugs, lower cost, easier maintenance.

Is introducing underscores in literals in java beneficial or a drawback? [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 9 years ago.
Improve this question
Though oracle technotes state that :
In Java SE 7 and later, any number of underscore characters (_) can
appear anywhere between digits in a numerical literal. This feature
enables you, for example, to separate groups of digits in numeric
literals, which can improve the readability of your code.
example : float pi = 3.14_15F;
is same as
float pi = 3.1415F;
But does it not become confusing to the developers working on code written by someone else?
Also does the use of underscore put any overhead on compiler or not?
But does it not become confusing to the developers working on code written by someone else?
Only if the developers don't understand the Java language! This construct has been been supported for long enough that every Java professional should recognize it ... even if they don't use it in their own code.
On the other hand, if your Java developers have not bothered to keep up to date with the new things in Java 7, they may be (temporarily) baffled. But the real solution is to educate your developers.
Also does the use of underscore put any overhead on compiler or not?
The overhead would be so small that it is impossible to measure.
There is no performance issue here.
The only time it would make any sense to use underscores is in a very large integer or with a binary integer. Like almost any bit of syntactical freedom the language provides, people are free to misuse it and write difficult to read code. I doubt this underscore thing will become a problem any more than the freedom to add extra white space is a problem.
The best example for when you would want to use this is with binary numbers where it is customary to place a space between every 4 bits.
For instance, compare:
int bitField = 0b110111011111;
int bitField2= 0b1101_1101_1111; //clearly more readable.
Other examples might include a credit card number or SSN as given in Oracle's documentation of this feature.

Categories

Resources