2 starting javaprogramming
TRANSCRIPT
-
7/28/2019 2 Starting JavaProgramming
1/69
By: De Rosal, Ign. Moses S.
http://docs.oracle.com/javase/
http://docs.oracle.com/javase/http://docs.oracle.com/javase/ -
7/28/2019 2 Starting JavaProgramming
2/69
ContentJava Structure
Compile & Running Program
Language Basics
Variables Data Types
Operators
Scanner Class
Control Flow
Loopinghttp://docs.oracle.com/javase/
http://docs.oracle.com/javase/http://docs.oracle.com/javase/ -
7/28/2019 2 Starting JavaProgramming
3/69
JAVA STRUCTURE
http://docs.oracle.com/javase/
http://docs.oracle.com/javase/http://docs.oracle.com/javase/ -
7/28/2019 2 Starting JavaProgramming
4/69
Java Program Structure
Java must have a public class
Start with public static void main(String[]args)
There are two kinds of java programming: GUI-based
Text-based
Syntaxto declareclass:[modifier] [class] class_name{
}
-
7/28/2019 2 Starting JavaProgramming
5/69
Example
modifier class_name
main program
-
7/28/2019 2 Starting JavaProgramming
6/69
Details Halo.java
Defineclass andmodifier
That can be compiledand executed by theJVM
-
7/28/2019 2 Starting JavaProgramming
7/69
Details (2) Halo.java
Main program and first time it is run by JVM
Public : one of modifier Static : type of method
Void : no return value
Main : main method
String : type of argumen Args: Array of argumen
which can be added whilerunning
-
7/28/2019 2 Starting JavaProgramming
8/69
Details (3) Halo.java
Function to display text in console
println after display text produce a new line
just display text
-
7/28/2019 2 Starting JavaProgramming
9/69
COMPILE AND RUNNING
http://docs.oracle.com/javase/
http://docs.oracle.com/javase/http://docs.oracle.com/javase/ -
7/28/2019 2 Starting JavaProgramming
10/69
Compiling Program
Complie javacname_file.java
-
7/28/2019 2 Starting JavaProgramming
11/69
Compiling Program
Compliewill produce class file
-
7/28/2019 2 Starting JavaProgramming
12/69
Running Program
Running java class_file without.class
-
7/28/2019 2 Starting JavaProgramming
13/69
LANGUAGE BASIC
http://docs.oracle.com/javase/
http://docs.oracle.com/javase/http://docs.oracle.com/javase/ -
7/28/2019 2 Starting JavaProgramming
14/69
Variables
The Java programming language isstatically-typed, which means that all variablesmust firstbe declared before they can be used.
The Java programming language defines thefollowing kinds of variables:
Instance Variables (Non-StaticFields)
Class Variables (StaticFields)
Local Variables
Parameters
-
7/28/2019 2 Starting JavaProgramming
15/69
Naming Variables
Variable namesarecase-sensitive
Must start with aletter (a-z, A-Z), thedollar sign"$", or theunderscorecharacter _ after the first
character, can be followed bynumbers(0-9). Variable names containdash(-) or space( ).
Beginning with lowercase on the first wordand
uppercase letters in thesecondand subsequentwords.
Also keep in mind that the variable names you choosemust not be a keywordor reserved word.
-
7/28/2019 2 Starting JavaProgramming
16/69
Java Language Keywords
abstract continue for new switchassert*** default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throwscase enum**** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp** volatile
const* float native super while
* not used
** added in 1.2
*** added in 1.4
**** added in 5.0
-
7/28/2019 2 Starting JavaProgramming
17/69
Instance Variables
Can be access with instance class
-
7/28/2019 2 Starting JavaProgramming
18/69
Class/Static Variables
Can be access with static class
-
7/28/2019 2 Starting JavaProgramming
19/69
Local Variables
declare local variable within method
-
7/28/2019 2 Starting JavaProgramming
20/69
Parameters
-
7/28/2019 2 Starting JavaProgramming
21/69
Primitive Data TypesData type Length range of values Example
boolean 1 bit 0 and 1 0;
1
byte 8 bit -128 to 127
(-27 to 27)
-5;
10
short 2 byte / 16 bit -32,768 to 32,767
(-215 to 215)
-12,777;
31,578
int 4 byte / 32 bit -2,147,483,648 to 2,147,483,647
(-221 to 221)
-2,107,483,448 ;
2,145,483,638
long 8 byte / 64 bit -9,223,372,036,854,775,808 to
+9,223,372,036,854,775,807.
(-263 to 263)
9,103,372,036,854,775,807
float 4 byte / 32 bit 1.40129846432481707e-45 to
3.40282346638528860e+38
double 8 byte / 64 bit 4.94065645841246544e-324d to
1.79769313486231570e+308d
char 2 byte / 16 bit 0 to 65,535 (unsigned)
-
7/28/2019 2 Starting JavaProgramming
22/69
Default Values
Data Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
String (or any object) null
boolean false
-
7/28/2019 2 Starting JavaProgramming
23/69
Operators
Operators are symbols and special characters(mathematics) used in an expression
Example: int x =3;
int y =x;
int z =x * y;
booleanstatus =true;
-
7/28/2019 2 Starting JavaProgramming
24/69
Operators (2)
-
7/28/2019 2 Starting JavaProgramming
25/69
Operators (3)
ArithmeticOperators
perform addition, subtraction, multiplication, division, andmodulo.
UnaryOperators
requireonlyone operand
perform incrementing/decrementing a value by one,
negating an expression, or inverting the value of a boolean. The Equality and Relational Operators
determine if one operand is greater than, less than, equal to,or not equal to another operand.
-
7/28/2019 2 Starting JavaProgramming
26/69
Operators (4)
Conditional Operators
perform Conditional-AND and Conditional-ORoperations on two booleanexpressions.
Bitwise and Bit Shift Operators
To manipulated bit pattern
less commonly used.
-
7/28/2019 2 Starting JavaProgramming
27/69
Ex: Unary Operator
-
7/28/2019 2 Starting JavaProgramming
28/69
Ex: Bitwise and Bit Shift Operators
-
7/28/2019 2 Starting JavaProgramming
29/69
Ex: Bitwise and Bit Shift Operators(2)
-
7/28/2019 2 Starting JavaProgramming
30/69
Operator Priority
1. Operator in bracket or parentheses"(...)"
2. Incrementand decrement operators
3. Multiplication and division operators4. Addition and subtractionoperators
5. Bitwiseoperators
Try Out Operator
-
7/28/2019 2 Starting JavaProgramming
31/69
Try Out Operator
Change the following program to use assignments operator!!!
T O t O t
-
7/28/2019 2 Starting JavaProgramming
32/69
Try Out Operator answer
-
7/28/2019 2 Starting JavaProgramming
33/69
CLASS SCANNER
http://docs.oracle.com/javase/
http://docs.oracle.com/javase/http://docs.oracle.com/javase/ -
7/28/2019 2 Starting JavaProgramming
34/69
Class Scanner
A simple text scannerwhich can parse primitive typesand strings using regular expressions.
For example, this code allows a user to read a numberfrom System.in:
Import: java.util.Scanner;
-
7/28/2019 2 Starting JavaProgramming
35/69
Class Scanner (2)
nextInt(): to receive integer data type
nextShort(): to receive short data type
nextLong(): to receive long data type nextDouble(): to receive double data type
nextFloat():to receive float data type
nextLine(): to receive string data type
nextBoolean(): to receive booleandata type
Import: java.util.Scanner;
Ex: Class Scanner
-
7/28/2019 2 Starting JavaProgramming
36/69
Ex: Class Scanner
Result Ex: Class Scanner
-
7/28/2019 2 Starting JavaProgramming
37/69
Result Ex: Class Scanner
Ex: Class Scanner (2)
-
7/28/2019 2 Starting JavaProgramming
38/69
Ex: Class Scanner (2)
Result Ex: Class Scanner (2)
-
7/28/2019 2 Starting JavaProgramming
39/69
Result Ex: Class Scanner (2)
-
7/28/2019 2 Starting JavaProgramming
40/69
CONTROL FLOW STATEMENTS
http://docs.oracle.com/javase/
http://docs.oracle.com/javase/http://docs.oracle.com/javase/ -
7/28/2019 2 Starting JavaProgramming
41/69
Control Flow Statements
Control Flow Statements consist of:
Decision making statements,
if-then executed only if a particular test evaluates totrue
if-then-else
Switch the switch statement can have a number of possibleexecution paths.
Looping statements, and
For
While do-while
Branching statements
Break
Continue return
E IF ELSE
-
7/28/2019 2 Starting JavaProgramming
42/69
Ex: IF ELSE
Ex: Switch
-
7/28/2019 2 Starting JavaProgramming
43/69
Ex: Switch
T O t D i i M ki
-
7/28/2019 2 Starting JavaProgramming
44/69
Try Out Decision Making
StatementsCalculates the number of days in a particular month:
Numberof Daysin February2012=29
Numberof Daysin February2011=28
Numberof Daysin January, March, May, July, August,October, December=31
Numberof Daysin April, June, September, November=30
Clue: use if elseand switch statement
With scanneroutput
-
7/28/2019 2 Starting JavaProgramming
45/69
LOOPING
http://docs.oracle.com/javase/
http://docs.oracle.com/javase/http://docs.oracle.com/javase/ -
7/28/2019 2 Starting JavaProgramming
46/69
While Statement
The while statement continually executes a block ofstatements while a particular condition is true.
Its syntax can be expressed as:
Ex: While Statement
-
7/28/2019 2 Starting JavaProgramming
47/69
Ex: While Statement
-
7/28/2019 2 Starting JavaProgramming
48/69
Do-While Statement
The difference between do-while and while is thatdo-whileevaluates its expression at the bottomof theloop instead of the top.
Therefore, the statements within the do block arealways executed at least once.
Ex: Do While Statement
-
7/28/2019 2 Starting JavaProgramming
49/69
Ex: Do-While Statement
-
7/28/2019 2 Starting JavaProgramming
50/69
The for Statement
The for statement providesa compact way toiterate over a range of values.
Programmers often refer to it as the "for The general form of the for statement can be
expressed as follows:
-
7/28/2019 2 Starting JavaProgramming
51/69
Ex: For Statement
-
7/28/2019 2 Starting JavaProgramming
52/69
Ex (2): For Statement
-
7/28/2019 2 Starting JavaProgramming
53/69
The break Statement
Thebreak statement has two forms: labeledandunlabeled.
You can useunlabeled in switch statement, or toterminatea for, while, or do-while loop.
You can uselabeled for loops to search for a valuein atwo-dimensional array.
Ex: unlabeled break Statement
-
7/28/2019 2 Starting JavaProgramming
54/69
Ex: unlabeled break Statement
Ex: labeled break Statement
-
7/28/2019 2 Starting JavaProgramming
55/69
Ex: labeled break Statement
-
7/28/2019 2 Starting JavaProgramming
56/69
The continue Statement
The continue statement skips the current iterationofa for, while , or do-while loop.
Theunlabeled form skips to the end of the innermostloop's bodyand evaluates the boolean expression thatcontrols the loop
A labeled continuestatementskips the current
iterationof an outer loopmarked with the givenlabel.
Ex: unlabeled continue Statement
-
7/28/2019 2 Starting JavaProgramming
57/69
Ex: unlabeled continue Statement
Ex: labeled
-
7/28/2019 2 Starting JavaProgramming
58/69
Ex: labeled
continue
Statement
-
7/28/2019 2 Starting JavaProgramming
59/69
Assignment
Makea simple calculator to:
addition,
substraction,
multiplication, division
Use Class Scanner
-
7/28/2019 2 Starting JavaProgramming
60/69
NOTES
http://docs.oracle.com/javase/
http://docs.oracle.com/javase/http://docs.oracle.com/javase/ -
7/28/2019 2 Starting JavaProgramming
61/69
Escape Sequence Character
A character preceded by a backslash (\) is an escapesequence and has special meaning to the compiler.The following table shows the Java escape sequences:
-
7/28/2019 2 Starting JavaProgramming
62/69
Ex: Escape Sequence Character
-
7/28/2019 2 Starting JavaProgramming
63/69
Method printf()
The printf( ) method automatically usesFormatter to create a formatted string.
String format
args
-
7/28/2019 2 Starting JavaProgramming
64/69
Method printf() Result
Another Ex: printf()
-
7/28/2019 2 Starting JavaProgramming
65/69
Another Ex: printf()
Printf() to command line summary
-
7/28/2019 2 Starting JavaProgramming
66/69
Printf() to command line summary
Source: http://www.java2s.com/Tutorial/Java/0120__Development/printftocommandlinesummary.htm
Integer Literals
http://www.java2s.com/Tutorial/Java/0120__Development/printftocommandlinesummary.htmhttp://www.java2s.com/Tutorial/Java/0120__Development/printftocommandlinesummary.htm -
7/28/2019 2 Starting JavaProgramming
67/69
Integer Literals
-
7/28/2019 2 Starting JavaProgramming
68/69
Floating-Point Literals
THANKS
-
7/28/2019 2 Starting JavaProgramming
69/69
THANKSContact: [email protected]