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

Java Basics. Java High-level language  More readable for humans  Need to be translated to machine language for execution Compilers  CPU-independent. slide 0
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

Introduction

Java BasicsJavaHigh-level languageMore readable for humansNeed to be translated to machine language for executionCompilersCPU-independenttranslation can target different CPUs (machine languages)

Designed by Sun Microsystems in 1995Sun was bought by Oracle in 2010

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

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

Allocating space (declaration):int numberOfStudents;double temperature, humidity, pressure;boolean sunny, hurricane;char letterGrade;

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

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

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;

char letterGrade;letterGrade = A;

Initializing VariablesCombiningDeclaring a variable (allocating space) andAssigning an initial value

int numberOfStudents = 15;double gpa = 3.14;char letterGrade = A;boolean sunny = true;Manipulating DataOperatorsArithmeticRelationalLogical

Arithmetic Operators+-*/% modulo/reminder5 % 2 is 1++x , x++Increment x (int)Yields a number

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

Relational Operators===!=Yields true or false value5 < 2yields falsenot stating 5 is less than 2 (in math), which is impossiblex == 2Means what?Logical Operators&& and||or!notYields true or false valuetrue && false is false!(5 > 2) is falsePrecedence/Ordering of Operatorsx < y + z(x < y) + zx < (y + z)

Precedence/Ordering of Operatorsx < y + z(x < y) + zx < (y + z)

x < y + z && y < zx < (y + z) && y < z((x < (y + z)) && y) < z(x < (y + z)) && (y < z)

Precedence/Ordering of OperatorsQuite naturalArithmetic (calculate numbers) before Relational (compare numbers) before Logical (combine boolean--true/false values)

If not sure, add parentheses

CommentsIgnore by the compilerImproves readability, fewer mistakes

// describe something that is not obvious

/* this is a multi-line comment */

Math Constants and FunctionsMath.PI, Math.E

Math.abs(x)

Math.sqrt(x), Math.pow(x, exp)

Math.log(x), Math.log10(x)

Math.sin(x), Math.cos(x), Math.tan(x) // radiansMath.asin(x), Math.acos(x), Math.atan(x)

Math.random() // 0

Recommended

View more >