DateFormat.MEDIUM , . DateFormat LONG FULL. SHORT , .
String format(Date date) :
String dat = df.format(new Date());
Date parse(String source) , :
String str = "April 3, 2006";
Date d = df.parse(str);
, .
.
// # 18: : DemoDateFormat.java
package chapt07;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;
public class DemoDateFormat {
public static void main(String[] args) {
DateFormat df =
DateFormat. getDateInstance (DateFormat.MEDIUM, Locale.US);
Date d = null;
String str = "April 3, 2006";
try {
d = df.parse(str);
System. out. println(d);
} catch (ParseException e) {
e.printStackTrace();
}
df =
DateFormat.getDateInstance(DateFormat.FULL,
new Locale("ru","RU"));
System. out. println(df.format(d));
df =
DateFormat.getDateInstance(DateFormat.FULL,Locale.GERMAN);
System. out. println(df.format(d));
d = new Date();
// df
df = DateFormat. getTimeInstance ();
//
System. out. println(df.format(d));
}
}
:
Mon Apr 03 00:00:00 EEST 2006
2006 .
Montag, 3. April 2006
05:45:16
, :
Date d = new Date();
Locale[] locales =
DateFormat.getAvailableLocales();
for (Locale loc: locales) {
DateFormat df =
DateFormat. getDateInstance (DateFormat.FULL, loc);
System.out.println(loc.toString() + "---> "
+ df.format(d));
}
, , loc.toString().
7
A
1. k- .
k , .
2.
. , .
|
|
3. , , . .
4. , .
5. k- .
6. , , .
7. (0 1) , ,
k-o .
8. , , . .
9. , .
10. , , (, ( ) * ..).
11. , .
12. , , .
13. n ( ), .
14. , , , .
15. , .
16. , .
17. .
18. , .
19. , .
20. , .
21. , .
22. , .
23. .
24. .
25. Java (//, /*, /**).
26. . , . . .
27. , .
28. , .
29. 0 100000, . ,
n m.
30. , , , , .
31. , .
32. %user%%/user%
<a href=http://www.my.by/search.htm?param=></a>.
33. Java getter setter- , .
34.
.
B
1. , . , . , , .
|
|
2. , .
3. , .
4. .
5. , .
6. n , , .., (n-1)- n-, n- . , .
7. : 1, 4, 7, 10- .. ( ) , 2, 5, 8, 11-
.. ( ) , 3, 6, 9, 12- .. .
8. , , .
9.
. , , .
10. ( ).
11. , , .
12. . .
13. . , , .
14. ,
.
15. ,
.
16. .
17. ,
.
18. ,
.
19. , .
20. , () () .
21. , .
22. n2 :
n , ;
900 ;
1- , 2- ..
1- 2-, 3- 4- ..
, .
.
23. , , .
24. , .
25. , , , . , hellowoooorld hel2owo4rld.
26. , .
|
|
27. , . ? ( ) * ( ).
28. , . .
29. , : 2 ABC, 3 DEF, 4 GHI, 5 JKL, 6 MNO, 7 PQRS, 8 TUV, 9 WXYZ. , , .
30. , , .. .
31. . , . , .
32. , . .
33. . , , , .
34. . . , StringBuffer?
35. . : , , . , JAVA JAVA, AVAJ, VAJA, AJAV. AJAV, AVAJ, JAVA, VAJA. , VJAA. .
36. . , VJAA, JAVA.
7
7.1.
:
public class Quest1 {
public static void main(String[] args) {
String str = new String("java");
int i=1;
char j=3;
System.out.println(str.substring(i,j));
}}
:
1) ja;
2) av;
3) ava;
4) jav;
5) : substring().
7.2.
, v str = "Java"?
1) charAt(2,str);
2) str.charAt(2);
3) str.indexOf('v');
4) indexOf(str,'v');
7.3.
?
String s = new String("Java");
String t = new String();
String r = null;
1) r = s + t + r;
2) r = s + t + r;
3) r = s & t & r;
4) r = s && t && r;
7.4.
:
public class Quest4 {
public static void main(String[] args) {
String str="ava";
char ch=0x74; // 74 - 'J'
str=ch+str;
System.out.print(str);
}}
:
1) 74ava;
2) Java;
3) 0H74ava;
4) : ch+str;
|
|
5) : char ch=0x74;
6) .
7.5.
?
public class Quest5 {
public static void main(String[] args) {
StringBuffer s = new StringBuffer("you java");
s.insert(2, "like ");
System.out.print(s);
}}
1) yolike u java;
2) you like java;
3) ylike ou java;
4) you java like;
5) ;
6) .
() , . : , . , . , . - , . , , , . , , , .
Throwable |
Error |
Exception |
RuntimeException |
. , . Throwable Error Exception java.lang.
. 8.1.
Error . ,
, . , Error, 8.2.
. 8.2. , Error.
8.3 , Exception.
, , throws - . .
, . 8.4. , RuntimeException . , . RuntimeException . , :
if(a==null) throw new NullPionterException();
NullPionterException . , throw. main(),
printStackTrace(), .
. 8.3. (checked)
:
try-catch ;
throws ( );
.
. CloneNotSupportedException. :
public void changeObject(Student ob) {
try {
Object temp = ob.clone();
//
} catch (CloneNotSupportedException e) {
System. err. print(e);
}
}
. 8.4. (unchecked)
, ( Cloneable). , catch, , catch . try . catch(){} , - .
|
|
. , , , . throws, . . :
(_)
throws _ { }
try-catch, . , changeObject() :
public void changeObject(Student ob)
throws CloneNotSupportedException {
Object temp = ob.clone();
//
}
throws , . , changeObject():
public void load(Student stud) {
try {
changeObject(stud);
} catch (CloneNotSupportedException e) {
String error = e.toString();
System. err. println(error);
}
}
.
try , catch, catch .
/* # 1: : TwoException.java */
package chapt08;
public class TwoException {
public static void main(String[] args) {
try {
int a = (int)(Math. random () * 2);
System. out. println("a = " + a);
int c[] = { 1/a };
c[a] = 71;
} catch (ArithmeticException e) {
System. err. println(" 0" + e);
} catch (ArrayIndexOutOfBoundsException e) {
System. err. println(
" : " + e);
} // catch
System. out. println(" try-catch");
}
}
" 0" =0. ( =1) " " [], . , , , . , if . , , .
catch , . :
try { /*, */
} catch (RuntimeException e) { /* RuntimeException
*/
} catch (ArithmeticException e) {} /* ,
*/
try . try catch, , , catch try.
/* # 2: try-catch: MultiTryCatch.java */
package chapt08;
public class MultiTryCatch {
public static void main(String[] args) {
try { //
int a = (int) (Math. random () * 2) - 1;
System. out. println("a = " + a);
try { //
int b = 1/a;
StringBuffer sb = new StringBuffer(a);
} catch (NegativeArraySizeException e) {
System. err. println(
" : " + e);
}
} catch (ArithmeticException e) {
System. err. println(" 0" + e);
}
}
}
a=0 ArithmeticException, trycatch . .
.
throw
, , , , . throw, - , . throw . Throwable , . throw, :
throw Throwable;
- new:
throw new IOException();
throw . try catch. , , try. - throw .
, -, throw , catch,
.
/* # 3: : ThrowGeneration.java */
package chapt08;
import java.io.File;
public class ThrowGeneration {
public static void connectFile(File file) {
if (file == null ||!file.exists())
throw new IllegalArgumentException(); /*
*/
//
}
public static void main(String[] args) {
File f = new File("demo.txt");
// File f = null;
try {
connectFile (f);
} catch (IllegalArgumentException e) {
System. err. print(" unchecked-"
+ " : " + e);
}
}
connectFile() ( null) , . IllegalArgumentException, RuntimeException, main().
throws.
throw catch , (checked) throws . , RuntimeException (unchecked) , throws .
finally
, ( , ) , . finally, try catch. :
try { /*, */ }
catch (Exception1 e1) { /* 1*/ } //
catch (Exception2 e2) { /* 2*/ } //
finally { /* try, catch */ }
try catch finally. finally , . , return, break, continue. :
/* # 4: finally: StudentFinally.java */
package chapt08;
public class StudentFinally {
private static int age;
public static void setAge(int age) {
try {
//
if (age <= 0)
throw new RuntimeException(" ");
} finally {
System. out. print(" ");
//
}
System. out. print(" ");
}
public static int getAgeWoman() {
try {
return age - 3;
} finally {
return age;
}
}
public static void main(String[] args) {
try {
setAge (23);
setAge (-5);
} catch (RuntimeException e) {
e.printStackTrace();
}
System. out. print(getAgeWoman ());
}
}
setAge() - try, finally. getAgeWoman()
try return, finally.
Exception , , . , Human () age ().
/* # 5: , , :
RunnerLogic.java */
package chapt08;
public class RunnerLogic {
public static double salary(int coeff)
throws SalaryException {
double d;
try {
if ((d = 10 - 100/coeff) < 0)
throw new SalaryException("negative salary");
else return d;
} catch (ArithmeticException e) {
throw new SalaryException("div by zero", e);
}
}
public static void main(String[] args) {
try {
double res = salary (3); // 0, 71;
} catch (SalaryException e) {
System. err. println(e.toString());
System. err. println(e.getHiddenException());
}
}
}
ArithmeticException, , , SalaryException, . . - , ; - , . , . , SalaryException, getHiddenException(). , SalaryException.
/* # 6: : SalaryException.java */
package chapt08;
public class SalaryException extends Exception {
private Exception _hidden;
public SalaryException(String er) {
super (er);
}
public SalaryException(String er, Exception e) {
super (er);
_hidden = e;
}
public Exception getHiddenException() {
return _hidden;
}
}
, . .
. :
throws , ;
throws throws , .
. throws . , , , . checked- .
/* # 7: : Stone.java: WhiteStone.java: BlackStone.java: StoneLogic.java */
package chapt08;
class Stone { //
public void build() throws FileNotFoundException {
/* */ }
}
class WhiteStone extends Stone { //
public void build() {
System. out. println(" ");
}
}
public class StoneLgic { //
public static void infoStone(Stone stone) {
try {
stone.build(); // IOException
} catch (FileNotFoundException e) {
System. err. print(" ");
}
}
}
class BlackStone extends Stone { //
public void build() throws IOException{ //
System. out. println(" ");
/* */
}
}
throws , , throws
.
/* # 8: : FileInput.java: SocketInput.java */
package chapt08;
import java.io.FileNotFoundException;
import java.io.IOException;
class FileInput { //
public FileInput(String filename)
throws FileNotFoundException {
//
}
}
class SocketInput extends FileInput {
//
public SocketInput(String name)
throws FileNotFoundException {
super (name);
//
}
/
public SocketInput() throws IOException {
super ("file.txt");
//
}
/
public SocketInput(String name, int mode) { /*
*/
super (name);
//
}
}
, throws. , FileInput , .
assertion
. . , , , (, ), . .. (assertion). , .
:
int age = ob.getAge();
if (age >= 0) {
//
} else {
//
}
assertion , :
int age = ob.getAge();
assert (age >= 0): "NEGATIVE AGE!!!";
//
assert:
assert ( boolexp ): expression;
assert ( boolexp );
boolexp boolean Boolean, expression , . false, AssertionError, expression ( ).
assertion , , :
enum Mono { WHITE, BLACK }
String str = "WHITE"; //"GRAY"
Mono mono = Mono.valueOf(str);
//
switch (mono) {
case WHITE: //
break;
case BLACK: //
break;
default:
assert false: "Colored!";
}
assertion public -. : IllegalArgumentException, NullPointerException . assertion , .
Assertion :
java enableassertions MyClass
java ea MyClass
da -disableassertions.
8
A
4, /. , , . , , () , ..
B
4, /.
8
8.1.
:
class Quest1{
int counter;
java.io.OutputStream out;
Quest1(){/* out */}
float inc() {
try { counter++;
out.write(counter); }
//
}}
inc() , ? ( ).
1) catch (java.io.OutputStreamException e){};
2) catch (java.io.IOException e){};
3) catch (java.io.OutputException e){};
4) finally{};
5) return counter;
6) return;.
8.2.
meth(5)?
public int meth(int x) {
int y = 010; //1
try { y += x; //2
if (x<=5) throw new Exception(); //3
y++; } //4
catch (Exception e) { y--; } //5
return y; } //6
1) 12;
2) 13;
3) 14;
4) 15;
5) : 4.
8.3.
meth(12), mexcept(int x) ArithmeticException?
int meth(int x) {
int count=0;
try { count += x;
count += mexcept(count);
count++;
} catch (Exception e) {
--count;
return count;
}
finally {
count += 3;
return count;
}
}
1) 11;
2) 12;
3) 13;
4) 14;
5) - return finally.
8.4.
show() // Quest4?
class Base{
public void show(int i) {/**/}
}
public class Quest4 extends Base{
//
}
1) void show (int i) throws Exception {/**/}
2) void show (long i) throws IOException {/**/}
3) void show (short i){ /**/}
4) public void show (int i) throws IOException {/**/}
8.5.
:
import java.io.*;
public class Quest5 {
// ioRead()
public static void main(String[] args) {
try {
ioRead();
} catch (IOException e){}
}}
ioRead() , ?
1) private static void ioRead()
throws IOException{};
2) public static void ioRead()
throw IOException{};
3) public static void ioRead(){};
4) public static void ioRead()
throws Exception{}.
. /
/ , . . / .
File
(), , Java java.io.
File . , , , , , , , ..
File :
File myFile = new File(\\com\\myfile.txt);
File myDir = new File(c:\\jdk1.6.0\\src\\java\\io);
File myFile = new File(myDir, File.java);
File myFile = new File(c:\\com, myfile.txt);
File myFile = new File(new URI( - ));
, , . . . ,
.
File .
, : Unix / , Windows \\ . , , , File:
public static final String separator;