• Post Reply Bookmark Topic Watch Topic
  • New Topic

String h?  RSS feed

 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Somebody can explain what String h is?
I have a example:


Thanks ;-)
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
h is a variable; specifically, it's a local variable.

A variable is like an envelope with a name written on it. You can put a specific kind of data into the envelope, which you specify when you define the variable. Variables are terribly important in programming: without defining a variable, you have no place to put data and no way to refer to it.

Here, we say

String h = "";

and that means:

Create a variable named 'h' that can hold Strings, and then put the empty String "" into it.

The subsequent code uses the variable h to hold, in turn, each line of text read from (someplace).
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems a bit basic for the Intermediate forum, unless we've misunderstood what your question is asking.

On another note, it seems unintuitive to initialise h to "" (the empty string). Setting it to null would make more sense to me, also avoiding adding the empty string to the string pool. But that's just my preference
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Charles Lyons:
This seems a bit basic for the Intermediate forum

I tend to agree. All I can see in that example is quite basic syntax; the only thing not really basic is the initialization inside the loop.

Movig to beginner.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like basic syntax; even the while(()) bit is something beginners will have to learn off by heart. If you need more explanation, feel free to ask.

Calling a String h is a bit unusual; maybe "line" would be a better name.

The = "" bit is unnecessary; it will work if you simply write String h;

I would agree with Charles that initialisation to null might, in this instance, be better.

Somebody has seen errors caused by local variables not being initialised before they are used, and has initialised h to the empty String. In this instance there is no need to initialise it like that because the while(()) is sure to initialise h when it is executed.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the return type of the StringBuilder#append method. If it is StringBuilder, you can enhance your append call like this

sb.append(h).append('\n');

You can enhance that even more by calling the System.getProperties() method, and finding which Property ends lines. It is probably called something obscure like line.end or line.terminator. This little app will go through all the properties on your JVM and print them out.It will only compile on Java5 or Java6. And it gives a hint that I was mistaken about "line.end".

Precede your while with another declaration like this:
The rationale is that line ends vary; *nix uses \n, old Macs use \r and DOS/Windows uses \r\n; \r will print as d (ctrl-M) and \n prints as a (ctrl-J). This trick will ensure you are always using the line end appropriate to your operating system.
[ December 20, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's something wrong with the Integer.parseInt() bit. I shall work on it and report back in a few minutes.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's working. It will not work on Java1.4.2 or older.
[ December 20, 2008: Message edited by: Campbell Ritchie ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Charles Lyons:
On another note, it seems unintuitive to initialise h to "" (the empty string). Setting it to null would make more sense to me, also avoiding adding the empty string to the string pool. But that's just my preference

Even setting it to null is totally unnecessary, because in the next line another value is assigned to h.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!