A variable is a storage location for some type of value. days 102 taxrate 7.75 int days = 102; double taxrate = 7.75; char grade = ‘A’; boolean done = true;
What is a data type?
A data type describes what type of data the variable will hold. There are 8 basic primitive data types What is a data type?
byte short int long float double char boolean int num = 9; double total = 3.4;
TYPEHoldsRANGE intintegers (32 bit) -2 billion to 2 billion int num = 3; doubledecimals/ floating number (64 bits) -1.7E+308 to 1.7E+308 double num1 = 7.4; boolean {true, false}Holds two values: {true, false} boolean done = false; char One character ‘A’
Declarations A declaration is the introduction of a new name in a program. All variables must declared in advance General forms to declare variable. Data type and variable name int num; double digit; boolean done;
How do you name variables when defining them?
When naming a variable follow these rules and conventions: 1.The variable name must contain NO spaces. 2.The variable name must begin with a lower case letter. 3.If you run two words together, then the first letter of the second word should begin with a capital letter. 4.The remainder of the variable name contains only letters and digits. Can use _ $ as symbols and can start with _ but not recommended. IDENTIFIER variables are identifers
Below are some examples of valid variable names: number sum32 testAverage areaOfTrapezoid and below are some examples of invalid variable names: 2ndValue test Average question#2
Java is case sensitive. Brandon does not equal brandon. Brandon != brandon Java is case sensitive. (Yes, it’s so important that I said it twice!)
Keywords are reserved words that the language uses for a specific purpose. You cannot use keywordsas identifier names. abstractcontinuefornewswitch assert *** default goto * packagesynchronized booleandoifprivatethis breakdoubleimplementsprotectedthrow byteelseimportpublicthrows case enum **** instanceofreturntransient catchextendsintshorttry charfinalinterfacestaticvoid classfinallylong strictfp ** volatile const * floatnativesuperwhile
assignment operator (=). Called initializing the variable. int num = 1000; // This line declares num as an int variable which holds value "1000". boolean bol = true; // This line declares bol as boolean variable which is set to the value "true". boolean result = true; char capitalC = 'C'; byte b = 100; short s = 10000; int i = ; Statements: Statements in Java end with a semicolon ;
OOP: Basic Parts of Java Primitive Data Types, Example // create some integers int x, int y; // declared but not initialized x = 1234; y = 3; //initialized and ready to use double v = 3.14, w = 5.5; //declare and Initialize at same time