Java Tutorial #2 — New data types, variable naming and arithmetic.


Topics Discussed: Introduced new data types, when to use said data types, variable naming schemes, and how the java compiler handles arithmetic

Source Code Available Here

Style
At first I introduce the idea of style, which is going to be important for you to understand and develop as you progress in your learning. The essential bits of style that I want you to understand are as follows:

  • The more comments you have in your code, the better off you’ll be.
  • Every line should be ending with either an {, or a ; at this point in your programming if possible. Splitting output across multiple lines is bad, and I actually won’t be teaching you how to do so.
  • Giving your variables meaningful names will pay dividends later on when you come back to a program that you finished and want to revise. “itemCost” will make a lot more sense to you than “x”.


There are a few popular types of variable naming schemes out there. The most popular two out there are Hungarian notation, and Camelback notation. Hungarian notation isn’t as widely used in java, so I won’t really get into that here. Camelback notation on the otherhand seems to be everywhere on java forums, stackoverflow, etc. The basic idea is that any variable with more than one word in it’s name (itemCost, itemsOnHand, taxRate, etc. etc.) start with a lower-case letter and have the first letter in each word thereafter capitalized for additional readability.


New Data Types
As for new data types, I didn’t introduce every data type out there, but here’s a few that we’ll be using throughout this course

  • int– Integer numbers, somewhat small storage (16 bit in many cases, but can vary in some systems), non-decimal numbers.
  • float– Similar to int, float is the small-version of a number that can hold a decimal point if needed
  • double– Similar to float, but has a larger amount of storage, at the cost of taking up more space (32 bits vs 16 I believe)
  • char– A variable that holds a single character
  • String– A variable that holds a string of characters, whitespace included. Can hold multiple words.
  • The items below are used slightly less than the above but many are still very useful

  • short– Similar in part to int, but with even less storage
  • long– The exact opposite of short, same idea as int but with more storage.
  • unsigned (char/int/long/short/float/ double)– These can tend to hold larger numbers than their non-unsigned counterparts, however these variables cannot have a negative value applied to them.


Order of Operations
In java the order of operations is as follows:

  1. Parenthesis
  2. Multiplication / Division (equal priority, evaluates left to right).
  3. Addition and Subtraction (equal priority, evaluates left to right).

Due to the nature of how exponents are calculated in java (see: using a function), they are not considered in the order of operations. The code we can use to calculate some arithmetic is as follows.


int x=6, y=7;
System.out.println(x*y); // this would output 42
System.out.println(6*7); //this would also output 42
x+=6; // adds 6 to the current value of x, making it 12
int k = x-y; // assigns a value of 5 to k

Leave a Reply

Your email address will not be published. Required fields are marked *