By: De Rosal, Ign. Moses S.
http://docs.oracle.com/javase/
Content
Java Structure Compile & Running Program Language Basics
Variables Data Types Operators
http://docs.oracle.com/javase/
Scanner Class Control Flow Looping
JAVA STRUCTURE
http://docs.oracle.com/javase/
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
Syntax to declare class:
[modifier] [class] class_name{
}
Example
modifier class_name
main program
Details Halo.java
Define class and modifier That can be compiled and executed by the JVM
Details (2) Halo.java
Main program and first time it is run by JVM Public : one of modifier String : type of argumen Static : type of method Args : Array of argumen which can be added while Void : no return value running Main : main method
Details (3) Halo.java
Function to display text in console
println after display text produce a new line just display text
COMPILE AND RUNNING
http://docs.oracle.com/javase/
Compiling Program
Complie javac name_file.java
Compiling Program
Complie will produce class file
Running Program
Running .class java class_file without
LANGUAGE BASIC
http://docs.oracle.com/javase/
Variables
The Java programming language is staticallytyped, which means that all variables must first be declared before they can be used. The Java programming language defines the following kinds of variables:
Instance Variables (Non-Static Fields) Class Variables (Static Fields) Local Variables Parameters
Naming Variables
Variable names are case-sensitive Must start with a letter (a-z, A-Z), the dollar sign "$", or the underscore character _ after the first character, can be followed by numbers(0-9). Variable names contain dash (-) or space ( ). Beginning with lowercase on the first word and uppercase letters in the second and subsequent words. Also keep in mind that the variable names you choose must not be a keyword or reserved word.
Java Language Keywords
abstract assert*** boolean break byte case catch char class const*
* ** *** ****
continue default do double else enum**** extends final finally float
for goto* if implements import instanceof int interface long native
new package private protected public return short static strictfp** super
switch synchronized this throw throws transient try void volatile while
not used added in 1.2 added in 1.4 added in 5.0
Instance Variables
Can be access with instance class
Class/Static Variables
Can be access with static class
Local Variables
declare local variable within method
Parameters
Primitive Data Types
Data type boolean byte short int long Length 1 bit 8 bit 2 byte / 16 bit 4 byte / 32 bit 8 byte / 64 bit range of values 0 and 1 -128 to 127 (-27 to 27) -32,768 to 32,767 (-215 to 215) -2,147,483,648 to 2,147,483,647 (-221 to 221) -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. (-263 to 263) 1.40129846432481707e-45 to 3.40282346638528860e+38 4.94065645841246544e-324d to 1.79769313486231570e+308d 0 to 65,535 (unsigned) Example 0; 1 -5; 10 -12,777; 31,578 -2,107,483,448 ; 2,145,483,638 9,103,372,036,854,775,807
float double char
4 byte / 32 bit 8 byte / 64 bit 2 byte / 16 bit
Default Values
Data Type byte short int long float double char String (or any object) boolean Default Value (for fields) 0 0 0 0L 0.0f 0.0d '\u0000' null false
Operators
Operators are symbols and special characters (mathematics) used in an expression
Example:
int x = 3; int y = x; int z = x * y; boolean status = true;
Operators (2)
Operators (3)
Arithmetic Operators
perform addition, subtraction, multiplication, division, and modulo.
Unary Operators
require only one 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.
Operators (4)
Conditional Operators
perform Conditional-AND and Conditional-OR operations on two boolean expressions.
Bitwise and Bit Shift Operators
To manipulated bit pattern less commonly used.
Ex: Unary Operator
Ex: Bitwise and Bit Shift Operators
Ex: Bitwise and Bit Shift Operators(2)
Operator Priority
1. 2. 3. 4. 5. Operator in bracket or parentheses "(...)" Increment and decrement operators Multiplication and division operators Addition and subtraction operators Bitwise operators
Try Out Operator
Change the following program to use assignments operator!!!
Try Out Operator answer
CLASS SCANNER
http://docs.oracle.com/javase/
Class Scanner
Import: java.util.Scanner;
A simple text scanner which can parse primitive types and strings using regular expressions. For example, this code allows a user to read a number from System.in:
Class Scanner (2)
Import: java.util.Scanner;
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 boolean data type
Ex: Class Scanner
Result Ex: Class Scanner
Ex: Class Scanner (2)
Result Ex: Class Scanner (2)
CONTROL FLOW STATEMENTS
http://docs.oracle.com/javase/
Control Flow Statements
Control Flow Statements consist of: Decision making statements,
if-then executed only if a particular test evaluates to true if-then-else Switch the switch statement can have a number of possible execution paths.
Looping statements, and
For While do-while
Branching statements
Break Continue return
Ex: IF ELSE
Ex: Switch
Try Out Decision Making Statements
Calculates the number of days in a particular month:
Number of Days in February 2012= 29 Number of Days in February 2011= 28 Number of Days in January, March, May, July, August, October, December = 31 Number of Days in April, June, September, November = 30 Clue: use if else and switch statement
With scanner
output
LOOPING
http://docs.oracle.com/javase/
While Statement
The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:
Ex: While Statement
Do-While Statement
The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.
Ex: Do-While Statement
The for Statement
The for statement provides a compact way to iterate 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:
Ex: For Statement
Ex (2): For Statement
The break Statement
The break statement has two forms: labeled and unlabeled. You can use unlabeled in switch statement, or to terminate a for, while, or do-while loop. You can use labeled for loops to search for a value in a two-dimensional array.
Ex: unlabeled break Statement
Ex: labeled break Statement
The continue Statement
The continue statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop A labeled continue statement skips the current iteration of an outer loop marked with the given label.
Ex: unlabeled continue Statement
Ex: labeled continue Statement
Assignment
Make a simple calculator to:
addition, substraction, multiplication, division Use Class Scanner
NOTES
http://docs.oracle.com/javase/
Escape Sequence Character
A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:
Ex: Escape Sequence Character
Method printf()
The printf( ) method automatically uses Formatter to create a formatted string.
String format
args
Method printf() Result
Another Ex: printf()
Printf() to command line summary
Source : http://www.java2s.com/Tutorial/Java/0120__Development/printftocommandlinesummary.htm
Integer Literals
Floating-Point Literals
THANKS
Contact: [email protected]