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

Java Basics

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

Java

High-level language More readable for humans

Need to be translated to machine language for execution Compilers

CPU-independent translation can target different CPUs (machine languages)

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

Designed with internet in mind Can run in a web browser

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

Storing Data

To store data we need to allocate space in the memory

Declare (specify) Type

• what kind of data Name

• we can refer to it later• Essentially a named location in the memory

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

Types

int (signed) integer

double double-precision floating point number

boolean true or false

char character

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

Names (“Identifiers”)

Starts with a letterAfter that, can include

letter digit

Can these be names? numberOfStudents five5 55 5five

Case sensitive Balance and balance are different names

Meaningful names improve readability reduce mistakes

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

The Famous/Weird Semicolon

Semicolon Is similar to a period after a sentence in English End of one instruction Period 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 math How do we vary/change the value?

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

Changing Values

Assignment = Equal sign, but doesn’t mean equal as in math

x = 97.5; Means assign 97.5 to x (or store 97.5 in x) Doesn’t mean we state x is equal to 97.5

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

Changing Values

Assignment = Equal sign, but doesn’t mean equal as in math

x = 97.5; Means assign 97.5 to x (or store 97.5 in x) Doesn’t mean we state x is equal to 97.5

x = 97.5 + x; Why is this impossible in math? What does this mean in Java?

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

Changing boolean and char variables

boolean sunny;sunny = false;

char letterGrade;letterGrade = ’A’;

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

Initializing Variables

Combining Declaring a variable (allocating space) and Assigning an initial value

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

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

Manipulating Data

Operators Arithmetic Relational Logical

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

Arithmetic Operators

+-*/%

modulo/reminder 5 % 2 is 1

++x , x++ Increment x (int)

Yields a number

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

Arithmetic: Division with Integers

Math: 5 / 2 is 2.5Java

“integer division”—both values/operands are integers 5 / 2 has an integer value -- floor of 5/2 5 / 2 is 2 [sometimes this is useful]

If we want a floating point value (2.5) 5 / 2.0 , 5.0 / 2 , or …

Be careful int x = 5 / 2.0 ; x has 2 because 2.5 can’t fit into an int variable

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

Relational Operators

<<=>>===!=Yields true or false value

5 < 2 yields false not stating 5 is less than 2 (in math), which is impossible

x == 2 Means what?

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

Logical Operators

&& and

|| or

! not

Yields true or false value true && false is false !(5 > 2) is false

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

Precedence/Ordering of Operators

x < y + z (x < y) + z x < (y + z)

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

Precedence/Ordering of Operators

x < y + z (x < y) + z x < (y + z)

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

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

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

Precedence/Ordering of Operators

Quite natural Arithmetic (calculate numbers) before Relational (compare numbers) before Logical (combine boolean--true/false values)

If not sure, add parentheses

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

Comments

Ignore by the compiler Improves readability, fewer mistakes

// describe something that is not obvious

/* this is a multi-line comment */

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

Math Constants and Functions

Math.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) // radians Math.asin(x), Math.acos(x), Math.atan(x)

Math.random() // 0 <= num < 1

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

Input from the Keyboard

We’ll usually provide templates for input

Scanner keyboard = new Scanner(System.in);

x = keyboard.nextInt();

y = keyboard.nextDouble();

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

Output to the Screen

System.out.println( … ); Print the parameter followed by a new line Examples:

System.out.println(15);System.out.println(x);

System.out.println(“Hello!”); // “string”

System.out.print( … ); Print the parameter without a new line


Top Related