• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ultra Beginner Questions: Variable type initialization and iteration.  RSS feed

 
Kenon Sci
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java questions.

1. Subsequent to writing this question down I have become introduced to the general theory of casting, enough to recognize that is what the "b" example below represents. However, I'm still uncertain why it needed to be done, seeing as how the variable is declared to be in the type I had to recast it into.

a)



b)



2. I'm learning about loops tonight. I get the general concept, but the iterative element in the For loop begs a question.

Example:


// Why doesn't the iterator update "i"
// before the statement portion?

To explain my reasoning in greater detail: "i" begins at a zero value, then gets tested against the denver array's length. After this, "i" appears to get iterated and only then does the statement appear to be run. i.e. the first run of total[i] would be total at array index [1] vs total at array index [0] since it was iterated just prior to the statement being run. Is this the case?

Here's the full code, just to clarify:



Thanks!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Kenon Sci and welcome to the Ranch! The answers to both your questions is the same: because that's the behavior mandated by the Java Language Specification.

1. http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.8.1-200-B

2. http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.14
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a numeric literal with a decimal point, such as 0.4, in your source code, it's regarded as being of type double (a double-precision floating point number).

When you do computations that use float as well as double values, as you are doing in line 2 of your first two code samples, then Java automatically converts the float values into double (note that double is a "wider" type than float - it can contain anything that a float can contain, and more).

When you do a computation such as: invest + (invest * 0.4) then the type of the result will be double (because 0.4 is of type double).

You assign the result of the computation to a variable of type float. Java is not going to automatically convert the double value back to a float, because float is narrower than double - you might loose information when you convert a double to float. So, Java requires you to use an explicit cast, to make sure that you are aware that you might be throwing away information.

About the for loop: A loop like this for (int i = 0; i < denver.length; i++) means:

- declare and initialize an int variabled named i and initialize it with 0
- the middle part is the condition: the body of the loop will be repeated as long as this is true
- the last part (i++) is executed after each iteration, and before the condition is checked to decide to continue the loop

Note that for-loops can be rewritten as while-loops, which might make it more clear to show what happens. Your loop is equivalent to:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!