1 / 23

Program Basics

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). {.

hector
Download Presentation

Program Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Program Basics Concept of variables Variable declarations Naming rules and conventions Assignment statement

  2. 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 { {

  3. 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

  4. 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

  5. Inside look at memory

  6. 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

  7. 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

  8. 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

  9. Primitive Types

  10. 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, #, $, (, …

  11. 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.

  12. 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 …

  13. 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

  14. 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

  15. 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

  16. Assignments Variable = Expression; Left: variable to assign value to Right: value to be assigned NOT a mathematical "equal" sign Expressions can be complicated

  17. 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;

  18. Assignments – The bad news What is wrong with the following code: int age; double ageInDogYears; //Calculate your age in dog years: age = age * ageInDogYears;

  19. 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?

  20. 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

  21. 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

  22. 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;

  23. Review Type variable1, variable2, … ; variable = expression; double Memory and variable declaration Type variable1, variable2, … ; total = total + num2; double price; int

More Related