Variables and Data Types
Variables and Data Types
By
Amarinder Kaur
Asst. Professor, LPU
Outlines [Expected Time: 1 Hours]
.
2. Character datatype
A boolean variable can hold one of the two values: true and false
4. float g= 34.5//error if we are writing in this way JVM convert into double
so float g= 34.5f
Automatic type promotion in expressions
byte b = 50;
b = b * 2; // Error! Cannot assign an int to a byte!
The code is attempting to store 50 * 2, a perfectly valid byte value, back into a
byte variable. However, because the operands were automatically promoted to
int when the expression was evaluated, the result has also been promoted to
int.
Thus, the result of the expression is now of type int, which cannot be assigned
to a byte without the use of a cast.
Type Conversion, Casting and Promotion
Note: Not all the types are compatible,Ex. boolean is not compatible with numeric or char
types.
Type Conversion, Casting and Promotion
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is truncated)
Char c=‘4’;
Short s=(short)c;
Short s=78;
Char c=(char)s;
Type Conversion, Casting and Promotion
int i = 65;
char ch = (char)i;
System.out.println(ch); // ch is character A
When a floating-point value is cast into a char, the floating-point value is first cast into
an int, which is then cast into a char
char ch = (char)65.25; // decimal 65 is assigned to ch
System.out.println(ch); // ch is character A
Implicit casting can be used if the result of a casting fits into the target
variable. Otherwise, explicit casting must be used.
• int i = 'a'; //correct
• byte b = i; // incorrect byte b = (byte) i; //correct
Type Conversion, Casting and Promotion
All numeric operators can be applied to char operands. A char operand is automatically
cast into a number if the other operand is a number or a character. If the other operand
is a string, the character is concatenated with the string.
class Test
{ public static void main(String [] ar)
{
int i = '2' + '3'; // (int)'2' is 50 and (int)'3' is 51
System.out.println("i is " + i); // i is 101
int j = 2 + 'a'; // (int)'a' is 97
System.out.println("j is " + j); // j is 99
System.out.println(j + " is the Unicode for character "+ (char)j);
System.out.println("Chapter " + '2');
}
}
class Promote{
public static void main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println("result = " + result);
}
}
Conversion from int to byte
• Actual method:
example : int i=300 ;
byte b=(byte)i;
Represent i into 32 bits then take 8 bits from right side and that will be
our output
Shortcut:
Output:
(If positive number)-256
(if negative number)+ 256
Exmple:300-256=44
-218+256=38
Examples:
• Int to byte:
1. int x=130;
byte b =(byte)x
SOP(b);
2. int x=132;
byte b =(byte)x
SOP(b);
3. int x=150;
byte b =(byte)x
SOP(b);
Continue…
4. int x=257;
byte b =(byte)x
SOP(b);
5. int x=-482;
byte b =(byte)x
SOP(b);
6. int x=-996;
byte b =(byte)x
SOP(b);
Casting Double to Byte
Float to int
1. float a=3.14f;
int x=(int) a
SOP(x);
2.Int i =132;
short s=15;
byte b=(byte)i;
int x=b+s
SOP(x);
Double to char
1.double d=65.25;
char c =(char)d;
SOP(c);
1. int i = 50000;
short s=i;
SOP(s);
Byte to short and vice versa
1.byte b =123;
short s =999;
s=b; //implicit conversion
SOP(s);
2. byte b =123;
short s =999;
b=s; //explicit conversion
SOP(b);
1. int i = ‘2’; //implicit converted into char to int
SOP(i);
2. Char a= ’49’; // error because 49 is not a single char. Consider
//collection of char
3. int i=50;
char c=(char)i;
System.out.println("hello world "+c);
4. Int a=5,b=6;
System.out.println(a+b+"hello world "+a+b);
int i=50;
char c=(char)i;
short s;
s=(short)c;
class VariableDemo
{
public static void main(String [] rk)
{
public int x = 10;
System.out.print(x);
}
}
Brainstorming 2
What will be the output of the following Program?
class VariableDemo
{
static int x;
public static void main(String [] rk)
{
int x;
System.out.print(x);
}
}
Brainstorming 3
What will be the output of the following Program?
class VariableDemo
{
static int x;
public static void main(String [] rk)
{
int x;
System.out.print(VariableDemo.x);
}
}
Brainstorming 3
What will be the output of the following Program?
class VariableDemo
{
int x;
public static void main(String [] rk)
{
int x;
System.out.print(this.x); //Class variable
}
}