Jump to content

Java Data Structs. How does this make sense?

Guest

I'm nearing the end of the Java section in Codecademy, and this program makes no sense to me. "temperature" is not defined anywhere in the program.

 

There is one class in the file. Here it is: (Can you explain where temperature comes from? I understand it is saying "For every integer named temperature in weekly temperature, print temperature")

 

import java.util.ArrayList;

public class TemperaturesForEach {
	public static void main(String[] args){
		  ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
          weeklyTemperatures.add(78);
          weeklyTemperatures.add(67);
          weeklyTemperatures.add(89);
          weeklyTemperatures.add(94);
          for (Integer temperature : weeklyTemperatures){
              System.out.println(temperature);
          }
	}
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

for (Integer temperature : weeklyTemperatures){
              System.out.println(temperature);
          }

 

It is defined there, It creates temperature then adds each weeklyTemperatures.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, vorticalbox said:

for (Integer temperature : weeklyTemperatures){
              System.out.println(temperature);
          }

 

It is defined there, It creates temperature then adds each weeklyTemperatures.

Oh, okay. That makes sense. It's tricky because normally integers are defined by int. 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, fpo said:

Oh, okay. That makes sense. It's tricky because normally integers are defined by int. 

they are using the Integer class as opposed to the int primitive data type. They are using this because an arraylist stores objects not primitives

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

@fpo

It is as @SCHISCHKA says, Java generics (that is things like lists and maps where you can put a type in <>'s) MUST work on objects, not primitives.

To bypass this limitation, each primitive has what is called a wrapper class.  These classes are Integer, Boolean, Character, Short, Long, Float, Double, and Void (don't worry about Void for now, only listed for completeness).  These wrappers allow the primitives to be used like objects.  For example, you'll see that, while the ArrayList had to be declared with the wrapper class "Integer", the items added to it are plain old primitive int's.  This is called boxing.  When required, such as the generic requiring an object, the primitive will be automatically converted (boxed) into an instance of its wrapper class.

It would also have been valid to use "int" instead of "Integer" in the for loop, as these wrappers also support automatic conversion back to their primitive type (called unboxing).


As a side note:
These wrapper classes also contain a few methods dealing with strings, such as printing and parsing, and a few constant fields such as minimum and maximum values, so they're worth taking a look at.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, SCHISCHKA said:

they are using the Integer class as opposed to the int primitive data type. They are using this because an arraylist stores objects not primitives

 

5 minutes ago, Yamoto42 said:

@fpo

It is as @SCHISCHKA says, Java generics (that is things like lists and maps where you can put a type in <>'s) MUST work on objects, not primitives.

To bypass this limitation, each primitive has what is called a wrapper class.  These classes are Integer, Boolean, Character, Short, Long, Float, Double, and Void (don't worry about Void for now, only listed for completeness).  These wrappers allow the primitives to be used like objects.  For example, you'll see that, while the ArrayList had to be declared with the wrapper class "Integer", the items added to it are plain old primitive int's.  This is called boxing.  When required, such as the generic requiring an object, the primitive will be automatically converted (boxed) into an instance of its wrapper class.

It would also have been valid to use "int" instead of "Integer" in the for loop, as these wrappers also support automatic conversion back to their primitive type (called unboxing).


As a side note:
These wrapper classes also contain a few methods dealing with strings, such as printing and parsing, and a few constant fields such as minimum and maximum values, so they're worth taking a look at.

wow... Java has a lot of hidden stuff buried right beneath the surface. I'll do what I can to scout this stuff out! 

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×