Monday, March 25, 2013

Java;Notes - 5.) Comments and Data Types


Comments:
In Java there are two ways to do comments
  1. The Double Slash: (IE: //Zomg Dat code!) -- Double Slash's are good for taking notes in your code. Or "Commenting out (Explained later)" single lines of code. This in practicality would look more like:
    1. System.out.println("Say something!"); //This is a simple call for a system output on the screen
  2. The Multi-line Comment: (IE: /*This is a comment!*/) Anything between those two slash's will be marked as a comment. This allows for the use of more than one line of comments So it would look more like the following few lines for a proper example:
    1. /*This is a simple comment line
    2. *This is an extension of the same comment line above
    3. *This is the final line in our comment!*/
  3. Documentation Comments: These work in the same way as a multi-line but are used for something specific, generating program documentation All we do is add another asterisk to first line of the comment. Here is an example of how this would work:
    1. /**  <--- See that?
    2. * Program Name: progName.java
    3. * Code maintainer: Your Name (yourname@emailprovider.com)
    4. * Program functions: List, some, stuff
    5. * I am sure you get the point <-- Note if you put this in your program and release it it will show up in the docs. */
Commenting Out: Commenting out is reffered to as using Double Slash comments to "Strike out" code to do tests on how the program will work with out the need to kill the whole line. So for instance you have a program that has two print statements and you want to take one out with out totally killing it you would simply do the following:
  1. //System.out.println("Hello World!");
  2. System.out.println("We will not be saying 'Hello world!' Instead lets say "Salution Mundo!");


Data Types:
In Java every variable must have a declared type (You cant just type "out x = 3;" instead it must be "int x = 3;")
The 8 primitive types Breakdown:
  • Four of them are integers (Whole Numbers... yup... back to math!)
  • Two of them are Floating Point numbers (Numbers with decimal points)
  • One is the Character type (Used with Unicode)
  • Lastly we have Boolean (True and False)
Java Integers:
An integer in Java is any number positive or negative in which is whole and not a fraction or decimal. Java has the following four kinds of integers:
  1. int: with a storage requirement of 4 bytes and a maximum positive / negative value in the 2,000,000,000 range 
  2. short: with a storage requirement of 2 bytes and a Maximum positive / negative value in the 32,000 range
  3. long: with a storage requirement of 8 bytes and is considered anything over positive / negative 2,147,483,648
  4. byte: with a storage requirement of 1 byte and a maximum positive / negative range of: -128 and 127
Note: You will most likely be using the int type in most cases as the rest are really for specialized programs.
Java Floating Point Numbers:
An FPN is any number that has fractional parts or decimal points. Java Has two kinds of FPN:
  1. Float: Any number with up to 7 significant decimal digits (IE: 1.2345678 is a float)
  2. Double: Any number with up to 15 Significant decimal digits ( IE: 3.141592653589793 is a double.... this one is also pi or at least the first 15 digits)
Note: A "Significant Decimal Digit" is defined as how many decimal places will show before the numbers get abbreviated like when you see something like: "E+38F" at the end of a decimal number. All the numbers after the decimal and before the "E" are considered significant.
The Java "Char" type:
Put simply the char date type allows for the use of an old system called "Unicode" which was a number variant of regular keyboard buttons. Unicode was also used to be able to include different letters for different languages. to use Unicode in code you simply use the "Escape Sequence" of \u and whatever  unicode value you want to put in there. an example would be:
Normally you start the "Main" part of a java program by calling it out like so: public static void main(String[] args)
Now lets replace the square brackets ([]'s) with their Unicode values to get the same results: public static void main(String\u005B\005D args) 

Java Escape Sequences:
You can think of java's escape sequences as way of formatting the text in your program. Here are the basic need to know ES's and what they mean:
  • \b = backspace
  • \t = tab
  • \n= linefeed (everything after this shows up on the next line)
  • \r = carriage return
  • \" = double quote
  • \' = single quote
  • \\ = backslash
Java Boolean Values:
Put simply there are two and they are what they state:
  1. True
  2. False

No comments:

Post a Comment