programming in java lecture-3 (a). java data type typedescription byte8 bit signed integer short16...
TRANSCRIPT
Programming in java
Lecture-3 (a)
Java Data TypeType Description
byte 8 bit signed integer
short 16 but signed integer
int 32 bit signed integer
long 64 bit signed integer
float 32 bit signed real number
double 64 bit signed real number
char 16 bit Unicode character (ASCII and beyond)
boolean 1 bit true or false value
String A sequence of characters between double quotes ("")
Boolean data type This data type can store only two values; true and
false. Declaring a boolean variable is the same as declaring any other primitive data type like int, float, char.
boolean response = false; //Valid boolean answer = true; //Valid boolean answer = 9943; //Invalid, boolean response = “false”; // Invalid,
This is return type for relational & conditional operators.
Comments // One line comment
/* multiple line Comment */
/*** These comment used in documentation *This is comment. *This is comment*/
What should be in comment? Author name Purpose of function
Default values for primitive members
When a primitive type data is a member of a class, it’s guaranteed to get a default value even if you don’t initialize it.
Not true for those local variables!! There will be compile
error if you use it without initialization
Primitive type Default
boolean false
char ‘\u0000’ (null)
byte (byte)0
short (short)0
int 0
long 0L
float 0.0f
double 0.0d
String (+) OperatorString Concatenation
"Now is " + "the time."
"Now is the time."
String (+) OperatorAutomatic Conversion to a String
If either expression_1If either expression_1 or expression_2 evaluatesto a string the other will be converted to a string if needed. The result will be their concatenation.
expression_1 + expression_2
String (+) OperatorAutomatic Conversion with Primitives
"The number is " + 4
"The number is " + "4"
"The number is 4"
Increment Operator • i + + first use the value of i and then increment
it by 1.
‘i + +’ equivalent to ‘i = (i)+1’. // postfix increment
• + + i first increment the value of i by 1 and then use it.
‘+ + i’ equivalent to ‘i = (i+1)’. // prefix increment
int i=2;
• System.out.print(“ i = ”+ i++);//print 2, then i becomes 3
• System.out.print(“ i = ”+ ++i);//add 1 to i, then print 4
Decrement Operator
• i - - first use the i ’s value and then decrement it by one
i - - equivalent to (i)-1. // postfix decrement
• - - i first decrement i ’s value by one and then use it.
- - i equivalent to (i-1). // prefix decrement
int i=5; System.out.print(“i = ” + i--);//print 5, then i
becomes 4 System.out.print(“i = ” + --i);//subtract 1 from i,
then print 3
Relational Operator a < b a less than b. (true/false)
a <= b a less than or equal b. (true/false)
a > b a greater than b. (true/false)
a >= b a greater than or equal to b. (true/false)
These operations always return a boolean value.
System.out.println(“23 is less than 65 ” +23<65); // true
System.out.println(“5 is greater than or equal to 25.00?” + 5>=25.00); // false
Equality Operator a = = b a equal to b. (true/false)
a ! = b a not equal to b. (true/false)
boolean equal = 12 = = 150; // false
boolean again_equal = ‘r’= = ‘r’); // true
boolean not_equal = 53!=90); // true
Logical (&&) Operator Examples
public class Example {public static void main(String[] args) {
boolean t = true;boolean f = false;
System.out.println("f && f " + (f && f));System.out.println("f && t " + (f && t));System.out.println("t && f " + (t && f));System.out.println("t && t " + (t && t));
}}
> java Example f && f false f && t false t && f false t && t true >
Logical (||) Operator Examples
public class Example {public static void main(String[] args) {
boolean t = true;boolean f = false;
System.out.println("f || f " + (f || f));System.out.println("f || t " + (f || t));System.out.println("t || f " + (t || f));System.out.println("t || t " + (t || t));
}}
> java Example f || f false f || t true t || f true t || t true >
Logical (!) Operator Examples
public class Example {public static void main(String[] args) {
boolean t = true;boolean f = false;
System.out.println("!f " + !f);System.out.println("!t " + !t);
}}
> java Example !f true !t false >
Logical Operators (Bit Level) & | ^ ~
int a = 10; // 00001010 = 10int b = 12; // 00001100 = 12
a 00000000000000000000000000001010 10b 00000000000000000000000000001100 12a & b 00000000000000000000000000001000 8
a 00000000000000000000000000001010 10b 00000000000000000000000000001100 12a | b 00000000000000000000000000001110 14
a 00000000000000000000000000001010 10b 00000000000000000000000000001100 12a ^ b 00000000000000000000000000000110 6
a 00000000000000000000000000001010 10~a 11111111111111111111111111110101 -11
&AND
|OR
^XOR
~NOT
Shift Operators (Bit Level) << >> >>>
• Shift Left << Fill with Zeros
• Shift Right >> Based on Sign
• Shift Right >>> Fill with Zeros
Shift Operators << >>int a = 3; // ...00000011 = 3int b = -4; // ...11111100 = -4
a 00000000000000000000000000000011 3a << 2 00000000000000000000000000001100 12
b 11111111111111111111111111111100 -4b << 2 11111111111111111111111111110000 -16
<<Left
>>Right
a 00000000000000000000000000000011 3a >> 2 00000000000000000000000000000000 0
b 11111111111111111111111111111100 -4b >> 2 11111111111111111111111111111111 -1
Shift Operator >>> int a = 3; // ...00000011 = 3int b = -4; // ...11111100 = -4
>>>Right 0
a 00000000000000000000000000000011 3a >>> 2 00000000000000000000000000000000 0
b 11111111111111111111111111111100 -4b >>> 2 00111111111111111111111111111111 +big
Shift Operator Examplespublic class Example {public static void main(String[] args) {
int a = 3; // ...00000011 = 3int b = -4; // ...11111100 = -4
System.out.println("a<<2 = " + (a<<2));
System.out.println("b<<2 = " + (b<<2));
System.out.println("a>>2 = " + (a>>2));
System.out.println("b>>2 = " + (b>>2));
System.out.println("a>>>2 = " + (a>>>2));
System.out.println("b>>>2 = " + (b>>>2));}
}
> java Example a<<2 = 12 b<<2 = -16 a>>2 = 0 b>>2 = -1 a>>>2 = 0 b>>>2 = 1073741823 >
Shift Operator >>> and Automatic Arithmetic Promotion
byte a = 3; // 00000011 = 3byte b = -4; // 11111100 = -4byte c;c = (byte) a >>> 2c = (byte) b >>> 2
>>>RightFill 0
a 00000011 3a >>> 2 00000000000000000000000000000000 0c = (byte) 00000000 0
b 11111100 -4b >>> 2 00111111111111111111111111111111 1073741823c = (byte) Much to big for byte 11111111 -1
Type Casting When a large data size variable is assigned to
small data size variable.
Declaration Size and Type Number
Range
byte b; // 8 bit integer -2-7 to +2+7-1
short s; // 16 bit integer -2-15 to +2+15-1
char c; // 16 bit Unicode 0 to +2+16
int i; // 32 bit integer -2-31 to +2+31-1
long l; // 64 bit integer -2-63 to +2+63-1
float f; // 32 bit floating point IEEE754 Standard
double d; // 64 bit floating point IEEE754 Standard
The Primitive Numbers
Casting the Primitives
b = (byte)s;b = (byte)c;b = (byte)i;b = (byte)l;b = (byte)f;b = (byte)d;
i = (int)b;i = (int)s;i = (int)c;i = (int)l;i = (int)f;i = (int)d;
l = (long)b;l = (long)s;l = (long)c;l = (long)i;l = (long)f;l = (long)d;
s = (short)b;s = (short)c;s = (short)i;s = (short)l;s = (short)f;s = (short)d;
2's
Co
mp
lem
ent
c = (char)b;c = (char)s;c = (char)i;c = (char)l;c = (char)f;c = (char)d;P
osi
tive
On
ly
f = (float)b;f = (float)s;f = (float)c;f = (float)i;f = (float)l;f = (float)d;
d = (double)b;d = (double)s;d = (double)c;d = (double)i;d = (double)l;d = (double)f;F
loat
ing
Po
int
b = (byte)s;b = (byte)c;b = (byte)i;b = (byte)l;b = (byte)f;b = (byte)d;
i = b;i = s;i = c;i = (int)l;i = (int)f;i = (int)d;
l = b;l = s;l = c;l = i;l = (long)f;l = (long)d;
s = b;s = (short)c;s = (short)i;s = (short)l;s = (short)f;s = (short)d;
2's
Co
mp
lem
ent
c = (char)b;c = (char)s;c = (char)i;c = (char)l;c = (char)f;c = (char)d;P
osi
tive
On
ly
f = b;f = s;f = c;f = i;f = l;f = (float)d;
d = b;d = s;d = c;d = i;d = l;d = f;F
loat
ing
Po
int
Acceptable Implicit Casts
char16 bit
double64 bit
float32 bit
long64 bit
int32 bit
short16 bit
byte8 bit
Illegal b = l;l = f;c = s;
OKl = b;i = c;f = l
char16 bit
double64 bit
float32 bit
long64 bit
int32 bit
short16 bit
byte8 bit
char16 bit
double64 bit
float32 bit
long64 bit
int32 bit
short16 bit
byte8 bit
Automatic Promotion with Arithmetic
Illegal Castss = s + b;s = s + s;
OKs = (short)(s + b);s = (short)(s + s);
Arithmetic is never donein 8 or 16 bit containers.
char16 bit
double64 bit
float32 bit
long64 bit
int32 bit
short16 bit
byte8 bit
Arithmetic Promotion with Mixed Primitives
Illegal Castsi = i + l;f = f + d;l = l + f;
OKi = (int)(i + l);d = (double)(f + d);l = (long)(l + f);
Arithmetic is donein the "widest" type.
Implicit Casts in Method Calls
char16 bit
double64 bit
float32 bit
long64 bit
int32 bit
short16 bit
byte8 bit
Illegal i = st.indexOf(f);
OKi = st.indexOf(c);i = st.indexOf(b);
For: String st; and,public int indexOf(int ch);
Program(find errors in following program)Class one { public static void main(String arp[]) { int i; Byte b=20; b=b+1; System.out.println(i); }}
Integer i is not initialised. Every local variable must be initialised.
b is automatically converted into integer data type and integer value can’t be assigned to byte variable
int a=12;float f=12.2f;double d=4.5;
double result = a * f + d ;
float type
a * f
= int * float + double
= float * float + double
= float + double
= double + double
= double