Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Very Basic Java Q

Options
  • 05-12-2012 1:27pm
    #1
    Registered Users Posts: 82 ✭✭


    I'm only beginning in java and can't seem to comprehend initializing at all.Any definitions I seem to get are just too jargon filled for me to understand.So what exactly is initializing and why do you need to initialize a string?(if it's any help i'm working with set and get methods)


Comments

  • Closed Accounts Posts: 799 ✭✭✭Logical_Bear


    iniatializing a variable is basicaly giving a variable a value.
    int x=134;
    string myString="yes";
    

    if you dont iniatialize a varible then they just have their default values.Zero for int,long etc and false for boolean and null for strings.


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    iniatializing a variable is basicaly giving a variable a value.
    int x=134;
    string myString="yes";
    

    if you dont iniatialize a varible then they just have their default values.Zero for int,long etc and false for boolean and null for strings.

    That should read String myString = "yes";


  • Closed Accounts Posts: 799 ✭✭✭Logical_Bear


    ChRoMe wrote: »
    That should read String myString = "yes";
    lol yes it should my bad.I'm always at it too:o


  • Registered Users Posts: 2,781 ✭✭✭amen


    Forget computers. Imagine you have a big room full of boxes (Thats the computer memory). The boxes may have items in them or may not.

    When you declare a variable (int x ) you are taking a box from the room and using it. As this stage the box you picked may or not have items in it and may/may not be able to hold what you want (ints, strings etc)

    If you tried to use the box (x = x +1) then you cannot be sure what will happen. Is the box full , is it empty, are you adding text and strings ("hello" +1) etc.

    When you initialise the variable (int x=134;) you are taking the box, emptying it and setting it to 123 so you know exactly what is in the box and what type of box it is.

    Now some modern languages may automatically clear the box and set its type on variable declaration but in general it is good practice to declare and initialise at the same time.

    You can initialise by setting an actual value or calling a function.
    So you could do
    int x=1234;

    int x= NumberDaysInAyear()


    Uninitialised variables can lead to all sorts of fun in lower level languages.


  • Registered Users Posts: 2,297 ✭✭✭Ri_Nollaig


    Declaration is basically announcing to the computer you are about to use a new variable.
    int x;
    

    Initialisation is telling the computer what that variable should be set to.
    int x;
    x = 1;
    

    These can be combined into a single statement
    int x = 1;
    

    where you do the samething in a single line.

    if you didn't initialise a variable then (in Java) it will have a default value for primitives like 0 for int and null for object types.

    You'll find its best to initialise any variables you declare to ensure they start on values you have expected. Its common enough to want an int at -1 instead of 0 as -1 might mean "not found" while 0 could be a valid value.


  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Beware, instance and static variables get default values but local variables inside a method have no defaults and will throw an error if you don't initialize them.


  • Registered Users Posts: 2,781 ✭✭✭amen


    Why you are all correct your not really tell the user what is going which is that member is reserved and that contents of the memory at the location for variable is set to the initialised value.

    Just because you default values in Java doesn't mean all languages do.

    I think its more important to understand what is happening outside of Java so when you learn a new language you understand what exactly is happening and why you should in general set an initialisation value.

    When you write int x in Java all that has happened is that Java has taken care of the initilisation and set the default value in effect int x=0;

    Of course in other languages setting int x; there is no guarantee as to what exactly is in x.


  • Closed Accounts Posts: 799 ✭✭✭Logical_Bear


    In fairness the OP mentioned java and said he was only beginning and was also confused by the terminology


  • Registered Users Posts: 2,781 ✭✭✭amen


    In fairness the OP mentioned java and said he was only beginning and was also confused by the terminology

    True but I based my answers on
    So what exactly is initializing and why do you need to initialize a string


Advertisement