Download presentation
Presentation is loading. Please wait.
1
Java Basics
2
Java High-level language Designed by Sun Microsystems in 1995
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
3
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
4
Types int double boolean char (signed) integer
double-precision floating point number boolean true or false char character
5
Names (“Identifiers”)
Starts with a letter After 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
6
The Famous/Weird 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?
7
Changing Values Assignment x = 97.5; =
Equal sign, but doesn’t mean equal as in math x = 97.5; Means assign 97.5 to x (or store in x) Doesn’t mean we state x is equal to 97.5
8
Changing Values Assignment x = 97.5; x = 97.5 + x; =
Equal sign, but doesn’t mean equal as in math x = 97.5; Means assign 97.5 to x (or store in x) Doesn’t mean we state x is equal to 97.5 x = x; Why is this impossible in math? What does this mean in Java?
9
Changing boolean and char variables
boolean sunny; sunny = false; char letterGrade; letterGrade = ’A’;
10
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;
11
Manipulating Data Operators Arithmetic Relational Logical
12
Arithmetic Operators + - * / % ++x , x++ Yields a number
modulo/reminder 5 % 2 is 1 ++x , x++ Increment x (int) Yields a number
13
Arithmetic: Division with Integers
Math: 5 / 2 is 2.5 Java “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
14
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?
15
Logical Operators && || ! Yields true or false value and or not
true && false is false !(5 > 2) is false
16
&& (and) operator Green (boolean) Square Green && Square F T
17
|| (or) operator Green (boolean) Square Green || Square F T
18
! (or) operator Green (boolean) !Green F T
19
Precedence/Ordering of Operators
x < y + z (x < y) + z x < (y + z)
20
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)
21
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
22
Comments Ignore by the compiler
Improves readability, fewer mistakes // describe something that is not obvious /* this is a multi-line comment */
23
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
24
Input from the Keyboard
We’ll usually provide templates for input Scanner keyboard = new Scanner(System.in); x = keyboard.nextInt(); y = keyboard.nextDouble();
25
Output to the Screen System.out.println( … ); System.out.print( … );
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.