Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Today I have read that strings are terminated with null character in java. But why is it necessary since length of the string is already maintained by the length variable of the sting class ? So it's kind of strange thing to me. Please someone clarify it to me.
Thanks
Java strings are not null terminated. They end with the length in length. You can make a \0 the last character of a Java string, but that doesn't automatically terminate the string. The length of 12<\0>45 would still be 5 and not 2 as in C.
Don't believe everything you read. In the screen shot below, you can see there is no null-termination.
The documentation mentions nothing of the sort. I'd assume that you read wrong.
If some Java implementation null-terminates strings, then it is an implementation detail, but nothing that is guaranteed by the language specification. (And, of course, as assylias notes, Strings may contain U+0000 in Java.) This isn't unheard of, though. VB's strings pointed to the beginning and were null-terminated, making them compatible with normal C APIs and their length was stored before the beginning of the string.
This tactic of having both a length field and keeping the string null terminated is often employe because many libraries and api's expects null terminated strings.
This means that you still get the performance increase of keeping the length seperate and the ability to directly pass your strings to api's wihtout having to create a copy of the string. In short: the best of two worlds at only a bytes expense.
Related
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 4 years ago.
Improve this question
I am trying to finish an assignment in my intro to Java course, and I have some questions. First off, what does it mean when there is a -- in FRONT of an int value? Also what is a String Builder? I had some help but want to understand what it is I'm using in the code. Thanks.
The -- in front of a value simply means subtract 1 from it. Similarly, ++ in front of a value means add 1 to it.
If you write ++
before the number it is called prefix operator and if after then its post fix
preFix: ++a will increase the value before using it, will first increase and then use it.
postFix a++ will first use it and then use it, for later use you will get the incremented value.
-- is a predecrement
Java StringBuilder class is used to create mutable (modifiable) string.
A String is immutable i.e string cannot be changed once created and everytime a value is change it create new string.
But in case of StringBuilder which is mutable string can change.
My experience is mostly with C# not Java, but in C# strings cannot be changed, when you concatenate two strings like "hello" + "world" you do not change either string, you create a new one and the old two still exist. If you need to do this many times (dozens or hundreds) it can use a lot of memory. A StringBuilder allows you to conserve memory by appending characters to the same block of memory while you are building your string, and then you can turn the result into a normal string for passing around to other functions.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
The normal code works below
ShipSize size = shipSize.startsWith("s") ? ShipSize.SMALL:
(shipSize.startsWith("m") ? ShipSize.MEDIUM: ShipSize.LARGE); // get ship size
Is this the right way to change the code so the string equals a word. I doesn't seem to be working for me.
ShipSize size = shipSize.equals("small") ? ShipSize.SMALL:
(shipSize.equals("medium") ? ShipSize.MEDIUM: ShipSize.LARGE); // get ship size
Any ideas why?
Equals is case sensitive, depending on your input, so you may need to use .equalsIgnoreCase() instead of .equals()
Other than that, you haven't done anything "wrong". You'll need to specify exactly what your input is for more detail.
ShipSize size = "small".equalsIgnoreCase(shipSize.name) ? ShipSize.SMALL: ("medium".equalsIgnoreCase(shipSize.name) ? ShipSize.MEDIUM: ShipSize.LARGE); // get ship size
Assuming ShipSize is an enum, then you can use the .name of the enum to get the String. Also, put the constants first when comparing to avoid possible null pointer exceptions.
EDIT: changed to equalsIgnoreCase.
Note that this assumes that your inputs are actually "small", "medium" or "large" and that your enum is:
public enum ShipSize
{
SMALL,
MEDIUM,
LARGE,
;
}
If this is the case, you can just do:
final ShipSize size = ShipSize.valueOf(shipSize.toUpperCase());
.valueOf() will throw an IllegalArgumentException if the constant does not exist, so if the risk exists you should catch that and recover appropriately.
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
hi i have a java programming assignment wich include 3 exercice i have done 2 already but in the third one i am dont know wich data type i should use
here is the question :
Write a java program that reads from the user the course code, section and the scores of a student for three exams representing two midterms and a final. The percentage of each of the three exams is of fixed value 30, 30 and 40 percent respectively. Your program computes the final average grade and displays it along with the course code and the section.
Remark: All data, except for the average, must be whole numbers and you should use the most efficient data type that is suitable for this specific exercise.
Sample Run:
Enter your course code: CSCI250
Enter your section: E
Enter the scores of the tests and the final: 97 83 77
CSCI250 E 84.8 (result)
so what i want to know is what is the preferable data type to use for course code ? and char is the one that i should use for section right ?
If you're capturing user input, use a String for everything.
Reason? You may request a number, but the user can type anything. Your code needs to handle bad input.
I think you can use String as data type for Course Code. You can write both numbers and letters by using it. And for section, yes, char will be suitable for it.
Use a String for arbitrary text.
If the section code is always present1 and is never more than character than a char can be used.
However, I would still use a String for consistency, flexibility, and easy of use. The teacher may prefer this based on the "efficient"cy they are going for.
1The is a soft "always": while char cannot represent null a sentinel (eg. '\0' or ' ') can be used to indicate 'no section specified'. Using such a sentinel to supplement null can also (but does not always) lead to more logic work - in particular when the record is displayed.
In any case, it is probably best to not switch to Character just for the null as this is most likely outside of the scope of current course work.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to detect the file structure in a string.
e.g
if I have a string as /name/test/testme/2 I should be able to store it in a arraylist as different elements like {[name],[test],[testme],[2]}
String[] elements = "/name/test/testme/2".split("/");
More info can be found in the String.split() Javadoc
As Lukas pointed out, (please give him some upvoting) you should use the split method.
String[] elements = "/name/test/testme/2".split("/");
The regular expressions are not used to split strings in sections. Regular expressions are used for matching the target string with a specific generic format. In this case a boolean value indicating if the strings match is returned.
Hope I helped!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
here is the non-oop way of doing it:
When someone presses a day on a calendar, we need to find out the day, and path. Here is the non-oop way of storing that information:
A string was created with a "-" delimiter between each piece of info we need, like this:
12-c:\files\john_doe.png
Then it was stored in an array. However, to retrieve the data, we then use the "split" function like this.
for (int t = 0;t < day_and_path.length;t++)
{
String[] day_from_db = day_and_path[t].split("-");
String day_db = day_from_db[0];
String path_db = day_from_db[1];
However the OOP way is, make a class with properties: day, path. Then store them into an array of objects.
Which way is better and why?
If you use String#split, you'll be dealing with an array of strings. If all you do is assign the strings to local variables, then there is no point in making a class for that.
If you want to hold on to the string parts and pass them around to other methods, then it begins to make sense to have an object which will conveniently encapsulate those strings.