Java Basics. Java High-level language  More readable for humans  Need to be translated to machine language for execution Compilers  CPU-independent.

Download Java Basics. Java High-level language  More readable for humans  Need to be translated to machine language for execution Compilers  CPU-independent.

Post on 30-Dec-2015

219 views

Category:

Documents

0 download

TRANSCRIPT

<p>Introduction</p> <p>Java BasicsJavaHigh-level languageMore readable for humansNeed to be translated to machine language for executionCompilersCPU-independenttranslation can target different CPUs (machine languages)</p> <p>Designed by Sun Microsystems in 1995Sun was bought by Oracle in 2010</p> <p>Designed with internet in mindCan run in a web browserStoring DataTo store datawe need to allocate space in the memoryDeclare (specify)Type what kind of dataNamewe can refer to it laterEssentially a named location in the memory</p> <p>Typesint(signed) integerdoubledouble-precision floating point numberbooleantrue or falsecharcharacterNames (Identifiers)Starts with a letterAfter that, can includeletterdigitCan these be names?numberOfStudentsfive5555fiveCase sensitiveBalance and balance are different namesMeaningful names improve readabilityreduce mistakesThe Famous/Weird SemicolonSemicolon Is similar to a period after a sentence in EnglishEnd of one instructionPeriod is used to mean something else in Java</p> <p>Allocating space (declaration):int numberOfStudents;double temperature, humidity, pressure;boolean sunny, hurricane;char letterGrade;</p> <p>They are usually called variables similar to mathHow do we vary/change the value?Changing ValuesAssignment=Equal sign, but doesnt mean equal as in math</p> <p>x = 97.5;Means assign 97.5 to x (or store 97.5 in x)Doesnt mean we state x is equal to 97.5Changing ValuesAssignment=Equal sign, but doesnt mean equal as in math</p> <p>x = 97.5;Means assign 97.5 to x (or store 97.5 in x)Doesnt mean we state x is equal to 97.5x = 97.5 + x;Why is this impossible in math? What does this mean in Java?Changing boolean and char variablesboolean sunny;sunny = false;</p> <p>char letterGrade;letterGrade = A;</p> <p>Initializing VariablesCombiningDeclaring a variable (allocating space) andAssigning an initial value</p> <p>int numberOfStudents = 15;double gpa = 3.14;char letterGrade = A;boolean sunny = true;Manipulating DataOperatorsArithmeticRelationalLogical</p> <p>Arithmetic Operators+-*/% modulo/reminder5 % 2 is 1++x , x++Increment x (int)Yields a number</p> <p>Arithmetic: Division with IntegersMath: 5 / 2 is 2.5Javainteger divisionboth values/operands are integers5 / 2 has an integer value -- floor of 5/25 / 2 is 2 [sometimes this is useful]If we want a floating point value (2.5)5 / 2.0 , 5.0 / 2 , or Be carefulint x = 5 / 2.0 ; x has 2 because 2.5 cant fit into an int variable</p> <p>Relational Operators===!=Yields true or false value5 &lt; 2yields falsenot stating 5 is less than 2 (in math), which is impossiblex == 2Means what?Logical Operators&amp;&amp; and||or!notYields true or false valuetrue &amp;&amp; false is false!(5 &gt; 2) is falsePrecedence/Ordering of Operatorsx &lt; y + z(x &lt; y) + zx &lt; (y + z)</p> <p>Precedence/Ordering of Operatorsx &lt; y + z(x &lt; y) + zx &lt; (y + z)</p> <p>x &lt; y + z &amp;&amp; y &lt; zx &lt; (y + z) &amp;&amp; y &lt; z((x &lt; (y + z)) &amp;&amp; y) &lt; z(x &lt; (y + z)) &amp;&amp; (y &lt; z)</p> <p>Precedence/Ordering of OperatorsQuite naturalArithmetic (calculate numbers) before Relational (compare numbers) before Logical (combine boolean--true/false values)</p> <p>If not sure, add parentheses</p> <p>CommentsIgnore by the compilerImproves readability, fewer mistakes</p> <p>// describe something that is not obvious</p> <p>/* this is a multi-line comment */</p> <p>Math Constants and FunctionsMath.PI, Math.E</p> <p>Math.abs(x)</p> <p>Math.sqrt(x), Math.pow(x, exp)</p> <p>Math.log(x), Math.log10(x)</p> <p>Math.sin(x), Math.cos(x), Math.tan(x) // radiansMath.asin(x), Math.acos(x), Math.atan(x)</p> <p>Math.random() // 0 </p>

Recommended

View more >