Results 1 to 6 of 6
  1. #1
    elsenoire is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Default 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:

    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");
    }
    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.

  2. #2
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default 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:
    "using less storage means your computer will run faster because
    there's more free space."
    Means the processor's accumulator adds a long in two clock
    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).

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by elsenoire View Post
    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.
    Have a look:

    Java Code:
    public static final int FOO= 21;
    public static final int BAR= 2*FOO;
    kind regards,

    Jos

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Quote Originally Posted by elsenoire View Post
    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.
    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,
    Java Code:
    String q = "q";
    String Q = "Q";
    String response = stdIn.nextLine();
    Since they are all different Objects, they each take up their own space in memory and store their data separately.

    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.

  5. #5
    Vizoere is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default 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

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,199
    Rep Power
    19

    Default Re: Beginner Questions

    The question was asked two years ago. Please don't wake dead threads.

    This is wrong:
    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
    Primitives or primitive types, not "native data types"
    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 CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Beginner, need a little help
    By jimmy-lin in forum New To Java
    Replies: 6
    Last Post: 10-10-2009, 01:00 AM
  2. beginner here...help please
    By shroomiin in forum New To Java
    Replies: 6
    Last Post: 09-15-2009, 11:06 PM
  3. Beginner needs help!
    By Polyy in forum New To Java
    Replies: 1
    Last Post: 11-27-2008, 05:12 AM
  4. almost done...beginner needs help plz..
    By shongo in forum New To Java
    Replies: 15
    Last Post: 11-10-2008, 08:14 AM
  5. total beginner needs little help
    By asambasamba in forum New To Java
    Replies: 1
    Last Post: 06-18-2008, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •