JAVA Structure

public class Name {
    public static void main(String[] args) {


    }
}
            

CODE

MEANING

SAMPLE

System.out.println()

System.out.print()

output ข้อความ ออกหน้าจา

System.out.println ("plug");

//

/* */

comment one lene and multi line

// plug and java

/* plug and java */

byte

short

int

long

float

double

ประกาศ data type numbers

byte num1 = 80;

short num2 = 80;

int num3 = 80;

long num4 = 80L;

float num5 = 80.0f;

double num6 = 80.0d;

boolean

ประกาศ data type boolean

boolean T = true;

boolean F = false;

char

ประกาศ data type char เก็บตัวอักาณ 1 ตัว หรือ ค่า ascii

char A = 'A';

char B = 66;

String

ประกาศ data type string

String txt1 = "plug";

String txt2 = "don";

System.out.print(txt1 + txt2);

+

-

*

/

%

++

--

+=

-=

operaters

int n1 = 80;

int n2 = 80;

int s1 = n1 + n2;

int s2 = n1 - n2;

int s3 = n1 * n2;

float s4 = n1 / n2;

int s5 = n1 % n2;

int s6 = ++n1;

int s7 = --n2;

n1 += 10;

n2 -= 10;

==

!=

>

<

>=

<=

comparison operaters

int n1 = 80;

int n2 = 85;

boolean p1 = n1 == n2;

boolean p2 = n1 != n2;

boolean p3 = n1 > n2;

boolean p4 = n1 < n2;

boolean p5 = n1 >= n2;

poolean p6 = n1 <= n2;

&&

||

!

logical operaters

int n = 80;

if(n <= 80 && n >= 80) {

continue;
}

else if(n <= 80 || n > 80) {

continue;
}

else if(n <= 80 && !(n > 80)) {

continue;
}

.length()

นับ string and array

String str = "plug";

int len = str.length();

.toUpperCase()

.toLowerCase()

แปลง text เป็นตัวใหญ่ หรือ เล็ก

String str = "Plug";

String up = str.toUpperCase();

String lo = str.toLowerCase();

.indexOf()

หา index ของ string ถ้า string เหมือนกันจะหาแค่ตำแหน่งแรก

String str = "plug don";

int n = str.indexOf("don");

.charAt()

get string ตามตำแหน่ง index

String str = "Plug";

char ch = str.charAt(0);

Math.max()

Math.min()

หาค่า สูงสุด และ น้อยสุด

int M = Math.max(80,85);

int m = Math.max(80,85);

Math.random()

random numbers

int R = (int)(Math.random() * 101);

Integer.parseInt()

แปลง string เป็น int

String str = "80";

n = Integer.parseInt(str);

Long.parseLong()

แปลง string เป็น long

String str = "80";

n = Long.parseLong(str);

Float.parseFloat()

แปลง string เป็น float

String str = "80";

n = Float.parseFloat(str);

Double.parseDouble()

แปลง string เป็น double

String str = "80";

n = Double.parseDouble(str);

Character.getNumericValue()

แปลง char เป็น number

char ch = '4';

int n = 0;

n = Character.getNumericValue(ch);

if

เช็คเงื่อนไขลำดับแรก

int x = 0;

if(85 > 80) {

x = 85;

}

else if

เช็คเงื่อนต่อจาก if

int x = 0;

if(85 < 80) {

x = 80;

}

else if(85 > 80) {

x = 85;

}

else

เช็คเงื่อนต่อจาก else if

int x = 0;

if(85 < 80) {

x = 80;

}

else if(85 == 80) {

x = 85;

}

else() {

x = 100;

}

for

loop กำหนดจำนวน

int x = 0

for(int i=0;i<10;i++) {

x += i;

}

while

loop ไม่มีสิ้นสุด

int x = 0;

while(x < 100) {

x += 10;

}

break

หยุดการทำงานของ loop

int i = 0;

while(true) {

i+=1;

if(i == 100) {

break;

}

}

continue

ข้าม loop

int i = 0;

while(true) {

i+=1;

if(i % 2 == 0) {

continue;

}

}

input

import java.util.Scanner

Scanner input = new Scanner


import ประกาศไว้นอกสุด ตัวแปร oop ประกาศใน main

int n = input.nextInt();

input type

ประเภท input ของ Scanner

int n1 = input.nextInt();

long n2 = input.nextLong();

float n3 = input.nextFloat();

double n4 = input.nextDouble();

String n5 = input.next();

char n6 = input.next().charAt(0);

array

ตัวแปร array ใช้เก็บข้อมูลหลายตัวในตัวแปรเดียว

int n[] = {100,200,300};

array oop

ตัวแปร array แบบ oop

int n[] = new int[4];

for-each

ใช้วน loop array โดยเฉพาะ

int n[] = {100,200,300}

for(int i : n) {

System.out.print(i)

}

JAVA Dialog

"โชว์กล่อง input บนจอ"

JOptionPane.showInputDialog()

"โชว์กล่องข้อความ"

JOptionPane.showMessageDialog()

String result;

result = JOptionPane.showInputDialog("title");

JOptionPane.showMessageDialog(null,result,"title",JOptionPane.INFORMATION_MESSAGE);