2 Starting JavaProgramming
2 Starting JavaProgramming
http://docs.oracle.com/javase/
Content
Java Structure Compile & Running Program Language Basics
Variables Data Types Operators
http://docs.oracle.com/javase/
JAVA STRUCTURE
http://docs.oracle.com/javase/
Example
modifier class_name
main program
Details Halo.java
Define class and modifier That can be compiled and executed by the JVM
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
println after display text produce a new line just display text
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.
new package private protected public return short static strictfp** super
switch synchronized this throw throws transient try void volatile while
Instance Variables
Can be access with instance class
Class/Static Variables
Can be access with static class
Local Variables
Parameters
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.
Operators (4)
Conditional Operators
perform Conditional-AND and Conditional-OR operations on two boolean expressions.
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
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:
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
http://docs.oracle.com/javase/
Branching statements
Break Continue return
Ex: IF ELSE
Ex: Switch
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:
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.
Assignment
Make a simple calculator to:
addition, substraction, multiplication, division Use Class Scanner
NOTES
http://docs.oracle.com/javase/
Method printf()
The printf( ) method automatically uses Formatter to create a formatted string.
String format
args
Source : http://www.java2s.com/Tutorial/Java/0120__Development/printftocommandlinesummary.htm
Integer Literals
Floating-Point Literals
THANKS
Contact: [email protected]