Forums Register Login

Why don't I need to re-declare a var in the same method when changing the value?

+Pie Number of slices to send: Send
Example


So I only entered int var; 1 time before I gave it a value of 10.  Then, I use the var (10) again but I make this var "var / 4".  It takes on a new value, so is it the same var but from here out, at least in this method, 2.5?  My frustration, after a day of searching (but learning) has not led me to a comprehensible answer.
1
+Pie Number of slices to send: Send
It is not redeclared. You are just changing the value.
Declaration is done only once.
+Pie Number of slices to send: Send
Thank you for responding and clarifying.  To be sure I understand, it is the same var?
1
+Pie Number of slices to send: Send
 

Nathan banks wrote:So I only entered int var; 1 time

I think you mean to say declared one time.
  • Here int var and x are local variable whose scope is withing main method only.Means their existance is within main method.
  • No you don't have to redeclare a variable when changing it's value.
  • You are not allowed to declare a variable having same name more than one time in a scope (Here scope of int var and x is main method only).

  • but I make this var "var / 4".  It takes on a new value, so is it the same var but from here out, at least in this method, 2.5?

  • Yes It is the same var but
  •  results var = 2 because var/4 means 10 /4  which results 2 where we lost precision i.e. 0.5 because both diviser and dividend are int.

  • In second case
  • x is a double so before calculating int value 4 is promoted to double by widening and now it becomes 10.0/ 4.0  which results 2.5 without loss of precision.
    +Pie Number of slices to send: Send
     

    Ganesh Patekar wrote: I think you mean to say declared one time.


    I sure did.  I'm still trying learn and practice all of the correct terminology and syntax.  (long way to go!)

    Thank you both Tapas and Ganesh.  This a new way to look at things right now but you helped clear the air on my current, frustrating yet simple problem.
    +Pie Number of slices to send: Send
    You're most welcome Nathan  
    +Pie Number of slices to send: Send
    Most welcome
    And do not get frustrated, people are here to help.
    1
    +Pie Number of slices to send: Send
    One thing, I'd suggest not to use such variable name as "var", because in some other programming languages it is an identifier to declare a variable.

    For instance JavaScript:
    Because most of the people know more than one language, and could be confusing for somebody to read your  code.
    +Pie Number of slices to send: Send
    Due to java is strongly typed language <Data Type> need every time when to declare a variable, hence it's not a redeclaration its a reassignment of value.
    +Pie Number of slices to send: Send
    Welcome to CodeRanch Amit !  
    +Pie Number of slices to send: Send
    Thank You, Ganesh  
    +Pie Number of slices to send: Send
     

    Ganesh Patekar wrote:. . .  results var = 2 because var/4 means 10 /4  which results 2 where we lost precision i.e. 0.5 because both diviser and dividend are int. . . .

    I am not sure I would call that loss of precision. Integer division has worked on the same basis for a very long time. It is what I learnt when I was about 7 years old at Kensworth Voluntary Primary School.

    10 ÷ 4 = 2, remainder 2

    I think I would prefer to say that integer arithmetic follows these particular rules instead of loss of precision.
    Note you get no loss of precision with 10.0 / 4.0 either, because 2.5 is one of the few numbers which can be accurately represented as a binary fraction.
    +Pie Number of slices to send: Send
     

    Campbell Ritchie wrote:I think I would prefer to say that integer arithmetic follows these particular rules instead of loss of precision.

    Yes you are right, loss means we wanted but couldn't get for some reason where definition says Integer division is division in which the fractional part (remainder) is discarded is called integer division  Ref: Math world
    +Pie Number of slices to send: Send
     

    Liutauras Vilda wrote:One thing, I'd suggest not to use such variable name as "var", because in some other programming languages it is an identifier to declare a variable.


    This was definitely causing a lot of my confusion.  I see now that the name of the variable was var!  This is an exercise from a book, and I overlooked the fact that "var" was the name for the variable.  In fairness, the author explains that it is "a variable called var," but I am trying to understand a lot of new information and that got lost in my head.  Still, he could have called it "cats" or "pickle" or anything else and I would have probably been less confused  

    Liutauras Vilda wrote:



    So I changed it all up
    I changed all of the subsequent "vars" to Nathan, and felt great when I got it to run.

    Thank you Liutauras and again, everyone for your help.
    +Pie Number of slices to send: Send
    The JLS tends to use the phrase "loss of precision" when dealing with floating point numbers and "loss of information" when dealing with whole numbers.

    JLS wrote:A narrowing primitive conversion from double to float is governed by the IEEE 754 rounding rules (ยง4.2.4). This conversion can lose precision, but also lose range,e resulting in a float zero from a nonzero double and a float infinity from a finite double. A double NaN is converted to a float NaN and a double infinity is converted to the same-signed float infinity.

    A narrowing conversion of a signed integer to an integral type T simply discards all but the nlowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

     
    1
    +Pie Number of slices to send: Send
     

    Nathan banks wrote:I changed all of the subsequent "vars" to Nathan, and felt great when I got it to run.

    One more thing. Variable names if these aren't constants (forget about those for a while) suppose to start with a lower case (this is how the Java coding convention states). Basically follow camel-casing rules.

    Your name and surname variable would look as:

    some others:


    With an upper case starts Class names, an example:
    1
    +Pie Number of slices to send: Send
     

    Nathan banks wrote:So I changed it all up I changed all of the subsequent "vars" to Nathan, and felt great when I got it to run.


    By convention, all variable names should beging with a lower case letter.
    1
    +Pie Number of slices to send: Send
    Nathan, Worth reading click here -->Java Programming Style Guide
    +Pie Number of slices to send: Send
    This is good information.

    Liutauras Vilda wrote:One more thing. Variable names if these aren't constants (forget about those for a while) suppose to start with a lower case (this is how the Java coding convention states).

    I thought was what i read.  I wasn't sure if you were trying to test me/see if I would catch it when you wrote:

    Liutauras Vilda wrote:For instance JavaScript:
    [code=javascript]var name = "Nathan";

     I actually tried it both ways (probably more like 6 ways until I got it to run right.)  I figured I would post the results with "Nathan", but I am now more clear on the convention.
    +Pie Number of slices to send: Send
     

    Ganesh Patekar wrote:Nathan, Worth reading click here -->Java Programming Style Guide



    Within seconds I learned that I should not be using the tab key.  Mistakes, mistakes mistakes.  This is going to make typing take quite a bit longer!  I'll keep reading, and plugging away at my book and exercises as well.  Thanks!
    1
    +Pie Number of slices to send: Send
     

    Nathan banks wrote:. . .  This is going to make typing take quite a bit longer!  . . .

    No, it won't. If it takes longer, that is because you are using the wrong text editor. The suggestions in this post will allow you to use a text editor to code more quickly and to find formatting errors more easily.
    +Pie Number of slices to send: Send
     

    Campbell Ritchie wrote:No, it won't. If it takes longer, that is because you are using the wrong text editor. The suggestions in this post will allow you to use a text editor to code more quickly and to find formatting errors more easily.

    Hey, that's terrific!  Thank you for even more helpful info.  I'm really glad I decided early on to sing up for an account and dive into the forums.  It's out of character for me, but I'm happy here already.
    +Pie Number of slices to send: Send
    That's a pleasure
    It's weird that we cook bacon and bake cookies. Eat this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 609 times.
    Similar Threads
    Head scratcher
    Eliminate object from array by setting boolean to false
    Hello World
    Using == operator on wrapper classes
    bisection method in java
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 29, 2024 02:02:31.