The First Java Program class Welcome comments The
















![Method Declaration Elements Modifier Return Type Method Name Parameter public static void main(String[] args) Method Declaration Elements Modifier Return Type Method Name Parameter public static void main(String[] args)](https://slidetodoc.com/presentation_image_h2/9ce5290741bdaa0822af06257b87b831/image-17.jpg)


































![Operator Precedence class Arithmetic { public static void main(String args[]) { int x = Operator Precedence class Arithmetic { public static void main(String args[]) { int x =](https://slidetodoc.com/presentation_image_h2/9ce5290741bdaa0822af06257b87b831/image-52.jpg)


![Unary Example class Inc. Dec { public static void main(String args[]) { int x Unary Example class Inc. Dec { public static void main(String args[]) { int x](https://slidetodoc.com/presentation_image_h2/9ce5290741bdaa0822af06257b87b831/image-55.jpg)














- Slides: 69


The First Java Program ��������� class Welcome { comments /* The Welcome Program ---------Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System. out. println("Welcome to Java!"); } } attawit_it@hotmail. com

The First Java Program class Welcome { /* The Welcome Program ---------Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System. out. println("Welcome to Java!"); } ������� } attawit_it@hotmail. com �� main() �������

The First Java Program class Welcome { ������� input �������� /* The Welcome Program arguments ���� parameters ---------Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System. out. println("Welcome to Java!"); } } attawit_it@hotmail. com

The First Java Program class Welcome { ��������� output ������ caller ทเรยกวา return value /* The Welcome Program ---------Illustrates a simple program displaying a message. */ } public static void main (String [ ] args) { System. out. println("Welcome to Java!"); } attawit_it@hotmail. com

The First Java Program class Welcome { ����� static ���������������� /* The Welcome Program ---------Illustrates a simple program displaying a message. */ } public static void main (String [ ] args) { System. out. println("Welcome to Java!"); } attawit_it@hotmail. com

The First Java Program class Welcome { ����������� �public ��������� /* The Welcome Program ---------Illustrates a simple program displaying a message. */ } public static void main (String [ ] args) { System. out. println("Welcome to Java!"); } attawit_it@hotmail. com

The First Java Program class Welcome { /* The Welcome Program ---------Illustrates a simple program displaying a message. */ } public static void main (String [ ] args) { System. out. println("Welcome to Java!"); } attawit_it@hotmail. com ���������


Java First program (3) System. out. println(“Welcome to Java!"); n n System. out - เปน standard output object เมธอด System. out. println ใชแสดงผลขอความทอยภายในเครองหมายคำ พดและสนสดชดคำสงดวยเครองหมาย semicolon ; attawit_it@hotmail. com

Program Component: Comment */ Program Hello This program displays a message “Hello Test” on the screen. */ import java. lang. *; class Hello { public static void main(String[] args) { System. out. println("Hello Test"); { { attawit_it@hotmail. com Comment

Matching Comment Markers /* This is a comment on one line */ /* Comment number 1 */ /* Comment number 2 */ These are part of the comment. /* /* /* This is a comment */ */ attawit_it@hotmail. com Error: No matching beginning marker.

Three Types of Comments /* This is a comment Multiline Comment with three lines of text. */ // This is a comment // This is another comment // This is a third comment attawit_it@hotmail. com Single line Comments

Component: Import Statement */ Program Hello This program displays a message “Hello Test” on the screen. */ import java. lang. *; Import Statement class Hello { public static void main(String[] args) { System. out. println("Hello Test"); { { attawit_it@hotmail. com

Component: Class Declaration */ Program Hello This program displays a message “Hello Test” on the screen. */ Class Declaration import java. lang. *; class Hello { public static void main(String[] args) { System. out. println("Hello Test"); { { attawit_it@hotmail. com

Component: Method Declaration */ Program Hello This program displays a message “Hello Test” on the screen. */ import java. lang. *; Method Declaration class Hello { public static void main(String[] args) { System. out. println("Hello Test"); { { attawit_it@hotmail. com
![Method Declaration Elements Modifier Return Type Method Name Parameter public static void mainString args Method Declaration Elements Modifier Return Type Method Name Parameter public static void main(String[] args)](https://slidetodoc.com/presentation_image_h2/9ce5290741bdaa0822af06257b87b831/image-17.jpg)
Method Declaration Elements Modifier Return Type Method Name Parameter public static void main(String[] args) { System. out. println("Hello Test"); { attawit_it@hotmail. com Method Body

The First Java Program class Welcome { */ * The Welcome Program * * Illustrates a simple program displaying a message. */ public static void main(String[] args) { System. out. println("Welcome to Java!"); { { attawit_it@hotmail. com

JAVA Keywords abstract continue for new switch assert*** default goto* package synchronized boolean do if private this break double implements protected throw byte else import public throws )added in 5. 0( instanceof return transient catch extends int short try char final interface static void class finally long strictfp** volatile const* float native super while case enum**** 50 keywords หามใชในการนำไปตงชอ attawit_it@hotmail. com Class, method, variable

Access Modifiers (3) v private : Makes a method or a variable accessible only from within its own class. v protected : Makes a method or a variable accessible only to classes in the same package or subclasses of the class. v Public : Makes a class, method, or variable accessible from any other class. attawit_it@hotmail. com

Class, Method & Variable Modifiers (13) v abstract : Used to declare a class that cannot be instantiated, or a method that must be implemented by a nonabstract subclass. v class : Keyword used to specify a class. v extends : Used to indicate the superclass that a subclass is extending. v final : Makes it impossible to extend a class, override a method, or reinitialize a variable. v implements : Used to indicate the interfaces that a class will implement. v interface : Keyword used to specify an interface. v native : Indicates a method is written in a platformdependent language, such as C. attawit_it@hotmail. com

Class, Method & Variable Modifiers (…) v new : Used to instantiate an object by invoking the constructor. v static : Makes a method or a variable belong to a class as opposed to an instance. v strictfp : Used in front of a method or class to indicate that floating-point numbers will follow FP-strict rules in all expressions. v synchronized : Indicates that a method can be accessed by only one thread at a time. v transient : Prevents fields from ever being serialized. Transient fields are always skipped when objects are serialized. v volatile : Indicates a variable may change out of sync because it is used in threads. attawit_it@hotmail. com

Flow Control (12) v break : Exits from the block of code in which it resides. v case : Executes a block of code, dependent on what the switch tests for. v continue : Stops the rest of the code following this statement from executing in a loop and then begins the next iteration of the loop. v default : Executes this block of code if none of the switch-case statements match. v do : Executes a block of code one time. It performs a test to determine whether the block should be executed again. v else : Executes an alternate block of code if an if test is false. v for : Used to perform a conditional loop for a block of code. attawit_it@hotmail. com

Flow Control (…) v if : Used to perform a logical test for true or false. v instanceof : Determines whether an object is an instance of a class, superclass, or interface. v return : Returns from a method without executing any code that follows the statement (can optionally return a variable. ( v switch : Indicates the variable to be compared with the case statements. v while : Executes a block of code repeatedly while a certain condition is true. attawit_it@hotmail. com

Error Handling (6) v catch : Declares the block of code used to handle an exception. v finally : Block of code, usually following a try-catch statement, which is executed no matter what program flow occurs when dealing with an exception. v throw : Used to pass an exception up to the method that called this method. v throws : Indicates the method will pass an exception to the method that called it. v try : Block of code that will be tried, but which may cause an exception. v assert : Evaluates a conditional expression to verify the programmer’s assumption. attawit_it@hotmail. com

Package Control (2) v v import : Statement to import packages or classes into code. package : Specifies to which package all classes in a source file belong. attawit_it@hotmail. com

Primitives (8)*** v boolean : A value indicating true or false. v byte : An 8 -bit integer (signed. ( v char : A single Unicode character (16 -bit unsigned( v double : A 64 -bit floating-point number (signed. ( v float : A 32 -bit floating-point number (signed. ( v int : A 32 -bit integer (signed. ( v long : A 64 -bit integer (signed. ( v short : A 16 -bit integer (signed. ( attawit_it@hotmail. com

Variable Keywords (2) v super : Reference variable referring to the immediate superclass. v this : Reference variable referring to the current instance of an object. attawit_it@hotmail. com

Void Return Type Keyword (1) v void : Indicates no return type for a method. attawit_it@hotmail. com

Unused Reserved Words (2) v const : Do not use to declare a constant; use public static final. v goto : Not implemented in the Java language. It’s considered harmful. attawit_it@hotmail. com

VARIABLES & DECLARATIONS attawit_it@hotmail. com


Data Types v Data types: เปนการระบใหคอมไพเลอรทราบถงวธการนำเสนอคา และสงทสามารถมากระทำกบคานน ๆ โดยปกตชนดของขอมลจะมสองแบบ คอ n primitive types ไดแก integer, real, boolean และ character n user-defined types ไดแก classes ตางๆ v Primitive types – ในจาวาจะมดวยกน 8 แบบ คอ : n Integers: byte, short, int, long n Real numbers: float, double n Boolean (logical) values: boolean n Characters: char attawit_it@hotmail. com

integer types v โดยปกตจะใชพนทในการจดเกบคา 231 - 1 ) Type Storage byte 1 byte short 2 bytes int 4 bytes long 8 bytes v ตวอยางการประกาศตวแปร int i; short rocket. Fuel; long angle, magnitude; byte red, green, blue; attawit_it@hotmail. com 4 bytes ( -231 to Range -128 to 127 -32678 to 32767 -2147483648 to 2147483647 -9233372036854775808 to 9233372036854775807


Ranges of Primitive Numbers attawit_it@hotmail. com



Integer Literals การกำหนดคาตวแปลแบบ int สามารถทำได v Decimal Literals (base 10( int length = 343; v Octal Literals (base 8( int int 3 รปแบบ five = 06; // Equal to decimal 6 seven = 07; // Equal to decimal 7 eight = 010; // Equal to decimal 8 nine = 011; // Equal to decimal 9 v Hexadecimal Literals (base 16 (“ 0 1 2 3 4 5 6 7 8 9 a b c d e f” int int x = y = z = length = 343; x = 0 X 0001; y = 0 x 7 fffffff; z = 0 x. Dead. Cafe; 1; 2147483647; -559035650; attawit_it@hotmail. com

Floating-Point Literals FLOAT ตองม ‘F’ หรอ ‘f’ ตอทายตวแปลแบบ float v float f = 23. 467890; // Compiler error, possible loss of precision v float g = 49837849. 029847 F; // OK; has the suffix "F" DOUBLE v double d = 110599. 995011 D; // Optional, not required v double g = 987. 897; // No 'D' suffix, but OK. v double d 1 = 123. 4; v double d 2 = 1. 234 e 2; // same value as d 1, but in scientific notation attawit_it@hotmail. com


Default Values Data Type Default Value (for fields) byte 0 short 0 int 0 long 0 L float 0. 0 f double 0. 0 d char 'u 0000' String (or any object) null boolean false attawit_it@hotmail. com

Declaring Variables v ทก ๆ ตวแปรทถกนำไปใชในโปรแกรมจะตองถกประกาศไวก อนเสมอ v Syntax: type name; v ตวอยางเชน int n; // primitive double x; // primitive boolean completed; // primitive String name; // class Button close. Btn; // class attawit_it@hotmail. com






OPERATOR & ASSIGNMENT attawit_it@hotmail. com


![Operator Precedence class Arithmetic public static void mainString args int x Operator Precedence class Arithmetic { public static void main(String args[]) { int x =](https://slidetodoc.com/presentation_image_h2/9ce5290741bdaa0822af06257b87b831/image-52.jpg)
Operator Precedence class Arithmetic { public static void main(String args[]) { int x = 17, y = 5; System. out. print("x = " + x); System. out. println(" y = " + y); System. out. println(" x + y = " + (x System. out. println(" x - y = " + (x System. out. println(" x * y = " + (x System. out. println(" x / y = " + (x System. out. println(" x % y = " + (x { { attawit_it@hotmail. com + * / % y)); y)); x y x x x = 17 =5 + y = 22 - y = 12 * y = 85 /y=3 %y=2


![Unary Example class Inc Dec public static void mainString args int x Unary Example class Inc. Dec { public static void main(String args[]) { int x](https://slidetodoc.com/presentation_image_h2/9ce5290741bdaa0822af06257b87b831/image-55.jpg)
Unary Example class Inc. Dec { public static void main(String args[]) { int x = 8, y = 13; System. out. println("x = " + x); System. out. println("y = " + y); System. out. println("++x = " + ++x); System. out. println("y++ = " + y++); System. out. println("x = " + x); System. out. println("y = " + y); { { attawit_it@hotmail. com x=8 y = 13 ++x = 9 y++ = 13 x=9 y = 14



JAVA CODING STANDARD attawit_it@hotmail. com

**/ * Copyright (C) 2000, Intalio Inc. * * The program(s) herein may be used and/or copied only with * the written permission of Intalio Inc. or in accordance with * the terms and conditions stipulated in the agreement/contract * under which the program(s) have been supplied. * * $Id: Example. java , v 1. 1 2000/08/03 22: 11: 13 authorname Exp $ * Date Author Changes * Aug 2 2000 author-name Created * Aug 2 2000 author-name Added new classes */ package demo; import java. util. No. Such. Element. Exception; class{ …………… } attawit_it@hotmail. com

**/ * A class representing a window on the screen. * For example: * <pre> * Window win = new Window(parent); * win. show(); * </pre> * * * @author <a href=”mailto: clown@intalio. com”>Bozo the Clown</a> * @version $Revision: 1. 4 $ $Date: 2000/02/29 22: 11: 13 $ * @see awt. Base. Window * @see awt. Button */ class Window extends Base. Window {. . . } attawit_it@hotmail. com

Why Have Code Conventions v 80% of software = Maintenance. v Hardly any software is maintained for its whole life by the original author. v Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. v If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. attawit_it@hotmail. com

Java Source Files v Beginning comments */ * Classname * * Version info * * Copyright notice /* v Package and Import statements; for example: n n n import java. applet. Applet; import java. awt; *. import java. net; *. attawit_it@hotmail. com


Naming Conventions v การตงชอควรใช ศพทภาษาองกฤษ v The terms get/set must be used where an attribute is accessed directly. n employee. get. Name(); n employee. set. Name(name); v ควรใช “is” นำหนาตวแปรและการทำงาน (variables and methods) ประเภท boolean. v The term compute can be used in methods where something is computed. v The term find can be used in methods where something is looked up. v “No” สามารถนำมาใชตอทายกบตวแปรทมลกษณะเปนตวเ ลข attawit_it@hotmail. com

Wrapping Lines v ตดบรรทดหลงเครองหมาย comma (, ). v ตดบรรทด กอนเครองหมาย operator (+ - * /). v จดระยะสำหรบบรรทดใหมใหตรงกบระดบเดม v If the above rules lead to confusing code or to code that’s squished up against the right margin, just indent 8 spaces instead. attawit_it@hotmail. com

Example function(long. Expression 1, long. Expression 2, long. Expression 3, long. Expression 4, long. Expression 5; ( var = function 1(long. Expression 1, function 2(long. Expression 2, long. Expression 3; (( long. Name 1 = long. Name 2 * (long. Name 3 + long. Name 4 - long. Name 5( *4+ longname 6; // PREFER long. Name 1 = long. Name 2 * (long. Name 3 + long. Name 4 - long. Name 5) + 4 * longname 6; //AVOID attawit_it@hotmail. com


Blank Spaces v A keyword followed by a parenthesis should be separated by a space while (true} (. . . { v A blank space should appear after commas in argument lists. v All binary operators except. should be separated from their operands by spaces. v The expressions in a for statement should be separated by blank spaces. n for (expr 1; expr 2; expr 3( n a = (a + b) / (c * d; ( n while (d++ = s} (++ n; ++ { n prints("size is " + foo + "n; (" attawit_it@hotmail. com

Q&A attawit_it@hotmail. com