Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Joey Goist
- 8/26/16
- This program will be a review how to start declaring variables in programs. */
- public class VariableType {
- public static void main(String[] args) {
- /*What is an identifier
- - It is the variable, parameter, constant, user-defined method, or a defined class.
- - You pick the name.
- - Cannot start with a number or a symbol
- - Cannot be a keyword
- - No spaces
- - It should desribe what the variable represents
- - variables should start with lowercase letter
- - Class name should be capital
- - Use an uppercase letter to seperate two words (getName)
- or use underscore get_name
- - Make it specific (age is different ffrom Age)
- - Do not repeat variable names
- Identifiers have a primitive or built-in type:
- What are the different primitive types:
- INTEGERS:
- byte
- short
- int
- long
- DECIMALS:
- float
- double
- LETTERS/WORDS:
- char
- String
- TRUE/FALSE:
- boolean
- ***IMPORTANT AP EXAM: int, double, boolean, String
- If you put in a number too large for an integer you get an overflow error
- Java will give you no warning, you just get a wrong result
- Why should you not always use a long?
- Compile and run time would be faster
- INTIALIZING VARIABLES: */
- int number = 5;
- String word = "hello";
- double decimal = 5.9;
- int x;
- char grade = 'A';
- boolean rerun = false;
- double d = 1.45667E23 //Scientific Notation
- /*CASTING:
- What is casting?
- Converting a data type manually
- Why would you need to use casting?
- We need to watch when dividing. */
- int total = 10, n =3;
- double average = total/n; //INTEGER DIVISION
- System.out.println(average);
- average = (double)total/n;
- System.out.println(average);
- average = total/(double)n;
- //What would happen here?
- average = (double)(total/n); //This prints 3.0 based on Order of Operations
- //ASSIGNING/DECLARING VARIABLES
- int sum = 5;
- double tot = sum; //This is allowedsince tot is a double which is larger than an int
- double g = 5.95;
- //int intNum = g; ERROR: Loss of Precision Error
- //To fix you need to Cast
- //ROUNDING TO WHOLE NUMBERS:
- //We can use Math.round(variableName);
- int negAmount = -5.9;
- System.out.println(Math.round(negAmount));
- //Using the strategy of adding or subtracting .5
- //Math.round returns a double
- /*FINAL VARIABLES:
- What is the final variable?
- - This variable cannot be changed (Constant)
- - Coding etiquette has the name in all caps
- - use _ for two or more words
- - Can declare it or we can delcare and intialize
- - Common use is for array bounds
- - Makes it easier to revise code, by only changing 1 variable */
- final double TAX_RATE = 0.6;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement