" !" println() (ln ) out System, java.lang. FirstProgram.java ( .java ), .
Package chapt01;
chapt01, . , , . package , . ( ), .
2 3 FirstProgram.java. .
( chapt01):
javac chapt01/FirstProgram.java
FirstProgram.class
Mustang.class. Java:
Java chapt01.FirstProgram
FirstProgram.class chapt01, .
, , java.sun.com. . JDK
( Windows) c:\jdk1.6.0, , Java , LASSPATH :
LASSPATH=.;c:\jdk1.6.0\
. , :\temp, .
, PATH
PATH=c:\jdk1.6.0\bin
javac.exe java.exe. JDK .
, . , . , . .
main(). , , String[] args. args[0] .. args.length.
|
|
/* # 4: : OutArgs.java */
package chapt01;
public class OutArgs {
public static void main(String[] args) {
for (String str: args)
System. out. printf("A-> %s%n", str);
}
}
Java 5.0 for Java printf().
for (int i = 0; i < args.length; i++)
System. out. println("A-> " + args[i]);
:
java chapt01.OutArgs 2007 Mustang "Java SE 6"
:
-> 2007
-> Mustang
-> Java SE 6
, , .
Java , , . C++: ; , , ; inline, C++, ; public, private, protected , , , , ++; private, . :
[] class [extends ]
[implements _]{/* */}
public ( ), final ( ), abstract ( , ). (friendly). . friendly . , - .
, extends, , implements. , .
// # 5: Java Beans : User.java
package chapt01;
public class User {
public int numericCode; //
private String password;
public void setNumericCode(int value) {
if (value > 0) numericCode = value;
else numericCode = 1;
}
public int getNumericCode() {
return numericCode;
}
public void setPassword(String pass) {
if (pass!= null) password = pass;
else password = "11111";
}
public String getPassword() {
//public String getPass() {//
return password;
}
}
User numericCode password, public private. password , setPassword(). numericCode User. public - .
|
|
/* # 6: ,
: UserView.java: Runner.java */
package chapt01;
class UserView {
public static void welcome(User obj) {
System. out. printf("! : %d, : %s",
obj.getNumericCode(), obj.getPassword());
}
}
public class Runner {
public static void main(String[] args) {
User user = new User();
user.numericCode = 71; // -
// user.password = null; //
user.setPassword("pass"); //
UserView. welcome (user);
}
}
:
! : 71, : pass
. . new , :
User user; //
user = new User(); //
:
User user = new User(); /* */
new , , . , .
, . , , equals(). Object, .
/* # 7: : ComparingStrings.java */
package chapt01;
public class ComparingStrings {
public static void main(String[] args) {
String s1, s2;
s1 = "Java";
s2 = s1; //
System. out. println(" "
+ (s1 == s2)); // true
//
s1 += '2';
// s1-="a"; //,
//
s2 = new String(s1);
System. out. println(" "
+ (s1 == s2)); // false
System. out. println(" "
+ s1.equals(s2)); // true
}
}
System.in . , , .
// # 8: System.in: DemoSystemIn.java
package chapt01;
public class ReadCharRunner {
public static void main(String[] args) {
int x;
try {
x = System. in. read();
char c = (char)x;
System. out. println(" : " + c + " =" + x);
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
IOException, / , main()
try-catch.
. .
// # 9: : ReadCharRunner.java
package chapt01;
import java.io.*; //
|
|
public class ReadCharRunner {
public static void main(String[] args) {
/* System.in
InputStreamReader */
InputStreamReader is =
new InputStreamReader(System. in);
/* ,
*/
BufferedReader bis = new BufferedReader(is);
try {
System. out. println(
" <Enter>:");
/* ; readLine()
try */
String name = bis.readLine();
System. out. println(", " + name);
} catch (IOException e) {
System. err. print(" " + e);
}
}
}
, , :
<Enter>:
,
, , , .
, printf() Console.
/* # 10: printf() Console: PrintDeutsch.java */
public class PrintDeutsch {
public static void main(String[] args) {
String str = "über";
System. out. println(str);
Console con = System. console ();
con.printf("%s", str);
}
}
:
□ber
über
Java - , Web-. , , Web- .
// # 11: : FirstApplet.java
import java.awt.Graphics;
import java.util.Calendar;
public class FirstApplet extends javax.swing.JApplet {
private Calendar calendar;
public void init() {
calendar = Calendar. getInstance ();
setSize(250,80);
}
public void paint(Graphics g) {
g.drawString(" :", 20, 15);
g.drawString(
calendar.getTime().toString(), 20, 35);
}
}
Calendar java.util. toString() , , drawString(). , .
main() - init() paint(). HTML- Web-, Java. () <applet> HTML- view.html:
<html><body>
<applet code= FirstApplet.class width=300 height=300>
</applet></body></html>
FirstApplet.class , HTML-. HTML- Microsoft Internet Explorer - , Java.
view.html .1.4.
. 1.4.
JDK
|
|
appletviewer.exe.
1
A
1. Hello, , .
2. , main() .
3. , n .
4. -.
5. , () .
6. , , , . Calendar java.util.
7. HTML-.
B
n . :
1. .
2. .
3. , 3 9.
4. , 5 7.
5. , .
6. , .
7. .
8. .
9. .
10. .
11. .
12. : f0 = f1 = 1, f (n) = f (n1) + f (n2).
13. -, .
14. , .
15. p = m/n n m, .
16. .
1
1.1.
:
class Quest1 {
private static void main (String a) {
System.out.println("Java 2");
} }
, Quest1 ? ( 2 )
1) Quest1 public;
2) main() String[] a;
3) main() public;
4) main().
1.2.
Java: ( 2)
1) goto;
2) , ;
3) ;
4) ;
5) .
1.3.
:
class Quest3 {
public static void main(String s[ ]) {
String args;
System.out.print(args + s);
}
}
:
1) : main() ;
2) : args ;
3) : print();
4) .
1.4.
:
public class Quest4 {
public static void main(String[] args) {
byte b[]= new byte [80];
for (int i=0;i<b.length;i++)
b[i]=(byte)System.in.read();
System.out.print(Ok);
} }
:
1) : Ok;
2) , read() IOException;
3) , b
;
4) , .
1.5.
:
public class Quest5{
public static void main(){
System.out.print(""); }
public static void main(String args){
System.out.print(""); }
public static void main(String[] args){
System.out.print(""); } }
:
1) ;
2) ;
3) ;
4) ;
5) .
( ) . .
Java , (stack). . , -, , (heap).
|
|
, . Java . . .
() | ( ) | ||
boolean | false | true, false | |
byte | 128..127 | ||
char | '\u0000' | 0..65535 | |
short | 32768..32767 | ||
int | -2147483648..2147483647 | ||
long | 922372036854775807L | ||
float | 0.0 | 3.40282347E+38 | |
double | 0.0 | 1.797693134486231570E+308 |
Java , : 35 , 071 , 051 . int. long, L (: 0xffffL). , int (2147483647), Java , long. 1.618 0.112E-05 double, , double. float, F. ('a', '\n', '\141', '\u005a'). Unicode, . Unicode , ASCII , C++. '\u code ', code . Java , ;
'\n' , '\r' , '\f' , '\t' , '\b' , '\uxxxx' Unicode, '\ddd' . J2SE 5.0 Unicode 4.0. Character.
true false, null . String . , , , .
byte à short à int à long à float à
double. Java byte short int . (). :
byte b = (byte)128; // int byte
, . , , -. Java , . (++), (--) (+=, /= ..). () .
, , # . $ _ , .
/* # 1: , :TypeByte.java */
package chapt02;
public class TypeByte {
public static void main(String[] args) {
byte b = 1, b1 = 1 + 2;
final byte B = 1 + 2;
//b = b1 + 1; //
/* b1 , b = b1 + 1; , b1 + 1 byte- */
b = (byte)(b1 + 1);
b = B + 1; //
/* B - , , B + 1, byte , */
//b = -b; //
b = (byte) -b;
//b = +b; //
b = (byte) +b;
int i = 3;
//b = i; // , int byte
b = (byte) i;
final int I = 3;
b = I; //
/*I . , byte, , */
final int I2 = 129;
//b=I2; // , .. 129 , 127
b = (byte) I2;
b += i++; //
b += 1000; //
b1 *= 2; //
float f = 1.1f;
b /= f; //
/* , . , b /= f; b = (byte)(b / f); */
}
}
, , , . , , . {}, .
Java /* */
//, , C++. /** */, :
@author ;
@version ;
@exception ;
@param , ;
@return , ;
@deprecated , ;
@since ( ) ;
@throws , ;
@see .
java-, , javadoc.exe
HTML-.
User .
package chapt02;
public class User {
/**
* personal user's code
*/
private int numericCode;
/**
* user's password
*/
private String password;
/**
* see also chapter #3 "Classes"
*/
public User() {
password = "default";
}
/**
* @return the numericCode
* return the numericCode
*/
public int getNumericCode() {
return numericCode;
}
/**
* @param numericCode the numericCode to set
* parameter numericCode to set
*/
public void setNumericCode(int numericCode) {
this. numericCode = numericCode;
}
/**
* @return the password
* return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
* parameter password to set
*/
public void setPassword(String password) {
this. password = password;
}
}
HTML- :
. 2.1. User
Java C++ . Java , *, &, ->, delete . - . + += String. ==, != = , .
, .
: +, , *, %, /, ++, &, |, ^, ~ C++. , .
, , IEEE 754 +Infinity Infinity NaN (Not a Number), , , .
+ | / | ( ) | |
+= | ( ) | /= | ( ) |
% | ( ) | ||
= | ( ) | %= | ( ) |
* | ++ | ( ) | |
*= | ( ) | ( ) |