Jump to content

Java - Are Strings Arrays?

Guest
Go to solution Solved by Tazman192,
6 minutes ago, IAmLamp said:

How is that a new object though? 

I added a little more to my post to explain. I'll add a little more here.

 

Say you create the string:

 

String x = "foo";

This is implicitly saying:

String x = new String("foo");

When you want to change x to "bar:

 

x = "bar";

This is again implicitly saying:

 

x = new String("bar")

Meaning, because we used the "new" keyword, we have created a NEW String object, and we have pointed an existing variable to point to this newly created object. This is opposed to a mutable type such as an int - you don't implicitly create a new int everytime (or at all - int's aren't objects, they're primitive), you overwrite your existing int..

Just wondering if Strings are arrays in Java. from what I've been currently doing, it doesn't seem like it. I believe I was told Strings are arrays of chars. 

Link to comment
Share on other sites

Link to post
Share on other sites

I posted something originally but the person below wrote a clearer explanation :)

Link to comment
Share on other sites

Link to post
Share on other sites

In some languages, such as C, there exists no String types, so you have to declare an array of characters - that's where you might have thought Strings were an array of characters. Generally speaking, in Java you get primitive types and object types.

 

Primitive types are your basic types: int, double, float, long, boolean, short, byte.

Object types include Strings, and anything else that need instantiated (using the new keyword) e.g. Car car = new Car();.

 

 

To answer your question: Strings are stored as an arrary of characters, and also executed as an array of characters, but they differ from an array of characters in that Strings are immutable, basically meaning that once a string is created in memory ("String x = "Hello, World!";), that string cannot be changed. There's also other small differences such as where in memory the variable is stored.

 

Generally, you shouldn't ever use an array of characters. Using a string is more sensible, and if you do need to mutate your string, look into using StringBuilder.

 

 

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Tazman192 said:

In some languages, such as C, there exists no String types, so you have to declare an array of characters - that's where you might have thought Strings were an array of characters. Generally speaking, in Java you get primitive types and object types.

 

Primitive types are your basic types: int, double, float, long, boolean, short, byte.

Object types include Strings, and anything else that need instantiated (using the new keyword) e.g. Car car = new Car();.

 

 

To answer your question: Strings are stored as an arrary of characters, and also executed as an array of characters, but they differ from an array of characters in that Strings are immutable, basically meaning that once a string is created in memory ("String x = "Hello, World!";), that string cannot be changed. There's also other small differences but this should answer your question.

 

 

But they are not immutable, for example

 

String s1 = "Example here. ";

 

s1 = "Edited string";

 

And this would work fine. So my understand is that if you are to change a string, you can only do the entire String, not just individual pieces. 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, IAmLamp said:

But they are not immutable, for example

 

String s1 = "Example here. ";

 

s1 = "Edited string";

 

And this would work fine. So my understand is that if you are to change a string, you can only do the entire String, not just individual pieces. 

When I say "immutable", I mean in memory. When you first do String s1 = "Example";, you're creating a new String object and giving it the value "Example".

 

When you do s1 = "Edited", you are actually again creating a new object, as opposed to editing the existing object.  You create the new string "Edited", and then change the reference of s1 to point from "Example" to "Edited".

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

when you call s1 = "Edited string" you aren't changing the string, you are changing the reference of the string s1 to a new object that is created by "Edited string".

 

The object s1 itself is not changing, only the reference to it, from "Example here." to "Edited string"

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, elpiop said:

when you call s1 = "Edited string" you aren't changing the string, you are changing the reference of the string s1 to a new object that is created 

How is that a new object though? It's using the already existing string and not creating a new one

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, IAmLamp said:

How is that a new object though? 

I added a little more to my post to explain. I'll add a little more here.

 

Say you create the string:

 

String x = "foo";

This is implicitly saying:

String x = new String("foo");

When you want to change x to "bar:

 

x = "bar";

This is again implicitly saying:

 

x = new String("bar")

Meaning, because we used the "new" keyword, we have created a NEW String object, and we have pointed an existing variable to point to this newly created object. This is opposed to a mutable type such as an int - you don't implicitly create a new int everytime (or at all - int's aren't objects, they're primitive), you overwrite your existing int..

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Tazman192 said:

I added a little more to my post to explain. I'll add a little more here.

 

Say you create the string:

 


String x = "foo";

This is implicitly saying:


String x = new String("foo");

When you want to change x to "bar:

 


x = "bar";

This is again implicitly saying:

 


x = new String("bar")

Meaning, because we used the "new" keyword, we have created a NEW String object, and we have pointed an existing variable to point to this newly created object. This is opposed to a mutable object such as an int - you don't implicitly create a new int object everytime, you overwrite your existing int object.

Jesus fucking christ. Why didn't they teach me this for fuck's sake?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, IAmLamp said:

Jesus fucking christ. Why didn't they teach me this for fuck's sake?

It's not considered "important" to most people - I only learned this stuff because I get into the really theoretical parts of programming languages at Uni. This is fine grained, and personally as a beginner developer, nothing you should worry about.

 

char arrays are rarely used, so just use Strings for block of words. Only time I've ever used an array of chars is for passwords, because of the reasons discussed above. You don't want to use an immutable data type for sensitive objects such as passwords, you want to be able to overwrite that data once you've authorized a password for example, and a char array lets you do that.

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Tazman192 said:

It's not considered "important" to most people - I only learned this stuff because I get into the really theoretical parts of programming languages at Uni. This is fine grained, and personally as a beginner developer, nothing you should worry about.

 

char arrays are rarely used, so just use Strings for block of words. Only time I've ever used an array of chars is for passwords, because of the reasons discussed above. You don't want to use an immutable data type for sensitive objects such as passwords, you want to be able to overwrite that data once you've authorized a password for example, and a char array lets you do that.

Ironically this type of work/question is for a password project lmao

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, IAmLamp said:

Ironically this type of work/question is for a password project lmao

Lmao then I'd advise using a char of arrays. http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords-in-java

 

The above link is a very worthwhile read and dwells into exactly why char arrays are safer for passwords (or any sensitive data for that matter).

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

Kind of strange how 

 

int q = new int(5); 

 

doesn't work

 

oh well, at least I learned some stuff

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, IAmLamp said:

Kind of strange how 

 

int q = new int(5); 

 

doesn't work

 

oh well, at least I learned some stuff

Remember how I described primitive and non primitive (object) types in my first post? Int, float, double, boolean etc are primitive types.

 

With primitive types you don't create objects. The "new" keyword is reserved only for instatiating a class (in other words, creating an object). So, because int isn't an object type, there's nothing to be instantiated (there's no int class), so the new keyword isn't needed, nor would it work.

 

There is a String class however, and all of the methods in the String class (as well as its constructors for creating a String object) can be found here https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

 

You know how constructors etc work?

 

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

Would the same ideology be for like ints and doubles then?

 

int iV1 = 1234;

double dV1 = 1234;

 

The same idea being that you can not change individuals characters of the types? It seems like ints and doubles share a bit of the same characteristics as strings do. 

 

I'm seeing ints and doubles as "arrays" of ints and/or doubles

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Tazman192 said:

Remember how I described primitive and non primitive (object) types in my first post? Int, float, double, boolean etc are primitive types.

 

With primitive types you don't create objects. The "new" keyword is reserved only for instatiating a class (in other words, creating an object). So, because int isn't an object type, there's nothing to be instantiated (there's no int class), so the new keyword isn't needed, nor would it work.

 

There is a String class however, and all of the methods in the String class (as well as its constructors for creating a String object) can be found here https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

 

You know how constructors etc work?

 

CAM object oriented, yeah

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, IAmLamp said:

Would the same ideology be for like ints and doubles then?

 

int iV1 = 1234;

double dV1 = 1234;

 

The same idea being that you can not change individuals characters of the types? It seems like ints and doubles share a bit of the same characteristics as strings do. 

 

I'm seeing ints and doubles as "arrays" of ints and/or doubles

No, the same ideas don't apply.

 

The ideas of immutability apply ONLY to non-primitive (object) types. Doubles, ints, booleans etc are primitive, and so their memory is rewritten rather than new doubles etc being created and the reference being changed.

"The only person you are destined to become is the person you decide to be."


CPU: Intel i5 4690K - Motherboard: Asus Maximus VII Ranger - RAM: Corsair Vengeance LP - 2x4GB @ 1866Mhz - GPU: MSI Twin Frozr GTX 770 4GB - CPU Cooler: Be Quiet! Dark Rock Pro 3 CPU Cooler - PSU: EVGA SuperNova G2 750W - Storage: Seagate Barracuda 2TB HDD- Case: Fractal Design Define R4 Windowed (with Red AKASA Led Strips) - Display: Benq GL2460HM 24" Monitor

Link to comment
Share on other sites

Link to post
Share on other sites

So just to be clear then, I wouldn't be able to change just the specific character of a String then to something else right? Like after the string has been declared 

 

@Tazman192

Link to comment
Share on other sites

Link to post
Share on other sites

Don't know how exactly strings are implemented in Java, but at a basic level, yes. They contain arrays of characters, or lists, or whatever. I'm not sure of how exactly they're implemented in Java, but things shouldn't differ to much coming from C++'s strings.

In essence, a string is a container class that contains a character array and automatically manages it for you.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, IAmLamp said:

So just to be clear then, I wouldn't be able to change just the specific character of a String then to something else right? Like after the string has been declared 

 

@Tazman192

In Java, no. Strings are immutable for various reasons. The only way to do that would be to create a new string with whatever you wanted then replace the other one.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Nineshadow said:

In Java, no. Strings are immutable for various reasons. The only way to do that would be to create a new string with whatever you wanted then replace the other one.

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

To really get this you need to understand the difference between "the stack" and "the heap". The stack is where the variables declared locally are held in memory when your program is running. The Stack is a "Last in First Out" order, (If you have a stack of papers you and stack them up one by one and took them off one by one the last one in would be the first one out). When you have a primitive it's all held in the stack. When you create a new object the variable (i.e. "string myName")  in the stack holds a pointer to a memory location in the heap where the the string is held. More correctly it since it's an array and arrays are contiguous (meaning one after another) memory it only holds a pointer to the first letter. (i.e. string myName = "Stephen"; it would only point to the 'S'.)

 

TL:DR with string myName = "Stephen"; myName on the stack with a pointer to the 'S' in the heap.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×