Pages

Tuesday 6 August 2019

Data Types

Agenda:
  • Primitive data types
  • Literal values of primitive data types
  • Object reference variables
Primitive Data Types:
  • There are eight primitive data types in java.
  •  byte,short,int,long,float,double,character,boolean these are predefined.
  • A variable defined as one of the primitive data types is a primitive variable.
  • byte, short, int, long represents Integer Numbers.
  • float double represents decimal numbers.
  • Except for char and boolean remaining data, types are signed(-ve and +ve) data types.
Literals:
Any constant value which can be assigned to the variables is called literal.
int x =10;
here int is a data type
     x is identifier/name of the variable
     10 is literal/constant value

Integer Literal values come in four flavors: binary, octal, decimal, hexadecimal:
  • Binary number system: A base-2 system, which uses only 2 digits(0,1).Literal values must start with 0b or 0B. e.g int x=0b100; int x= 0B100;
  • Octal number system: A base-8 system, which uses digits 0 through 7(0,1,2,3,4,5,6,7).Literal values must start with 0. e.g int x=053;
  • Decimal number system: A base-10 system, which uses digits 0 through 9(0,1,2,3,4,5,6,7,8,9). e.g int x=10;
  • Hexadecimal number system: A base-16 system, which uses digits 0 through 9 and the letters A through F.Literal values must start with 0x or 0X.
Notes:
  • By default, every integer literal value is int type but we can specify the explicitly long type by suffixing with (l or L).
  • Ex: int x=0x101; or int x =0X101;
  • By default, every floating-point literal value is a double type but we can specify explicitly it by suffixing with(d or D).
  • And for float type, we can specify explicitly by suffixing with(for F).
  • String literal values represent within double quotes " ".
  • char literal values represent within a single quote ' '.

Data Types Summary:

Data Type
Size(in Bytes)
Range
Wrapper Class
Default Value
Literal
byte
1
-128 to 127
Byte
0
0
short
2
-32768 to 32767
Short
0
0
int
4
-2147483648 to 2147483647
Integer
0
0
long
8

Long
0
0L
float
4

Float
0.0
0.0F or 0.0f
double
8

Double
0.0
0.0D or 0.0d
char
2
0 to 65535
Character
Blank space
' '
boolean
NA
NA
Boolean
false
true or false


Java 7 introduced the use of underscores as part of the literal values.
To make them more readable. And to use you have to follow below rules;
Rules:
  • You can place an underscore right after the prefix 0. which is used to define an octal literal value.
  • You can't start or end a literal value with an underscore.
  • You can't place an underscore right after the prefixes 0b,0B,0x,0X, which are used to define binary and hexadecimal literal values.
  • You can't place an underscore prior to an L,l, D,d, F,f suffix.
  • You can't place an underscore adjacent to the decimal point.
  • Example float x = 10._48F;// invalid  float x =10_.48F;// invalid
  • You can't use an underscore in positions where a string of digits is expected.
  • Example int i = Integer.parseInt("10_15"); // invalid
Note:
  • The literal value for all types of object reference variables is null.
  • In other words, the default value of object reference variables is null.
Character(Unsigned integer)
  • char is an unsigned integer.
  • Its range is 0 to 65535.
  • You can represent a char literal by 16 bits unicode( '\uxxxx')
  • For ex: '\u0000' represent 0 and '\uffff' represent 65535.
  • Every escape character in java acts as char literal.
Escape Character
Description
\n
Newline
\t
Horizontal tab
\r
Carriage return
\f
Form feed
\b
Blank space
\'
Single quote
\''
Double quote
\\
Backspace

Supporting topic: Wrapper Class
Next Topic: OPERATORS