Java
Java :
Java
byte
- byte 8
- -128-2^7
- 1272^7-1
- 0
- byte byte int
- byte a = 100byte b = -50
short
- short 16
- -32768-2^15
- 327672^15 - 1
- Short byte shortint
- 0
- short s = 1000short r = -20000
int
- int 32
- -2,147,483,648-2^31
- 2,147,483,6472^31 - 1
- int
- 0
- int a = 100000, int b = -200000
long
- long 64
- -9,223,372,036,854,775,808-2^63
- 9,223,372,036,854,775,8072^63 -1
- 0L
-
long a = 100000Llong b = -200000L
"L""l""1"
float
- float 32IEEE 754
- float
- 0.0f
- float f1 = 234.5f
double
- double 64 IEEE 754
- double
- double
- 0.0d
-
double d1 = 7D ; double d2 = 7.; double d3 = 8.0; double d4 = 8.D; double d5 = 12.9867;
7 int 7D7. 8.0 double
boolean
- boolean
- true false
- true/false
- false
- boolean one = true
char
- char 16 Unicode
- \u0000 0
- \uffff 65535
- char
- char letter = 'A';
public class PrimitiveTypeTest {
public static void main(String[] args) {
// byte
System.out.println("byte " + Byte.SIZE);
System.out.println("java.lang.Byte");
System.out.println("Byte.MIN_VALUE=" + Byte.MIN_VALUE);
System.out.println("Byte.MAX_VALUE=" + Byte.MAX_VALUE);
System.out.println();
// short
System.out.println("short " + Short.SIZE);
System.out.println("java.lang.Short");
System.out.println("Short.MIN_VALUE=" + Short.MIN_VALUE);
System.out.println("Short.MAX_VALUE=" + Short.MAX_VALUE);
System.out.println();
// int
System.out.println("int " + Integer.SIZE);
System.out.println("java.lang.Integer");
System.out.println("Integer.MIN_VALUE=" + Integer.MIN_VALUE);
System.out.println("Integer.MAX_VALUE=" + Integer.MAX_VALUE);
System.out.println();
// long
System.out.println("long " + Long.SIZE);
System.out.println("java.lang.Long");
System.out.println("Long.MIN_VALUE=" + Long.MIN_VALUE);
System.out.println("Long.MAX_VALUE=" + Long.MAX_VALUE);
System.out.println();
// float
System.out.println("float " + Float.SIZE);
System.out.println("java.lang.Float");
System.out.println("Float.MIN_VALUE=" + Float.MIN_VALUE);
System.out.println("Float.MAX_VALUE=" + Float.MAX_VALUE);
System.out.println();
// double
System.out.println("double " + Double.SIZE);
System.out.println("java.lang.Double");
System.out.println("Double.MIN_VALUE=" + Double.MIN_VALUE);
System.out.println("Double.MAX_VALUE=" + Double.MAX_VALUE);
System.out.println();
// char
System.out.println("char " + Character.SIZE);
System.out.println("java.lang.Character");
// Character.MIN_VALUE
System.out.println("Character.MIN_VALUE="
+ (int) Character.MIN_VALUE);
// Character.MAX_VALUE
System.out.println("Character.MAX_VALUE="
+ (int) Character.MAX_VALUE);
}
}
byte 8 java.lang.Byte Byte.MIN_VALUE=-128 Byte.MAX_VALUE=127 short 16 java.lang.Short Short.MIN_VALUE=-32768 Short.MAX_VALUE=32767 int 32 java.lang.Integer Integer.MIN_VALUE=-2147483648 Integer.MAX_VALUE=2147483647 long 64 java.lang.Long Long.MIN_VALUE=-9223372036854775808 Long.MAX_VALUE=9223372036854775807 float 32 java.lang.Float Float.MIN_VALUE=1.4E-45 Float.MAX_VALUE=3.4028235E38 double 64 java.lang.Double Double.MIN_VALUE=4.9E-324 Double.MAX_VALUE=1.7976931348623157E308 char 16 java.lang.Character Character.MIN_VALUE=0 Character.MAX_VALUE=65535
FloatDouble"E+"E103.14E33.14 103 =31403.14E-3 3.14 x 10-3 =0.00314
JAVA void java.lang.Void
Java
| int | 0 |
| long | 0L |
| short | 0 |
| char | '\u0000' |
| byte | 0 |
| float | 0.0f |
| double | 0.0d |
| boolean | false |
| null |
int,short,long,byte0char\u0000float0.0fdouble0.0dbooleanfalse-
null
public class Test {
static boolean bool;
static byte by;
static char ch;
static double d;
static float f;
static int i;
static long l;
static short sh;
static String str;
public static void main(String[] args) {
System.out.println("Bool :" + bool);
System.out.println("Byte :" + by);
System.out.println("Character:" + ch);
System.out.println("Double :" + d);
System.out.println("Float :" + f);
System.out.println("Integer :" + i);
System.out.println("Long :" + l);
System.out.println("Short :" + sh);
System.out.println("String :" + str);
}
}
Bool :false Byte :0 Character: Double :0.0 Float :0.0 Integer :0 Long :0 Short :0 String :null
- JavaC/C++ EmployeePuppy
- null
- Site site = new Site("Runoob")
Java
Java final
final double PI = 3.1415927;
byte a = 68; char a = 'A'
byteintlongshort168
0 8 0x 16 ,
int decimal = 100; int octal = 0144; int hexa = 0x64;
Java
"Hello World" "two\nlines" "\"This is in quotes\""
Unicode
char a = '\u0001'; String a = "\u0001";
Java
| \n | (0x0a) |
| \r | (0x0d) |
| \f | (0x0c) |
| \b | (0x08) |
| \0 | (0x0) |
| \s | (0x20) |
| \t | |
| \" | |
| \' | |
| \\ | |
| \ddd | (ddd) |
| \uxxxx | 16Unicode (xxxx) |
------------------------------------> byte,short,char—> int —> long—> float —> double
1. boolean
-
2.
-
3.
-
4.
int i =128; byte b = (byte)i;
byte 8 127 int byte 128
-
5.
(int)23.7 == 23; (int)-45.89f == -45
: short1632intfloat3264double
public class ZiDongLeiZhuan{
public static void main(String[] args){
char c1='a';//char
int i1 = c1;//charint
System.out.println("charint"+i1);
char c2 = 'A';//char
int i2 = c2+1;//char int
System.out.println("charint"+i2);
}
}
:
charint97 charint66
c1 a , ASCII int 97 A 65 i2=65+1=66
1.
2. (type)value type
public class ForceTransform { public static void main(String[] args){ int i1 = 123; byte b = (byte)i1;//byte System.out.println("intbyte"+b); } }intbyte123
1 int
2 double float F f
Java
ali***[email protected]
~
L
ali***[email protected]
LadyLeane
q-b***sn.com
LadyLeane
q-b***sn.com
bul***[email protected]
a+b
javaintjavaintint
bul***[email protected]
bbb
115***[email protected]
char a = 'S'; char
String a = "I AM FINE"; String
bbb
115***[email protected]
CHENG
159***[email protected]
1
floatdouble
Boolean(boolean value)Character(char value)Integer(int value)Long(long value)Float(float value)Double(double value)
Value()intValue()doubleValue()
2
3,
1,
"32.1"double:new Float("32.1").doubleValue():Double.valueOf("32.1").doubleValue()
2parseXXX
3CharactergetNumericValue(char ch)
4Date
DateintDate
Date197011000DateDate(long date)
DateDategetYear()getMonth()getDate()getHours()getMinutes()getSeconds()getDay()Dateint
DategetTime()DatetoString()String
Date20020324
import java.text.SimpleDateFormat; java.util.Date date = new java.util.Date(); //YYYYMMDD SimpleDateFormat sy1=new SimpleDateFormat("yyyyMMdd"); String dateFormat=sy1.format(date); // SimpleDateFormat sy=new SimpleDateFormat("yyyy"); SimpleDateFormat sm=new SimpleDateFormat("MM"); SimpleDateFormat sd=new SimpleDateFormat("dd"); String syear=sy.format(date); String smon=sm.format(date); String sday=sd.format(date);1 boolean
2
3
CHENG
159***[email protected]
591***[email protected]
java
String.valueOf s value StringBuilder hello value hello
String s = null; s = (new StringBuilder(String.valueOf(s))).append("hello").toString(); System.out.println(s);591***[email protected]
2
yum***[email protected]
Integer
int Integer Integer valueOf(int i)
/** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #Integer(int)}, as this method is likely * to yield significantly better space and time performance by * caching frequently requested values. * * This method will always cache values in the range -128 to 127, * inclusive, and may cache other values outside of this range. * * @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */ public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }i >= -128 && i <= 127 Integer.valueOf(i) i IntegerCachestatic final Integer cache[]:
:
a b a == b
2
yum***[email protected]
flaming
248***[email protected]
booleancharbyteshortintlongfloatdouble
BooleanCharacterByteShortIntegerLongFloatDouble
Java 8 primitive typereference type
flaming
248***[email protected]
758***[email protected]
byte 8
short 16
int 32
long 64
float 32IEEE 754
double 64 IEEE 754
boolean
char 16 Unicode
758***[email protected]
207***[email protected]
@ javadoublefloatfFfloat
longlLintint
207***[email protected]
bri***q.com
@CHENG CharactergetNumericValue(char ch)
Unicode
intA-Z
'\u0041''\u005A''\u0061''\u007A''\uFF21''\uFF3A''\uFF41''\uFF5A'10 35 Unicodechar361-9_A-Z A=10 Z=25
-1 -2
bri***q.com