DATA TYPES & LITERALS
DATA TYPES:
Every variable in java should have some type. Every expression should have some type and every type is strictly defined.
Every Assignment should be checked by the compiler for type
compatibility Hence java language is considered as strongly typed programming
language
Java is a not pure object oriented programming language.
Because several OOPs features not supported by java. Like multiple inheritances,
operator overloading, etc.
Moreover we are using primitive datatypes in java which are
not objects
Primitive Data types:
Primitive Datatypes (8)
Numeric Datatypes character datatype Boolean
datatype
(To represent number) (To represent character) (To represent
Boolean values)
Integral Datatypes
Floating Point Datatypes
Byte float
Short double
Int
Long
Except Boolean and char all remaining data types are
considered as signed datatypes. Because
we can represent both positive and negative numbers.
1. Byte:
Size : 8 bits
Max value : +127
Min Value : -128
Range : -128 to +127
MSB bit(sing bit 0-positive,1-negative)
The most significant bit acts as sign bit .positive numbers
will be represent directly in the memory where negative numbers are will be
represented in 2’s complement form.
Byte b=10;//valid
Byte b=130;//CE : possible loss of precision
Found: int Required : byte
Byte b=10.4;//CE : possible loss of precision
Byte b=-129;//CE:
If we type cast to byte b=(byte) -129;
Output: 127
Byte b=”Sahitya”;//incompatible types
Byte b=true;//incompatible types
Byte b=’c’;//o/p: ASCII value of ‘c’ i.e :99
Byte b=(int) 128;//CE : can’t convert into to byte
Byte b=’all’;//CE: unclosed character literal
Byte b=faq;//ce: can’t find symbol
Byte b=@;//CE : illegar start of expression
Byte Data type is best suitable if we want to handle data in
terms of streams either from the file or from the N/W.
2. Short:
The most rarely used datatype in java is
short datatype.
Size : 2 Bytes[16 bits]
Range : -2 pow 15 to 2 pow 15-1[-32768 to
+32767]
Short s=-32768;//valid
Short s=-32769//o/p : +32767
Short s=’o’; //o/p : ASCII value of ‘o’ i.e
111
Short s=”sahitya”;//incompatible types
Short s=true;//incompatible types
Short s=(byte)200;//o/p -56
3. Int:
Most commonly used datatype in java
Size : 4 Bytes[32 bits]
Range : -2 pow 31 to +2 pow 31-1
[-2147483648 to +2147483647]
Ex: int I
=-2147483649;//ce: literal is out of range
int I =(int)-2147483649;//ce:
literal is out of range
int i=(short)32768;//o/p:-32768
int i=(byte)128;//o/p :
-128
4.LONG:
Sometimes ‘int’ may not be enough to hold
big values then we should go for long datatypes.
Ex: i. to represent the amount of distance
travelled by light in thousand days. Int is not enough. We should go for long
datatypes.
Ex: long l=30000000000X60X60X24X1000;
ii.to represent no of characters present
inside a file ‘int’ may not enough we should go for long. Hence the return type
of length method is long data type.
Long l=f.lenght();
Size : 8 Bytes[64 bits]
Range : -2pow 63 to +2 pow 63-1
[9223372036854775807 to
-9223372036854775808
]
Ex:
long lo=9223372036854775807;//CE :
the literal ‘xxx07’ of type int is out of range
this is because : when you
type in a number in java , the compiler automatically reads it as an integer
There are specific suffixes for long e.g
:39829L, float (e.g : 2.4f) and double e.g:-7.83d
If there
is no suffix, and it is an integral type (e.g.
5623
), it is assumed to be an int
. If it is not an integral type (e.g. 3.14159
), it is assumed to be a double
.
n all
other cases (
byte
, short
, char
), you need the cast as there is no specific
suffix.
The Java
spec allows both upper and lower case suffixes, but the upper case version for
long
s is preferred
long lo=9223372036854775807L;
5.
Float :
if we want 5 to 6 decimal
places of accuracy then we should go for float.
Float follows single
precession.
Size : 4 bytes
Range : -3.4e38 to +3.4e38
6.
Double :
If we want 14 to 15
decimal places of accuracy then we should go for double
Double follows double
precision.
Size : 8 bytes
Range : -1.7e308 to
1.7e308
7.
Boolean:
Size : NA[JVM Dependent]
Range : NA[allowed values are true
/false ]
Boolean b=0;//CE : incompatible types
Boolean b=True;//CE : cannot find symbol
8.
Char:
Old languages like c & CPP are
ASCIII code based. The number of ASCII characters are less than or equal to
256. To represent these 256 characters 8 bits are enough. Hence ‘char’ size in
both language is one byte.
But java is Unicode based the number of
Unicode characters allowed in java are less than or equals to 65535.
To represent these many characters we
should go for 16 bits. Hence java’s char size is 2 Bytes.
Range : 0 to 65535
Data type
|
Size
|
Range
|
Corresponding wrapper class
|
Default value
|
Byte
|
1Byte
|
-2pow7 to +2pow7-1[-128 to +127]
|
Byte
|
0
|
Short
|
2 Bytes
|
-2pow15 to +2pow15-1[-32768 to +32767]
|
Short
|
0
|
Int
|
4 bytes
|
-2pow31 to +2 pow 31-1[-2147483648 to +2147483647]
|
Integer
|
0
|
Float
|
4 Bytes
|
-3.4e38 to +3.5e38
|
Float
|
0.0
|
Double
|
8 Bytes
|
-1.7e308 to +1.7e308
|
Double
|
0.0
|
Boolean
|
NA
|
True or flase
|
Boolean
|
false
|
Char
|
2 Bytes
|
0 to 65535
|
Character
|
0[represent space character]
|
The default Data type for object reference is ‘null’
LITERALS
Any constant value which can be assigned to a variable is called a literal.
Ex : int x=10;[10 is constant value/literal, int is datatype or keyword, x is variable name or identifier]
1. Integral Literals:
by default every integral literal is int type .
by default every integral literal is int type .
For the Integer datatypes we can specify literal values in the following ways.
i. decimal Literals : allowed digits are : 0 to 9
Ex : int x=10;
ii. octal Literals : allowed digits are : 0 to 7
The literal value should prefer with ‘0’
Ex : int x=010;
iii. Hexadecimal literal;
allowed digits are 0 to 9, a to f[A to F]
allowed digits are 0 to 9, a to f[A to F]
Literal value should be prefixed with 0x/0X
Ex : int x=0X10;
iv. Binary Literals: 1.7 Version Enhancements w.r.t literals
The allowed digits are ‘0’ & ‘1’
Literal value should be prefixed with ‘0B/0b’
Ex : int x=0B10;
Syso(x);//o/p : 2
2. Floating point Literals:
By default every floating point literal is double type. But we can specify explicitly as float type. By suffixed with ‘f/F’.
Ex: float f=123.456; CE: possible loss of precision found: double require : float
Float f=123.456f; //valid
Double d=123.456//valid
We can specify floating point literal as double type. By suffixing with d/D .of course this is not required.
We can’t specify floating point literals in octal and hexadecimal form.
Ex: double d=01234.455//valid : Compiler treats as decimal
Double d=0x123.45;//CE: malformed floating point literal
Double d=0xface;//valid : integer literal
Double d=0786;//CE : integer literal too large
We can assign integer literals directly to floating point literals and that integral literal can be specified either in octal decimal or hexadecimal form,
But we can’t assign floating point literals to the integral types.
Ex : int x=123.435;//CE : possible loss of precision.
We can specify floating point literal in exponential form also(scientific notation)
Double d=1.2e3;//valid
Float f=12.3e4f;//valid
3. Boolean Literals:
The only allowed values for the Boolean data type are true/false[lower case]
Ex: Boolean b=true;
Boolean b=0;//CE : incompatible types found : int require: Boolean
Boolean b=True;//CE: can ‘t find symbol symbol: variable True
4. char Literals:
A char literal can be represented as single character within single quotes.
Ex: char ch=’a’;
Char ch=”a”;//CE : incompatible types
Char ch=a;//CE : can’t find a symbol symbol: variable a location : class xxx
Char ch=’ab’;//CE: unclosed character literal
A char literal can be represented as integral literal which represents Unicode of the character.
The integral literal can be specified either in decimal or octal or hexa decimal form.
The allowed range is 0 to 65535
Ex: char ch=97;//integral
Char ch=0xFace;//valid
Char ch=0777;//valid
We can represent a char literal by using Unicode representation which is nothing but ‘\uxxxx’.
Ex: char ch=’\u0061’;//o/p a
Escape Characters act as ‘char’ literal.
Char ch=’\t’;
Char ch=’\m’;//CE:
Escape character
|
Description
|
\n
|
New line character
|
\t
|
Horizontal tab
|
\r
|
Carriage return
|
\b
|
Backspace character
|
\f
|
Form feed
|
\\
|
Backslash character
|
\’
|
Single quote character
|
\”
|
Double quote
|
Char ch=’\ufacer’;//invalid Unicode
Char ch=’\uface’;//valid
5. String Literals:
Any sequence of characters within double quotes is called String literal.
String s=”ocjp”;
1.7 Version :
Usage of underscore in numeric literals:
Ex: double d=123456.789;
Double d=1_23_456.7_8_9;
Or double d=1_____2_2343.9;
The main advantage of ‘_’ is for readability. After compilation ‘_’ will be removed automatically .hence the above lines will become double d=122343.9;
We have to use ‘_’ symbol only between digits.
Byte---short
Int-long-flaot-double
Char
Long and float are same but internal representation is different.
1. Literal Values for All primitives:
A primitive literal is merely a source code representation of the primitive data types.
Ex: ‘b’ //char literal
42 // int literal
False //Boolean literal
2343.345 //double literal
i. Integer Literals:
4 ways to represent
i. Decimal (base 10)
ii. Octal (base 8)
iii. HexaDecimal (base 16)
iv. Binary(base 2) added in java 7
Numeric Literals with Underscore(added in Java 7):
java 7 numeric literals can be declared using underscore character(_) to improve readability.
java 7 numeric literals can be declared using underscore character(_) to improve readability.
Ex:
Int pre7=1000000;
Int with7=1_00_00_00;
Note: you can’t use the underscore literal at the beginning or end of the literal.
You can use the underscore character for any of the numeric types including double and float. But for double and floats, you can’t add underscore character directly next to the decimal point.
i. Decimal Literals:
Ex: int length=32;
ii. Binary Literals:
Add in Java 7
Represent with digits 0 and 1. Must start with either 0b or 0B.
Int b1=0B0101010;
Int b2=0b0101010;
iii. OctalLiterals:
use only the digits 0 to 7.in java we represent an integer in octal by placing a zero infront of the number
Ex:
Int six=06;
Int seven =07;
Int eight=010;
Int nine=011;
You can have upt 21 digits in an octal number, not including the leading zero.
iv. HexaDecimal Literals:
These are constructued using 16 distinct symbols. Form number 10 to 15 we use alphabetic characters to represent these digis
Counting from 0 to 15 like this
0 1 2 3 4 5 6 7 8 9 a b c d e f
Java can accept upper case or lowercase letters for the extra digits. You are allowed up to 16 digits in a hexadecimal number, not including the prefix 0x or 0X or the optional suffix extension L.
Ex:
Int x=0x0001;
Int y=0X7fffffff;
Int z=oxDeadCafe;
Sos(x+””+y+””z);
0xCAFE and oxcafe are both legal and have the same value.
All four integer literals are defined as int by default, but they may also be specified as long by placing a suffix of L or l after the number.
Long jo=11090L;
Long so=0xFFFFl;
ii. Floating point Literals:
Floating point numbers are defined as a number, a decimal symbol and more numbers representing the fraction. In the following example the number 11301874.9881024 is the literal value.
Double d=11301874.9881024;
Floating point literals are defined as double(64 bits) by default. So if you want to assign a floating –point literal to variable of type float(32 bits) you must attach the suffix f or F to the number.
Possible loss of precision is you are trying to fit a number into a less precise container. The ‘F’ suffix gives you a way to tell the compiler that I know what I am doing, and I will take the risk
Float f=23.234567;//compiler error
Float f=23.234567F;//compiles fine
You may optionally attach ‘D’ or ‘d’ but it is not necessary it is the default behaviour.
iii. Boolean Literals:
Source code representation of Boolean values. A Boolean value can be true or false.
Boolean t=true;
Boolean f=0//illegal
Ex: int x=1;
If(x){}//compiler error
iv. Character Literal:
Char literal is represented by a single character in single quotes
Char a=’a’;
Char b=’@’;
Can also represent in Unicode notation of prefixing the value with '\u'[should be small]
Char letterN=’\u004E’;//the letter “N”
Characters are 16 bit unsigned integers under the hood. That means you can assing a number liter, assuming it will fit into the unsinged 16 bit range (0 to 65535)
Ex:
Char a=0x892;//hexadecimal literal
Char b=982;//int literal
Char c=(char)7000;//the cast is required 7000 is out of range
Char d=(char) -98;//Ridiculous, but legal
Char e=-29;//CE:possible loss of precision; needs a cast
Char f=7000;//CE: possible loss of precision; needs a cast
You can also use an escape code if you wan to represent a character that can’t be typed in as a litera. Including for linefeed, new line, horizontal tab, backspace and quotes
Char c=’\”’;//a double quote
Char d=’\n’;//new line
Char tab=’\t’;//a tab
Literal Values for Strings:
Source code representation of value of String object.
Two ways we can represent
String s=”Bill Joy”;
Syso(“Bill”+” ”+”Joy”);
Though the strings are not primitive but still they can be represented as literals. And the other type that has a literal representation is an array
Ex:
class LiteralsDemo
{
public static void main(String[] args)
{
//Integer Literals..4 bytes[range: ]
//Decimal, octal, hexadecimal, binary
//Decimal
int x=1234567;
//ocatal : start with 0 and should be 0-7 range
int x1=0177;//CE: integer number too large
//hexadecimal : start with 0x: rangge 0-9 and a-f
int x2=0xcab90;
long x22=0xcabl;
System.out.println(x2);
//binary : only 0 and 1
int x3=0b011__010110_011;
System.out.println(x3);
//Floating point literals float and double
//float f=123.456;//CE: PSP
float f=123.456f;//compiles fine
double d=0xface;//we can assing integral type of literals to the floating point literals
double d1=01245;
double d2=0b010101010;
System.out.println("d2 "+d2);
//character literals
//we can assing integral values within the range of 0 to 65535
//range [a - z] [97 - 122 ],[A - Z] [65- 90]
char ch=0xface;
System.out.println(ch);
char ch1='\u004E';//unicode character representration u should be small
int i='A';
System.out.println(i);
System.out.println(ch1);
//samples
long l=2345;
System.out.println(l);
}
}
class LiteralsDemo
{
public static void main(String[] args)
{
//Integer Literals..4 bytes[range: ]
//Decimal, octal, hexadecimal, binary
//Decimal
int x=1234567;
//ocatal : start with 0 and should be 0-7 range
int x1=0177;//CE: integer number too large
//hexadecimal : start with 0x: rangge 0-9 and a-f
int x2=0xcab90;
long x22=0xcabl;
System.out.println(x2);
//binary : only 0 and 1
int x3=0b011__010110_011;
System.out.println(x3);
//Floating point literals float and double
//float f=123.456;//CE: PSP
float f=123.456f;//compiles fine
double d=0xface;//we can assing integral type of literals to the floating point literals
double d1=01245;
double d2=0b010101010;
System.out.println("d2 "+d2);
//character literals
//we can assing integral values within the range of 0 to 65535
//range [a - z] [97 - 122 ],[A - Z] [65- 90]
char ch=0xface;
System.out.println(ch);
char ch1='\u004E';//unicode character representration u should be small
int i='A';
System.out.println(i);
System.out.println(ch1);
//samples
long l=2345;
System.out.println(l);
}
}
Comments
Post a Comment