文档介绍:一、Java编程基础
Type Name
Kind of value
Memory Used
Size Range
byte
integer
1 byte
-128 to 127
short
integer
2 bytes
-32768 to 32767
int
integer
4 bytes
-2147483648 to 2147483647
long
integer
8 bytes
-9223372036854775808 to …07
float
floating-point
4 bytes
+/-+38 to
+/--45
double
floating-point
8 bytes
+/-**********e+308 to
+/--324
char
character
(Unicode)
2 bytes
all Unicode characters
boolean
true of false
1bit
not applicable
1
牛牛文库文档分享
一、Java编程基础
(1)匿名常量
int x = 1;
float y = ;
char z = ‘A’;
boolean flag = true;
(2)命名常量
final double pi = ;
2
牛牛文库文档分享
一、Java编程基础
Scanner input = new Scanner();
double x = ();
int y = ();
Scanner的方法
nextByte()
nextShort(),nextInt(),nextLong()
nextFloat(),nextDouble()
next() //读取一个字
nextLine() //读取一行文本
3
牛牛文库文档分享
Console I/O
import .*;
public class ScannerDemo{
public static void main(String[] args){
int n1,n2;
Scanner scannerObject = new Scanner();
(“Enter two whole numbers:”);
n1 = ();
n2 = ();
//() reads one word
//() reads an entire line.
(“You entered “ + n1 + “ and “ + n2);
}
}
4
牛牛文库文档分享
一、Java编程基础
(“abc”);
(“abc”);
(“abc\n”);
5
牛牛文库文档分享
一、Java编程基础
char symbol;
symbol = ‘A’;
char ch = ‘B’;
int m;
m = 4;
int n = 5;
double x = ;
boolean male = true;
6
牛牛文库文档分享
一、Java编程基础
(1)强制类型转换
double distance = ;
int points = (int)distance;
char ch = ‘7’;
int a = (int)ch; //variable a has value 55.
(2)隐含类型转换
byteshortintlo