Anda di halaman 1dari 1

Java Data Types

Possible values Write a literal Declare a variable

int Ranges from approximately No commas or decimal int numberOfItems = 10;


negative two billion to point.
An int is an integer
positive two billion. For a
(whole number). 0
bigger range, use long. 1
2147483647

long Ranges from approximately No commas or decimal // Four billion stars


// in our Milky Way
negative nine quintillion to point. Must end with L.
A long is an integer that can go // galaxy
positive nine quintillion. long numberOfStars =
higher (and lower) than an int. 0L
4000000000L;
1L
9223372036854775807L

float Can hold up to six significant No commas, but can have a float weight = 123.5f;
decimal digits. For more decimal point. Must end with f.
A float is a number that can have
digits, use double.
a fraction. .123456f
98.6f
1000000.f
0f

double Can hold up to 15 significant No commas, but can have a double interestRate =
3.000025;
decimal digits. decimal point. If no decimal
A double is a number that can
point, must end with d.
have a fraction. Use it when you
need more digits than a float can .123456789012345
66.66667
hold. 1000000000.
0d

boolean Either true or false. All lowercase, no quotes. boolean isWinner =


true;
A boolean can hold the result of true
false
a comparison or decision.

char A char value comes from the Surround with single quotes. char myInitial = 'A';
Unicode character set.
A char is a single character, no 'A' // uppercase
more and no less. letter A
' ' // a space

String The length of a String is Surround with double String greeting =


"Good morning.";
limited only by the available quotes.
A String is series of zero or more
memory. Its characters come
characters. Use it to hold a piece "Pull the lever, Max!"
from the Unicode character " " // one space
of text. "" // no characters
set.
at all

*Code samples are licensed under the Apache 2.0 License. All other content is licensed under the Creative Commons Attribution 3.0 License.

Anda mungkin juga menyukai