200 likes | 407 Views
Variables. data is represented by variables variables are names for data variables refer to a location in memory; the data in that location may change; that’s why they’re called variables variable names may include letters, digits, $, and _, and may not start with a digit
E N D
Variables • data is represented by variables • variables are names for data • variables refer to a location in memory; the data in that location may change; that’s why they’re called variables • variable names may include letters, digits, $, and _, and may not start with a digit • Variable names are case-sensitive • by convention, variable names in Java start with a lowercase letter; interior words are capitalized • Examples: x xx a2 salary closeButton x$y group_name
Variables (continued) • Variables must have a type • The type may be a primitive data type or a class name
Primitive Data Types • Primitives are simple data entities: • byte - a 1 byte signed integer • short - a 2 byte signed integer • int - a 4 byte signed integer • long - an 8 byte signed integer • char - a 2 byte Unicode character • double - an 8 byte number including fractional part • float - a 4 byte number including fractional part • boolean - a true/false value
Integer Arithmetic • Integers have no fractional part 10 / 4 evaluates to 2 10./ 4 evaluates to 2.5 4 / 10 evaluates to 0 4 / 10. evaluates to .4
Classes as Data Types • Many pre-defined classes come with Java: • String s; // represents a string of characters • Date d; // represents a specific time • We may define our own classes and refer to instances of them with variables: • Student student; // represents a student • Company c; // represents a company
Declaring Variables • All variables must be declared • Syntax: type name; • Examples int n; // primitive double x; // primitive boolean completed; // primitive String name; // class Button closeBtn; // class
Literal Data Values • integers: digits without decimal point 2 345 -2 -45678L • floats, doubles: digits with decimal points 2. 3.0 0.2345 .2345 234.99f 1.065E15 • char: a character within single-quotes ‘x’ ‘X’ ‘?’ ‘9’ ‘\n’ • boolean: the values true and false • strings: characters within double-quotes "hello" "hello my good friend" "x" "" " " (note: String literals cannot be broken across lines)
Expressions • primitive data items may be combined by operators to create a new value • operators: * / % multiply, divide, modulo (higher precedence) + - add, subtract (lower precedence) • Examples a + 1 34+56-cost a+b/7 (a+b)/7 a+(b/7) cost%100 • Promotion 5/8 // an integer-arithmetic division evaluating to 0 5/8. // a double-arithmetic division evaluating to .625
String Catenation • The + operator may be used to combine strings • non-String operands will be automatically converted to a String • Examples: "number" + "one" // "numberone" "number" + " " + "one" // "number one" "number " + 1 // "number 1" int i = 10; i + " is the answer"; // "10 is the answer" double d = 10; d + " is the answer"; // "10.0 is the answer"
Assignment Statements • “store” data in a variable • Syntax variable = expression; • data types must match (with some exceptions) • Examples int a, b, c; a = 1; b = 10; c = b + 12345; double salary; salary = 100000. / 12;
Assignment Statements (cont.) • variables may be assigned a value at declaration int a = 1; int x=24, y=36, z = -12, time; String name, greeting = "hello"; double salary=50000., bonus=10000, totalSalary=salary + bonus; • = indicates assignment, not equality int a = 5; // read as “a gets 5” a = a + 2; // read as “a gets a + 2” // a now contains the value 7
More Operators • shorthand operators • var op= expression is equivalent to var = var op (expression) • a += 5 is equivalent to a = a + 5; • a *= b + 5; is equivalent to a = a * (b + 5); • incrementation operators • a++ increments the value of a by 1 (postfix operator) • ++a increments the value of a by 1 (prefix operator) • when used in an expression, the postfix operator uses the existing value, then increments int a = 2; int b = 12 / a++; // b = 6, a = 3 • when used in an expression, the prefix operator increments first, then uses the incremented value in the expression int a = 2; int b = 12 / ++a; // a = 3, b = 4 • also decrementation: --a a--
Final Variables • a variable can be used to represent a constant • the value of such a variable should not be changable • this is enforced by the final keyword • final variables may only be assigned a value one time • the naming convention for final variables is all caps, with interior words separated by underscores • e.g. double pints = 1.761 * liters; // magic number final double PINTS_PER_LITER = 1.761; double pints = PINTS_PER_LITER * liters;
Comments • are not compiled into bytecode by the compiler • are for the programmer’s or reader’s benefit • single-line comments // makes the remainder of the line a comment e.g., // now we will illustrate a comment a = a + 2; // a now contains a new value
Comments (cont.) • multi-line comments • /* begins a comment; */ ends it e.g., /* if you need to write a paragraph to explain what’s happening, you can use this style of comment */ • this style of comment cannotbenested /* start comment 1 /* start comment 2 end of comment 2 */ end of comment 1 */
A Simple Program public class SimpleProgram { public static void main (String[] args) { double overallGpa = 3.4; int totalCredits = 25; double newGrade = 2.5; int newCredits = 4; double points = overallGpa * totalCredits + newGrade * newCredits; totalCredits = totalCredits + newCredits; overallGpa = points / totalCredits; System.out.println ("GPA = " + overallGpa + "; credits = " + totalCredits); } }