Results 1 to 6 of 6
Thread: Beginner Questions
- 06-02-2010, 05:17 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 1
- Rep Power
- 0
Beginner Questions
Hey guys, I've just started learning Java and I have some questions which I would like to ask. Unless otherwise stated, all of the below are text from a book which I am reading from:
1) When using notepad, why is there a need to use spaces instead of tabs for indentations? I personally use the Eclipse IDE and I have tabbed my way through without any errors so far. Is there a reason for this in notepad?
2) When saving a .java file using notepad, I was told to save it with quotes (i.e. "Hello.java"). I re-read the paragraph multiple times and yes, the authors want the quotes in. Why is that so?
3) It was recommended that doubles be used in place of floats since doubles could hold a wider range of numbers. However, ints were recommended where possible over longs as "using less storage means your computer will run faster because there's more free space." In view of the fast computers we have today, is this actually relevant?
4) "Usually, you'll want to initialize each named constant to a single hard-coded constant....But be aware that it's legal to use a constant expression for a name constant initialization value." - I understand the first sentence, but not the second.
5) The following is with regards to writing a program such that it terminates when the user keys in "q" or "Q".
The provided code was:
The book then goes on to explain that this will not work because The response string variable and the "q" string literal both hold memory addresses that point to string objects; they don't hold string objects themselves. I do not quite get the bolded text.Scanner stdIn = new Scanner(System.in);
String response;
System.out.print("Enter q or Q :");
response = stdIn.nextline();
if (response == "q" || response == "Q")
{
System.out.println("Bye");
}
- 06-02-2010, 07:25 PM #2
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
Some answers
1) I don't see any FUNCTIONAL reason why tabs cannot be used
when using Notepad. The Java compiler is flexible enough to
hand both kinds of spacing.
2) The author uses double quotes because he has never learned
any other way, or somehow this harmless restriction was not
caught in the proof-read of his text. I almost always use Notepad
and I never use double quotes when saving a .java file.
3) Answers to this question may be anecdotal, because they refer
to beliefs about the computer hardware as viewed from a
programmer's understanding and experience.
3A) I have created a wireframe model "engine" that upon
rendereding a model, made it look moronic. The problem was fixed
when I switched from "float" to "double" number types.
3B) The integer statement is probably true. The reason lies in
computer hardware design. In the 1980-1990s, before Windows 95,
integers were 16 bits in length. Back then, a processor could
RELIABLY add two 16 bit values in a single clock tick. With
Windows 95, the integer was bumped up to 32 bits because
accumulator speed could reliably produce 32 bit results. Today,
we can probably make a commercial grade accumulator that
can reliably add 128 bit values (or greater. Ask an electronics
engineer).
In short, I'm agreeing with this statement and pointing out
that there is an "economy of scale" that supports this belief.
This statement:
Means the processor's accumulator adds a long in two clock"using less storage means your computer will run faster because
there's more free space."
ticks, taking twice as much time to process while using twice
as much memory to store the values than integers would.
Its relevance depends on the application you are writing.
4) I don't know.
5)I understand the point that is trying to be made here; that
each unique string is created and stored only once, then
referenced, but I think in this instance it should work. I'll
have to test it (some day).
- 06-02-2010, 09:34 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 06-02-2010, 10:23 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Basically it is saying just because the String objects have the same data, it doesn't mean they are the same object. If you look at the example, there are three String objects being created,
Since they are all different Objects, they each take up their own space in memory and store their data separately.Java Code:String q = "q"; String Q = "Q"; String response = stdIn.nextLine();
For regular Java Objects(non primitives), the '==' operator checks the memory addresses of the objects being compared, and only returns true if they are the exact same object.
- 04-09-2012, 07:54 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
Re: Beginner Questions
Fairly new at java programming, but I thought I'd add what I know in response to these questions:
1) By default, the standard notepad, at least in WinXP, displays a tab as the equivalent of 8 spaces. Also, Netbeans displays tabs as 8 characters, but when you press tab while coding, it converts it to 4 spaces. I'm not sure what Eclipse' defaults are, but what the author is getting at I would assume is that he wants your code to look the same, regardless of what you use to edit it. If you always use spaces, then the indenting you see in notepad will look exactly the same in Netbeans and Eclipse. Also, because notepad displays tabs as 8 spaces, once you get 3 or 4 levels deep inside your braces with a tab indenting the code another time at each level, you don't have as much space to see the code, as it will run off the right edge of your screen. Personally, I prefer tabs, and so I use a different notepad downloaded from the web, and configured NetBeans to display tabs as only 4 spaces. I would argue that either way is fine, personal preference.
2) In Windows notepad by default, when you save a file, it likes to default to ".txt". If you don't put a ".txt" at the end of your file name, many times, it tries to help you and adds it for you when you hit the save button. You can work around that by, either surrounding the file name in quotes, or by changing the 'Save as type' drop down to "All Files". In addition, by default, Windows Explorer hides the extensions of most file types. So if you save a file as "helloworld.java", then notepad, trying to be helpful will add the .txt onto the end, in Explorer, it looks like the file is named "helloworld.java", when in fact, it's really "helloworld.java.txt". The author could've explained this, but chose instead to just give you the workaround without telling you why. I won't disagree with his choice though, because I've been in programming classes with some folks that obviously shouldn't be there and they'd get more confused by the explanation than the workaround.
4) I agree, that's hard to read. Basically, it means, "You can set a constant variable using either a constant value or an expression."
public static final int HOURS_IN_DAY = 24;
public static final int MINUTES_IN_DAY = 24 * 60; // This is more clear than just assigning the constant, 1440
public static final int SECONDS_IN_DAY = 24 * 60 * 60; // This is more clear than just assigning the constant 86400
or you could take that further and say:
public static final int HOURS_IN_DAY = 24;
public static final int MINUTES_IN_HOUR = 60;
public static final int SECONDS_IN_MINUTE = 60;
public static final int MINUTES_IN_DAY = HOURS_IN_DAY * MINUTES_IN_HOUR; // Probably overkill for this example, but there may be situations where this helps the next coder understand what you're doing
public static final int SECONDS_IN_DAY = HOURS_IN_DAY * MINUTES_IN_HOUR * SECONDS_IN_MINUTE; // Probably overkill for this example, but there may be situations where this helps the next coder understand what you're doing
5) You can use the == comparison to compare the contents of native data types only (byte, short, int, long, float, double, char, boolean) . If you're comparing objects (A String is an object), then the == comparison is comparing memory locations, not the actual content of the variable
String a = "Hello World";
String b = "Hello World";
if (a == b) This evaluates to false because the memory location of each variable is different
(a.equals(b)) This evaluates to true because the "equals" method of String objects compares the contents rather than the memory location
- 04-10-2012, 10:53 AM #6
Re: Beginner Questions
The question was asked two years ago. Please don't wake dead threads.
This is wrong:
Primitives or primitive types, not "native data types"5) You can use the == comparison to compare the contents of native data types only (byte, short, int, long, float, double, char, boolean) . If you're comparing objects (A String is an object), then the == comparison is comparing memory locations, not the actual content of the variable
The == operator compares for equivalence; it will be true for primitive variables (or literals) of equal value or reference variables of equal value. Reference variables are equal when they refer to the same object. Nothing to do with "memory location" which Java doesn't define.
If you have any questions about that, please start a new thread. I'm closing this one.
db
THREAD CLOSEDWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Beginner, need a little help
By jimmy-lin in forum New To JavaReplies: 6Last Post: 10-10-2009, 01:00 AM -
beginner here...help please
By shroomiin in forum New To JavaReplies: 6Last Post: 09-15-2009, 11:06 PM -
Beginner needs help!
By Polyy in forum New To JavaReplies: 1Last Post: 11-27-2008, 05:12 AM -
almost done...beginner needs help plz..
By shongo in forum New To JavaReplies: 15Last Post: 11-10-2008, 08:14 AM -
total beginner needs little help
By asambasamba in forum New To JavaReplies: 1Last Post: 06-18-2008, 05:33 PM


LinkBack URL
About LinkBacks


Bookmarks