• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Nathan banks
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not redeclared. You are just changing the value.
Declaration is done only once.
 
Nathan banks
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for responding and clarifying.  To be sure I understand, it is the same var?
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Nathan banks
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Ganesh Patekar
    Bartender
    Posts: 726
    23
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're most welcome Nathan 
     
    Tapas Chand
    Ranch Hand
    Posts: 614
    9
    BSD Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Most welcome
    And do not get frustrated, people are here to help.
     
    Liutauras Vilda
    Sheriff
    Posts: 4922
    334
    BSD
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Amit Emitt
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Ganesh Patekar
    Bartender
    Posts: 726
    23
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to CodeRanch Amit ! 
     
    Amit Emitt
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank You, Ganesh 
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Ganesh Patekar
    Bartender
    Posts: 726
    23
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Nathan banks
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Daniel Cox
    Ranch Hand
    Posts: 231
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Liutauras Vilda
    Sheriff
    Posts: 4922
    334
    BSD
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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:
     
    Carey Brown
    Saloon Keeper
    Posts: 3323
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Ganesh Patekar
    Bartender
    Posts: 726
    23
    Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nathan, Worth reading click here -->Java Programming Style Guide
     
    Nathan banks
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Nathan banks
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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!
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Nathan banks
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's a pleasure
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!