Вы находитесь на странице: 1из 10

Primitive Data Types

There are eight primitive datatypes supported by Java. Primitive datatypes are
predefined by the language and named by a keyword. Let us now look into the
eight primitive data types in detail.

byte

Byte data type is an 8-bit signed two's complement integer


Minimum value is -128 (-2^7)
Maximum value is 127 (inclusive)(2^7 -1)
Default value is 0
Byte data type is used to save space in large arrays, mainly in place of
integers, since a byte is four times smaller than an integer.
Example: byte a = 100, byte b = -50

short

Short data type is a 16-bit signed two's complement integer


Minimum value is -32,768 (-2^15)
Maximum value is 32,767 (inclusive) (2^15 -1)
Short data type can also be used to save memory as byte data type. A short is
2 times smaller than an integer
Default value is 0.
Example: short s = 10000, short r = -20000

int

Int data type is a 32-bit signed two's complement integer.


Minimum value is - 2,147,483,648 (-2^31)
Maximum value is 2,147,483,647(inclusive) (2^31 -1)
Integer is generally used as the default data type for integral values unless
there is a concern about memory.
The default value is 0
Example: int a = 100000, int b = -200000
long

Long data type is a 64-bit signed two's complement integer


Minimum value is -9,223,372,036,854,775,808(-2^63)
Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
This type is used when a wider range than int is needed
Default value is 0L
Example: long a = 100000L, long b = -200000L

float

Float data type is a single-precision 32-bit IEEE 754 floating point


Float is mainly used to save memory in large arrays of floating point
numbers
Default value is 0.0f
Float data type is never used for precise values such as currency
Example: float f1 = 234.5f

double

double data type is a double-precision 64-bit IEEE 754 floating point


This data type is generally used as the default data type for decimal values,
generally the default choice
Double data type should never be used for precise values such as currency
Default value is 0.0d
Example: double d1 = 123.4

boolean

boolean data type represents one bit of information


There are only two possible values: true and false
This data type is used for simple flags that track true/false conditions
Default value is false
Example: boolean one = true

char

char data type is a single 16-bit Unicode character


Minimum value is '\u0000' (or 0)
Maximum value is '\uffff' (or 65,535 inclusive)
Char data type is used to store any character
Example: char letterA = 'A'

Escape Sequences
A character preceded by a backslash (\) is an escape sequence and has
special meaning to the compiler. The following table shows the Java escape
sequences:

Escape Sequences
Escape
Description
Sequence
Insert a tab in the text at
\t
this point.
Insert a backspace in the
\b
text at this point.
Insert a newline in the text
\n
at this point.
Insert a carriage return in
\r
the text at this point.
Insert a formfeed in the
\f
text at this point.
Insert a single quote
\' character in the text at this
point.
Insert a double quote
\" character in the text at this
point.
Insert a backslash
\\ character in the text at this
point.
When an escape sequence is encountered in a print statement, the compiler
interprets it accordingly. For example, if you want to put quotes within
quotes you must use the escape sequence, \", on the interior quotes. To print
the sentence
She said "Hello!" to me.
you would write
System.out.println("She said \"Hello!\" to me.");

JAVA KEYWORDS

Keyword What It Does


Indicates that the details of a class,
abstract a method, or an interface are given
elsewhere in the code.

Tests the truth of a condition that


assert
the programmer believes is true.

Indicates that a value is either true


boolean
or false.

break Jumps out of a loop or switch.

Indicates that a value is an 8-bit


byte
whole number.

Introduces one of several possible


case paths of execution in a switch
statement.

Introduces statements that are


executed when something
catch
interrupts the flow of execution in
a try clause.
Indicates that a value is a
character (a single letter, digit,
char
punctuation symbol, and so on)
stored in 16 bits of memory.

Introduces a class a blueprint


class
for an object.

You cant use this word in a Java


program. The word has no
const meaning but, because its a
keyword, you cant create a
variable named const.

Forces the abrupt end of the


continue current loop iteration and begins
another iteration.

Introduces a path of execution to


default take when no case is a match in a
switch statement.

Causes the computer to repeat


some statements over and over
do again (for instance, as long as the
computer keeps getting
unacceptable results).

Indicates that a value is a 64-bit


double number with one or more digits
after the decimal point.

Introduces statements that are


else executed when the condition in an
if statement isnt true.
Creates a newly defined type a
enum group of values that a variable can
have.

Creates a subclass a class that


extends reuses functionality from a
previously defined class.

Indicates that a variables value


cannot be changed, that a classs
final functionality cannot be extended,
or that a method cannot be
overridden.

Introduces the last will and


finally testament of the statements in a try
clause.

Indicates that a value is a 32-bit


float number with one or more digits
after the decimal point.

Gets the computer to repeat some


statements over and over again
for
(for instance, a certain number of
times).

You cant use this word in a Java


program. The word has no
goto meaning. Because its a keyword,
you cant create a variable named
goto.

Tests to see whether a condition is


if true. If its true, the computer
executes certain statements;
otherwise, the computer executes
other statements.

Indicates that a class provides


implements bodies for methods whose headers
are declared in an interface.

Enables the programmer to


import abbreviate the names of classes
defined in a package.

Tests to see whether a certain


instanceof
object comes from a certain class.

Indicates that a value is a 32-bit


int
whole number.

Introduces an interface. An
interface is like a class but, for the
interface
most part, an interfaces methods
have no bodies.

Indicates that a value is a 64-bit


long
whole number.

Enables the programmer to use


native code that was written in a
language other than Java.

Creates an object from an existing


new
class.

Puts the code into a package a


package collection of logically related
definitions.
Indicates that a variable or method
private can be used only within a certain
class.

Indicates that a variable or method


protected can be used in subclasses from
another package.

Indicates that a variable, class, or


public method can be used by any other
Java code.

Ends execution of a method and


return possibly returns a value to the
calling code.

Indicates that a value is a 16-bit


short
whole number.

Indicates that a variable or method


static belongs to a class, rather than to
any object created from the class.

Limits the computers ability to


represent extra large or extra small
strictfp numbers when the computer does
intermediate calculations on float
and double values.

Refers to the superclass of the


super code in which the word super
appears.

Tells the computer to follow one


switch
of many possible paths of
execution (one of many possible
cases), depending on the value of
an expression.

Keeps two threads from


synchronized
interfering with one another.

A self-reference refers to the


this object in which the word this
appears.

Creates a new exception object


and indicates that an exceptional
throw
situation (usually something
unwanted) has occurred.

Indicates that a method or


throws constructor may pass the buck
when an exception is thrown.

Indicates that, if and when an


transient object is serialized, a variables
value doesnt need to be stored.

Introduces statements that are


try watched (during runtime) for
things that can go wrong.

Indicates that a method doesnt


void
return a value.

Imposes strict rules on the use of a


volatile variable by more than one thread
at a time.
Repeats some statements over and
while over again (as long as a condition
is still true).

Вам также может понравиться