Tuesday, January 13, 2015

Chapter 1 findings

In the first chapter of Headfirst Java, the two biggest things I learned about are casts and declaring arrays. Casts allow you to change numbers from one data type to another, such as from Float to Int.
Example:

float x = 32.25f;
if ((int) x == 32) {
     System.out.println("This will print because 32.25 rounds to 32.")
}
if (x==32) {
     System.out.println("This won't print because x is still 32.25.")
}

Arrays in Java are created somewhat differently than other languages- You have to tell the code you want an array after the primitive type. This means the array will only accept one type of data (String, integer, etc).

String[] animals = {"cats", "dogs", "pigs"}
int[] numbers = {1, 30, 25, 22.5} // This will throw an error, since 22.5 is a float in an integer array.

No comments:

Post a Comment