Practical code to understand the Primitive types in java, to know theoretical part follow my post here,Basic Java Fundamentals
package in.technoscience; class hello { byte b; short s; int i; long l; float f; double d; char ch; boolean b1; float f1 = 10.9f; void show1() { System.out.println("instance Variables default value demo :"); System.out.println(b); System.out.println(s); System.out.println(i); System.out.println(l); System.out.println(f); System.out.println(d); System.out.println(ch); System.out.println(b1); System.out.println(f1); } void show2() { System.out.println("Integer literal demo :"); int a = 12; int b = 012; int c = 0x12; int c1 = 0x12a; byte d = 012; // when you will check .class file using decompiler you will find it has been replaced as follow: //int a = 12; // int b = 10; //int c = 18; // int c1 = 298; // byte d = 10; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(c1); System.out.println(d); } void show3() { System.out.println("Floating point literal demo :"); float f1 = 99.9f; double d1 = 9.9e-9; double d2 = 9.9E+9;
// when you will check .class file using decompiler you will find it has been replaced as follow: //float f1 = 99.9F; //double d1 = 9.8999999999999993E-009D; //double d2 = 9900000000D;
System.out.println(f1); System.out.println(d1); System.out.println(d2); } void show4() { System.out.println("Character literal demo :"); char ch1 = 'A'; int x1 = 'A'; char ch2 = ' '; int x2 = ' '; char ch3 = '1'; int x3 = '1'; char ch = ' ';
System.out.println(ch1); System.out.println(x1); System.out.println(ch2); System.out.println(x2); System.out.println(ch3); System.out.println(x3); System.out.println(ch); } void show5() { System.out.println("Boolean literal demo :"); boolean bb1 = false; boolean bb2 = true; boolean bb3 = true; System.out.println(bb1); System.out.println(bb2); System.out.println(bb3); } void show6() { System.out.println("string literal demo :"); String str1 = ""; String str2 = "123 abc + "; String str3 = " 1";
// when you will check .class file using decompiler you will find it has been replaced as follow: // char ch1 = 'A'; // int x1 = 65; //ASCII value according to type we have used for value // char ch2 = ' '; // int x2 = 32; // char ch3 = '1'; // int x3 = 49; // char ch = ' ';
System.out.println("Str1=" + str1); System.out.println(str1.length()); System.out.println("Str2=" + str2); System.out.println(str2.length()); System.out.println("Str3=" + str3); System.out.println(str3.length()); } } class lab2 { public static void main(String as[]) { hello h = new hello(); h.show1(); h.show2(); h.show3(); h.show4(); h.show5(); h.show6(); } }
// when you will check .class file using decompiler you will find it has been replaced as follow: //System.out.println((new StringBuilder("Str1=")).append(str1).toString()); //System.out.println(str1.length()); // System.out.println((new StringBuilder("Str2=")).append(str2).toString()); // System.out.println(str2.length()); //System.out.println((new StringBuilder("Str3=")).append(str3).toString()); // System.out.println(str3.length());
Output :
Instance variables default demo :
0
0
0
0
0.0
0.0
false
10.9
0
0
0
0
0.0
0.0
false
10.9
Integer literal demo :
12
10
18
298
10
Floating point literal demo :
99.9
9.9E-9
9.9E9
99.9
9.9E-9
9.9E9
Character literal demo :
A
65
32
1
49
A
65
32
1
49
Boolean literal demo :
false
true
true
false
true
true
string literal demo :
Str1=
0
Str2=123 abc +
10
Str3= 1
7
Str1=
0
Str2=123 abc +
10
Str3= 1
7
No comments:
Post a Comment