Introduction
Download
Report
Transcript Introduction
Java Basics
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
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
Types
int
(signed) integer
double
double-precision floating point number
boolean
true or false
char
character
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
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
double
boolean
char
numberOfStudents;
temperature, humidity, pressure;
sunny, hurricane;
letterGrade;
They are usually called variables similar to math
How do we vary/change the value?
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
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?
Changing boolean and char variables
boolean sunny;
sunny = false;
char letterGrade;
letterGrade = ’A’;
Initializing Variables
Combining
Declaring a variable (allocating space) and
Assigning an initial value
int
double
char
boolean
numberOfStudents = 15;
gpa = 3.14;
letterGrade = ’A’;
sunny = true;
Manipulating Data
Operators
Arithmetic
Relational
Logical
Arithmetic Operators
+
*
/
%
modulo/reminder
5 % 2 is 1
++x , x++
Increment x (int)
Yields a number
Arithmetic: Division with Integers
Math: 5 / 2
is 2.5
Java
“integer division”—both values/operands are integers
5 / 2
5 / 2
If we want a floating point value (2.5)
has an integer value -- floor of 5/2
is 2 [sometimes this is useful]
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
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?
Logical Operators
&&
and
||
or
!
not
Yields true or false value
true && false is false
!(5 > 2) is false
Precedence/Ordering of Operators
x < y + z
(x < y) + z
x < (y + z)
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)
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
Comments
Ignore by the compiler
Improves readability, fewer mistakes
// describe something that is not obvious
/*
this is a
multi-line comment
*/
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
Input from the Keyboard
We’ll usually provide templates for input
Scanner
keyboard = new Scanner(System.in);
x = keyboard.nextInt();
y = keyboard.nextDouble();
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!”);
System.out.print( … );
Print the parameter without a new line
// “string”