Learning Java: Day 10

Numbers and Statics
We are what we repeatedly do.  Excellence then, is not an act, but a habit.
- Aristotle
Today marks the halfway point in the book, Head First Java. Yesterday I was wondering why I was so determined to learn this programming language, especially since it is not even remotely similar to my upcoming Master's degree studies of acupuncture and Oriental medicine. Java is difficult for me to learn and frequently I've felt really, really dumb because it's taken so long to wrap my mind around some of the concepts. I've told people who've asked me why I'm bothering to learn programming, that I want to be able to create Android phone apps for use in my master's studies, so therefore I need to know Java (since the phone apps are created in Java). But I think the answer lies deeper than that. 

I want to learn Java because it's difficult for me.  I want to conquer this subject. I want to learn Java because it's just one of the many things I want to learn or do before I die. So let's get on with it...
______________________________________
STATIC
The keyword static lets a method run without any instance of the class.  This is used when you don't need to have an instance of the class (like when using the Math methods). 

You call a static method using the class name

Example:     Math.min(66,99);
You call a non-static method using a reference variable name
Example:     Werewolf Jacob = new Werewolf();
          Jacob.howl();

Static variable: value is the same for All instances of the class.
Static variables are shared.
All instances of the same class share a single copy of the static variables.
Instance variables: 1 per instance
static variables: 1 per class

FINAL means it can never change
A final variable means you can't change its value.
A final method means you can't override the method.
A final class means you can't extend the class (i.e. you can't make a subclass).

MATH
Converting a String to a primitive value ('parseInt', 'parseDouble', or 'parseBoolean'): 
     String s = "2";
     int x = Integer.parseInt(s);

Converting a primitive number into a string (easiest way is to concatenate the number to an existing string)
     double d = 42.5;
     String doubleString = " " + d;

     double d = 42.5;
     String doubleString = Double.toString(d);

FORMAT
%,d means "insert commas and format the number as a decimal integer."
%.2f means "format the number as a floating point with a precision of two decimal places."
%,.2f means "insert commas and format the number as a floating point with a precision of two decimal places."
Formatting Dates (Use java.util.Date when you want the current date and time):
%tc gives the complete date and time: String.format ("%tc", new Date()); 
          Fri Aug 17 23:58:29 HST 2012
%tr gives just the time: String.format ("%tr", new Date());
          11:58:29 PM
%tA %tB %td gives the Day of the week, month and day
     Date today = new Date();
     String.format ("%tA, %tB, %td", today,today,today)(there's an easier way but I can't get it to format correctly on this screen)
                    Friday, August 17

Use java.util.Calendar for date manipulation.
There are a LOT of methods in the Calendar API and a lot of Calendar Fields.
And we finally end with practice and exercises.  This was a very LONG chapter. My brain is fried...


0 comments:

Post a Comment