• Post Reply Bookmark Topic Watch Topic
  • New Topic

Low Level Knowledge of Strings  RSS feed

 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To my understanding Java has obtained many of its features from C/C++ and refined the language to deal with memory management issues that programmers faced with those languages with the aid of a Garbage Collector...

My personal view is that understanding some of the low level details contained in those languages can help a Java programmer put the language into perspective by comprehending why some things work the way they do...

String literals also known as string constants in source code, which are characters stringed together within double quotes such as "String" are loaded into memory as soon as the application gets executed where in the Java sense will be when a class gets loaded... these literals are placed in the static storage class area in memory where they persist for the lifetime of the application... I am assuming this is what Java refer to as the string constant pool

The compiler places a restriction on these literals that their values cannot be modified hence the reason that they are constants... with this restriction it is safe to let more than one reference refer to such a value, this helps in using memory efficiently.

This would mean that every time you refer to a string literal with the same sequence of characters it actually refers to the one that was previously placed in memory.

Therefore declarations such as:


will mean that both string1 and string2 refer to one value located at a specific address in memory which can hypothetically be 0xFFAC20...

With this observation an expression such as string1 == string2 would yield to be true since the actually value stored in the references would be the value of the address 0xFFAC20 where (0xFFAC20 == 0xFFAC20) yields to be true

There are more details to strings where it comes to initializing char arrays using string literals that also open up a deeper understanding of why they work the way they do...

My conclusion is that it is very true that learning more than one programming language helps an aspiring programmer link the similarities and differences in distinguished programming languages which on the whole would make him/her a better programmer...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what it is worth....NO String can be modified once it is created.

You can create new strings and change where the reference variable points, but the original String object is never changed.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an ideal Java sense a String cannot be modified which are literals and the String object itself... But on a lower level a string can be modified (char array of character elements terminated by a null character '\0') which is what the StringBuilder and StringBuffer classes abstracts from the user...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer and StringBuilder are not Strings.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class String internally stores its content in a character array. Class String has been designed to be immutable, which means that it has no methods that allow you to modify the content of the String after the object has been created and initialized. That is what causes it to be non-modifiable - there's no special compiler magic necessary to make strings non-modifiable. Note that in Java the character array is not terminated with a null character; that's how strings are represented in C, but not in Java.

The compiler converts string literals to String objects. Essentially, string literals are just syntactic sugar, it would be very cumbersome if you could only make strings by explicitly initializing a character array, for example like this:

Imagine you would have to do that for all string literals in your code...

By the way, using reflection you can subvert the immutability of strings. you should never do this ofcourse, but you can mess with the content of a String object:

 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... My understanding is that a string in programming is a sequence of characters (characters stringed together)... StringBuilder and StringBuffer are implemented using an array to represent a sequence of characters which are mutable...

If they are not strings then why does the name of these classes contain the initial word String?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do find that people who think Java® is an extension of C++ are heading for serious confusion; it happens all the time here.

Since the memory management in C and C++ is so different from Java®, learning one does not help the other. Learning about malloc and free may actually hinder correct memory management in Java®.
Also C does not support Strings. It only supports the char[] datatype, which is to all intents and purposes the same as a *char. Also, C “strings” are null‑terminated, which is rather in efficient and means a C “string” is one character longer than it appears to be. The null‑terminated array of chars, which you may be able to modify, does not constitute a Java® String. It even says so in the Java Language Specification.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:Hmm... My understanding is that a string in programming is a sequence of characters

String (capitalized) is an immutable class; string (lowercase) is a concept.

If they are not strings then why does the name of these classes contain the initial word String?

Hamster starts with "ham". Doesn't make them a pork product.

Point is that String, StringBuilder, and StringBuffer are distinct classes, each with their own rules of behavior.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the deep insight and the sharing of expertise on this subject matter...

I must say that I enjoy this site as its an exceptional resource for clarification on any idea that comes to mind

For reading alone in isolation is nowhere near close to what I seem to achieve by gathering knowledge from great minds in real-time
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:For reading alone in isolation is nowhere near close to what I seem to achieve by gathering knowledge from great minds in real-time

Well, let me say, for one, that you've done pretty darn well on your own. Your posts are usually good, and you've actually reminded me of a few things I should know better.

However, I'm with the others here: Trying to think of Java in terms of C or C++ simply isn't very useful - especially when it comes to memory. It's one of the reasons I wrote the DontSweatIt page.

HIH

Winston
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For one thing, I truly respect you guys because the things that you'll point out always make vast amount of sense.

I've always enjoyed advanced learning in the fields that I decide to pursue which I think gets me into trouble as I'm enrolled into university and the topics that the lecturers are covering at present is not of my interest simply because I've already covered them on my own ahead of time. There are stuff that I even have to update them on which they assume to be wrong such as updated standards in the programming languages as I keep close track of such things.

This leaves me in a position where I'm always wondering on my own trying to figure out stuff with only the guidance of text in books and online resources which I may sometimes misinterpret.

I'm currently learning four programming languages on my own simultaneously with the intention of certification as I'm already OCAJ7 certified and wish to pursue up to expert level in each.

Do you'll think I'm going down a dead end?... not really sure of what I'm doing at the moment
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:Do you'll think I'm going down a dead end?... not really sure of what I'm doing at the moment

Hi Rico, it's good that you are interested in learning things and figuring out things of your own, and I think it's also important to validate the idea that you have about how things work with other people, exactly as you did in this topic here. That way you can clear up any misunderstandings that you might have. You're not going down a dead end. I can imagine that learning four programming languages at a time can be intense, but if you can handle it, why not.

A good programmer doesn't limit him/herself to a single programming language. When you're working as a software development professional, you'll have to be able to deal with different technologies and programming languages. For example, on the project I'm working on currently, we're building software in Java and Scala, we are using Groovy (Gradle) for our build system, we have some JavaScript, and we use many frameworks, libraries and other technologies, including Spring, Hibernate, OSGi, Akka, XML, JSON, etc.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jesper for the motivation, I appreciate it. I was beginning to question myself whether I'm taking the right paths...

From your posts I've observed that you're a very knowledgeable I.T professional with in-depth knowledge on a lot of technologies.

May I ask from your experience how long does it take to become proficient in all these various technologies?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see no evidence that you are on the wrong path. Norvig suggests 10000 hours, which is something in the region of 6 years full time. Or ten years in real life.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's some priceless information

I do hope that I can keep up with this marathon as I'll have approximately 8.5 more years again to go according to such research data
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!