Chapter 1
Chapter 1
Chapter 1
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
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)
4 static String format(Locale l, String format, Object... args) returns formatted string with given locale.
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.
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.
15 String[] split(String regex, int limit) returns a split string matching regex and limit.
18 int indexOf(int ch, int fromIndex) returns the specified char value index starting with given 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.
25 static String valueOf(int value) converts given type into string. It is an overloaded method.