This Program uses arithmetic operators class arithmetic Int






- Slides: 6

/* This Program uses arithmetic operators */ class arithmetic. Int{ public static void main(String arg[]){ int num 1; int num 2; int sum; int diff; int mult; int whatisthis; num 1=15; num 2=10; sum=num 1+num 2; diff=num 1 -num 2; mult=num 1*num 2; whatisthis=num 1/num 2; } } System. out. println("The sum of the two numbers is: "+ sum); difference of the two numbers is: "+ diff); multiplication of the two numbers is: "+ mult); division of the two numbers is: "+ whatisthis); 1

/* This Program uses arithmetic operators */ class arithmetic. Doub{ public static void main(String arg[]){ double num 1; double num 2; double sum; double diff; double mult; double whatisthis; num 1=15; num 2=10; sum=num 1+num 2; diff=num 1 -num 2; mult=num 1*num 2; whatisthis=num 1/num 2; } } System. out. println("The sum of the two numbers is: "+ sum); difference of the two numbers is: "+ diff); multiplication of the two numbers is: "+ mult); division of the two numbers is: "+ whatisthis); 2

Variables • Anything that stores a value – Symbols made up of characters: A-Z, a-z, 09, _, $, … – Called identifiers – Must start with a letter or _ or $ – Examples: day. Of. The. Week, day_of_the_week, dayoftheweek, _dayoftheweek, myname, my. Name, … – Constants are not variables: 7, 100, 2. 5, … – Variables are useful for holding non-constant values 3

Data types • Integer and Long integer – int and long – Called keywords: will learn more keywords – Keywords cannot be used as identifiers – Example: int x; – x is the variable name which we have declared as an integer – x is said to be of type integer – “int” must be written in small characters – This is called syntax of a language 4 – Not following it properly leads to syntax errors

Data types • Floating-point and double – Used for representing non-integer numbers – Examples: float pi, _run_rate, Average_score; double e, interest. Rate; – pi, _run_rate, Average_score, e, interest. Rate are variable names – Notice the comma separating the names 5

Why data types? • Why can’t I just use a variable in computation? – Every variable must have a type – Why must it be declared to have a type? – Allocation in the memory (recall the scratchpad) – Every variable should get some space in the rough sheet; otherwise how can you use it for computation? – Help the compiler decide how many bits should be reserved for a variable – Observation: compiler must know the data 6 type to size mapping