Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Chapter 1

Java Programming Fundamental

Meas Vandeth (012 885 348)


Objectives
• ណែនាំអោយចំនុចពិសេសរបស់​JAVA LANAGUAGE
• ទំរង់ទូរទៅនៃ JAVA APPLICATION PROGRAM
• Compile and Run Java Application
• ប្រភេទទិន្នន័យមូលដ្ឋា នរបស់ JAVA និង OBJECT TYPE
សំខាន់ៗ
• ប្រមាណវិធី និង Statement មូលដ្ឋា ន
1. Introduction to Java Programming Lanuage
• Java Programming Language is an 100% Object Oriented Programming
Language develop by Sun Micro System now under Oracle Corp.
• ចំនុចពិសេសនៃ JAVA
• OOP
• OS Independent
• Safe and Secure
• Rich models and Platform
• Support distributed application
• Support Multi Threaded
2. ទំរង់ទូរទៅនៃ JAVA Application
import packagename1.*;
import packagename2.*;
…..
class ClassName [extends ClassName] [implements InterfaceName,..
{
User define member
Constructor
public static void main(String arg[]) {
main methods statements
}
}
អក្
NOTE
• Java PL case sensitive( ប្កាន់អក្សរធំតូច
សរធំ
តូច )
• វីធីសាស្តចំណាំ
• All key words are lower case. Ex: import, class, public, primitive data types,…
• Class name or Object type Initial Cap every word. Ex: ResultSet, BufferReader,
FileOutputStream,….
• Methods: Initial Cap from Second Words first word is lower case. Ex close(),
closeAll()
next(), nextLine(), toLowerCase(),…….
• Data Member in original java language mostly are constant and its format lower case
all and UPPER CASE ALL.
ex. Color.red=Color.RED, Math.pi=Math.PI,……
3. Compile and run java Application
Step 1: Edit java code in any text editor or use java application framework like
NetBean or Jcreator or Eclipse….
Step 2: Save code with extension *.java ( recommend ClassName.java in a
specific folder)
Step 3: Compile java in Java Byte Codes
java_home_path\javac Filename.java
To compile go into your (path) folder by “CD Foldname” to exit path “CD\”
Step 4 Run
java_home_path\Java MainClassName
• Console Output: 4. Input/output
System.out.print(data);//output and keep courser at ending line
System.out.println(data); // output and enter( go to New Line)
Input:
ប្ប្ស់ class Scanner នៃ Package java.util.*;
Scanner objSc=new Scanner(System.in);
Methods:
objSc.next*(); ដែល * តំណាង អោយ Datatype
String nextLine(),int nextInt(),double nextDouble(),….
Operator
• Like C or C++
ប្រមាណវិធីពិជគណិ ត
ប្រមាណវិធី LOGIC
Data Types
• Primitive types:
ជា ប្រភេទទិន្ន័យដែលអថេរទទួលទុកតំលៃផ្ទា ល់ និងអាចអនុវត្តប្រមាណវិធី
ដោយផ្ទា ល់។

• Object types: ជា Class សំរាប់បង្កើត Objects ដែល ខ្ចប់ទុក Data


Member សំរាប់ទុក DATA និង Methods​ផ្ទុក កូដសំរាប់ Process Data.
People p=new People();
p.name,p.age
Primitive Type
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647

long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits

double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits

boolean 1 bit Stores true or false values


char 2 bytes Stores a single character/letter or ASCII values
• Declaration
Primitive Type varName[=initial value];
By default every variable is null. Every java data type has null value. Every operation with null return null always.
ObjectType objVar[=new ObjectType([Arg1,…]); //ObjecType=ClassName
Ex int x=0;
double sum=0;
Scanner objSc=new Scanner(System.in);
TextField txt=new TextField();

Strng s=“”;
s=s+”Hello world”; // return “ Hello world”
String s1;
s1=s1+”Hello world”;// return null;
Data Conversion
(Destination Type) Expression
x=(int) s*10;
char ch=‘A’;
int code=(int) ch; //code=65 is ascii code of char អាចបញ្ច រាសមកវិញបាន
char c=(char) 65;
To convert object type use methods of Objects.
• NOTE: every primitive type has a partner Object Type.
Primitive type Object Type
byte Byte

• int Integer
Example Convert String to Values.
double Double
boolean Boolean
char Char
long Long
Ex
Long x=10;// Error
Long x=new Long(10); // correct
Double x; // no error
x=20.5; //Error because of x is Object Type
To convert “Number” String to number value use Object Type of Number to
convert.
Use method parse*(* Primitive Type)
ex: int x=Integer.parseInt(“123”);
double s=Double.parseDouble(“20.75”);
Convert String
• String s=new String(Value);
• ObjectType ភាគច្រើន​មាន Method toString();
• ចំពោះ Primitive type
• pritiveVar+Empty String => String
String s;
double x=20;
s=“”+x; // ok convert x to String
Example txt ជា TextField object;
double textVal=Double.parseDouble(txt.getText());
txtVal =…….;
txt.setText(textVal); // error because of textVal is double
txt.setText(textVal+””);// OK Convert and set value
No. Method
Methods of String Description

1 char charAt(int index) returns char value for the particular index(Start from)

2 int length() returns string length

3 static String format(String format, Object... args) returns a formatted string.

4 static String format(Locale l, String format, Object... args) returns formatted string with given locale.

5 String substring(int beginIndex) returns substring for given begin index.

6 String substring(int beginIndex, int endIndex) returns substring for given begin index and end index.

7 boolean contains(CharSequence s) returns true or false after matching the sequence of char value.

8 boolean equals(Object another) checks the equality of string with the given object.

9 boolean isEmpty() checks if string is empty.

10 String concat(String str) concatenates the specified string.


Methods of String
11 String replace(char old, char new) replaces all occurrences of the specified char value.

12 String replace(CharSequence old, CharSequence new) replaces all occurrences of the specified CharSequence.

13 static String equalsIgnoreCase(String another) compares another string. It doesn't check case.

14 String[] split(String regex) returns a split string matching regex.

15 String[] split(String regex, int limit) returns a split string matching regex and limit.

16 String intern() returns an interned string.

17 int indexOf(int ch) returns the specified char value index.

18 int indexOf(int ch, int fromIndex) returns the specified char value index starting with given index.

19 int indexOf(String substring) returns the specified substring index.

20 int indexOf(String substring, int fromIndex) returns the specified substring index starting with given index.
21 String toLowerCase()
Method of String
returns a string in lowercase.

22 String toLowerCase(Locale l) returns a string in lowercase using specified locale.

23 String toUpperCase() returns a string in uppercase.

24 String trim() removes beginning and ending spaces of this string.

25 static String valueOf(int value) converts given type into string. It is an overloaded method.

26 compareTo(String S2) Return 0 if Equal,>0 if S2 smaller, <0 if S2 Bigger


copareToIgnoreCase(String s2)
Methods of Math
• ស្វែងយល់ របស់ class Math, every methods in Math are static.
Math.PI
Math.round(x)
Math.ceil(x);
Math.floor(..)
Math.sqrt()
Math.abs()
Math.pow
Math.sin,cos,tan,…..
Math.random()=> 0 to 1 return double
Int rval=(int) (Math.random()*10);
…………………………………
Call Math.methName(arg,…);
StringTokenizer
In pacakage java.util.*;
Object សំរាប់ កាត់ជា កង់ៗៗ
Contructor
StringTokenizer(String s);// កាត់តាម space
StringTokenizer(String s,String symbol); // កាត់តាម
symbol
Methods: String nextToken(); read 1 by one sub String
int countTokens(); ចំនួន Sub String
boolean hasMoreTokens()
Control statement
Control statement/control structure
- Selection(if/swith.. Case)
if(condition)
statements
else
statements
-swith(Expression)
witch(expression)
{
case x: // code block break;
case y:
// code block break;
default: // code block
}
break use to exit loop or case
continue use to skip loop.
• while (condition)
{
// code block to be executed
}
for (statement 1; statement 2; statement 3)
{ // code block to be executed }
• do{
• //code to be executed / loop body
• //update statement
• }while (condition);

You might also like