Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java basics

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Java basics

    whats the difference?
     </** Defines the student's forename. */
      private String forename = null;
     
      /** Defines the student's surname. */
      private String surname  = null;
     
      /** Defines the student's age in (whole) years. */
      private int    age      = 0;
     
     **** or***** 
     
    /** Defines the student's forename. */
      private String forename;
     
      /** Defines the student's surname. */
      private String surname ;
     
      /** Defines the student's age in (whole) years. */
      private int  age  ;>


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java basics

    One assigns a value to a variable when it is defined.
    The other does not and lets the compiler give a default value to the variable.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java basics

    Thanks

  4. #4

    Default Re: Java basics

    integers are assigned 0 value by default, and objects (String is one of) are assigned null by default.
    So there is no difference if you write:

    int age = 0;

    and

    int age;

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java basics

    Quote Originally Posted by nambill View Post
    integers are assigned 0 value by default, and objects (String is one of) are assigned null by default.
    So there is no difference if you write:

    int age = 0;

    and

    int age;
    so is it a better style of coding

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java basics

    It depends on if the variable is at the class level or inside of a method. Local variables must be given an initial value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. I need help with basics.
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 8
    Last Post: December 5th, 2011, 05:09 AM
  2. [SOLVED] Basics With UDP Packet
    By servalsoft in forum Java Networking
    Replies: 0
    Last Post: October 13th, 2011, 12:47 PM
  3. Need a little help with the basics
    By Java Neil in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 17th, 2011, 10:54 PM

Tags for this Thread