230 likes | 363 Views
Program Basics. Concept of variables Variable declarations Naming rules and conventions Assignment statement. Structure of a Simple Application. Comments giving info. about this program. Import statements. Class name. public class. {. public static void main (String[] args). {.
E N D
Program Basics Concept of variables Variable declarations Naming rules and conventions Assignment statement
Structure of a Simple Application Comments giving info. about this program Import statements Class name public class { public static void main (String[] args) { Sequence of code here { {
Where is the information stored? Main memory: for holding data for the computer to process Temporary Also call RAM (random access memory) Different from auxiliary memory (secondary memory usually a hard drive, CD-ROM, USB device) Size of the main memory is described by MB or GB
Variables A variable is a name for a main memory location (storage) To store a user-input value To store an intermediate result Values in a variable can change
Variable Basics The address of memory is too difficult to remember! So high level languages let you give the addresses names. These names are called variables If you change the value of a variable, the contents of memory changes
Variable Types A variable type determines what kind of value the memory space can hold Also determines the size of memory space Example int myAge = 34; The type int uses 4 bytes to store values used by the variable myAge. The smallest number is -2147483648 The largest number is 2147483647
Variable Types Two kinds of types in Java: classes, and primitive types “Primitive types” are the building blocks of other types. Just like a, b, c, d, e,… are the building blocks that make words
Variable Types To store whole numbers, use int type Examples of whole numbers -1, 0, 100, 37, -4203 To store a number with a fractional part, use double type Examples of numbers with fractional part: 4.5, 0.5, 98.67, -1.9, 9.0 To store a single character, use char type letters, digits, space, #, $, (, …
Variable Declarations A variable has to be declared (its type and name) before it can be used! Type variable_1=expression_1, variable_2=expression_2, …; Some examples: int num1, num2; In English: Give me two memory locations for storing whole numbers, one will be called num1, and the other num2 double itemPrice=12.3; In English: Give me a memory location for storing a floating-point number It'll be called itemPrice and initialize it to 12.3.
Naming Rules The name of something in a Java program, including a variable, class, method, or object may contain only letters, digits (0 to 9), and the underscore character (_) but the first character can't be a digit Name is case-sensitive Name can't be a keyword (reserved word), such as public, static, void, int, double …
Naming Rules Which of the following names are valid? item#1 PAY_DAY data bin-2 float _title y Sq Ft netscape.com static 3Set num5 class return
Naming Conventions Variable name Use meaningful names for variables Start with a lowercase letter If a name consists of several words, capitalize the first letter of each successive word Why start with a lowercase letter? Usually a class name starts with a capital letter A constant name has all letters capitalized
Assignments Example: sum = num1 + num2 + num3; OR do this: sum=0; sum = sum + num1; sum = sum + num2; sum = sum + num3; Save the value of (num1 + num2 + num3) to variable sum
Assignments Variable = Expression; Left: variable to assign value to Right: value to be assigned NOT a mathematical "equal" sign Expressions can be complicated
Assignments – More Examples weekly_pay = hours_worked * pay_rate; count = 10; count = count + 1; y = -8; x = x *(-9); total = 0; num2 = 5; total = total + num2;
Assignments – The bad news What is wrong with the following code: int age; double ageInDogYears; //Calculate your age in dog years: age = age * ageInDogYears;
Test examples: What is the result of the following code: int x, y, num1, num2; x = 5; y = 10; num1 = x + y * 2; x = x * 2; y = num1; num2 = x / 2; //Remember the rule for division?
More on Assignments Converting types is called type casting //-------------Declare Section ---------; //short way of saying total = total +num; amount /= 2.0; //short way of saying amount = amount/2.0; Assignment compatibilities; see quick reference You can put a smaller box into a big one But not vice versa
More on Assignments Specialized assignment operators total += num; //short way of saying total = total +num; amount /= 2.0; //short way of saying amount = amount/2.0; Assignment compatibilities; see quick reference You can put a smaller box into a big one But not vice versa
Test examples: a) Fix the errors and b) what is the result of the following code: int x=10 y=20 double num1=1.0 num2=2.0 x += 5; y -= x; num1 = num1*num2 + num2;
Review Type variable1, variable2, … ; variable = expression; double Memory and variable declaration Type variable1, variable2, … ; total = total + num2; double price; int