Results 1 to 8 of 8
Like Tree5Likes
  • 1 Post By jim829
  • 2 Post By milovan
  • 2 Post By gimbal2

Thread: Java Beginner: Question

  1. #1
    Gunnadin is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Java Beginner: Question

    I'm currently learning Java from a book called "Java: A Beginners Guide Sixth Edition" by Herbert Schildt. I'm now learning about variables and I don't understand why you must declare it first instead of doing it the way I do. I believe my way of doing it will save me typing time.

    The book's way:
    Java Code:
     int var1; // this declares a variable
    int var2; // this declares another variable
    var1 = 75;
    var2 = 100;
    My way:
    Java Code:
    int var1 = 75; // this is my way of declaring a variable
    int var2 = 100; // this is my way of declaring a variable
    Does it make any difference which way I choose to do it?
    Last edited by Gunnadin; 04-27-2014 at 07:57 PM.

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    10

    Default Re: Java Beginner: Question

    Welcome to Java,
    Both ways are possible and depending on implementation more suitable or not. You will pretty soon start to recognize which one is more appropriate by continuing your studies.
    For now you can use both ways..

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Java Beginner: Question

    There are some subtleties, especially when initializing arrays.

    Java Code:
    int [] a = {1,2,3}; // okay
    int [] x;
    x = {1,2,3}; // compiler error - must do it like x = new int[]{1,2,3}
    Regards,
    Jim
    Hieu App likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Gunnadin is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Re: Java Beginner: Question

    Ok... and thanks for replying. You helped me a lot.
    Also one last question:
    (1) This book is teaching me words that I find hard to grasp. Do you think I'll start to remember these words as I move along in my studies?

  5. #5
    Gunnadin is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Re: Java Beginner: Question

    I'm not on Arrays yet... I only know System.out.println(""); and now I'm learning variables

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    10

    Default Re: Java Beginner: Question

    As I said, just go ahead and it will start to be more and more "visible" to you. Of course, we will be here to help..
    Gunnadin and Hieu App like this.

  7. #7
    Gunnadin is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Re: Java Beginner: Question

    Okay I will and thanks so much for the help...

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Java Beginner: Question

    Just to note the thing not mentioned yet, this is just a case of personal preference regarding "correctness" and readability. Some people like to declare all variables at the top of the method before they start to use them, some people just declare them wherever they are needed and initialize them on the spot. Usually when I see a Java programmer declare all variables at the top, I think "Ah, you've been exposed to either C or Javascript for a significant amount of time" :)

    IMO: your way (directly initialize variables) is better in the context of Java.
    Gunnadin and R03 like this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Beginner question but not new to Java
    By scottg in forum New To Java
    Replies: 4
    Last Post: 06-03-2012, 09:00 PM
  2. Beginner Java Question
    By chancock82 in forum New To Java
    Replies: 4
    Last Post: 01-28-2012, 07:21 AM
  3. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 04:01 PM
  4. Beginner Question About Java Coding
    By Humphrey Bogart in forum New To Java
    Replies: 3
    Last Post: 03-09-2011, 09:41 PM
  5. Beginner Java question
    By DanK in forum New To Java
    Replies: 3
    Last Post: 04-27-2009, 04:29 AM

Posting Permissions

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