X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

boolean :-

boolean is boolean data type which stores logical values allowing true or false as the value. The size of boolean in Java is virtual machine dependent. The boolean implemented by using a general purpose register called flip-flop.

Example 1 :

class Example { public static void main(String [ ] args) { boolean b = true; System.out.println(b); } }

Output :

true

Example 2 :

class Example { public static void main(String [ ] args) { boolean b = True; System.out.println(b); } }

Output :

The above program display compile time error because conversion between byte & character is not implicit. Compile time error : cannot find symbol symbol : variable True location : class Example

Example 3 :

class Example { public static void main(String [ ] args) { boolean True = true; boolean b = True; System.out.println(b); } }

Output :

true

char :-

char is 2 bytes character data types. The range of char is 0 to 65535 (UNICODE) i.e. '\u0000' to '\uffff'

In old languages like C & C++, we can use only ASCII (American Standard Code For Information Interchange) characters and to represent all ASCII characters 8 bits are enough. Hence, char size is 1 byte.

But in Java, we uses UNICODE (Universal Code) characters which covers worldwide all alphabet sets. Hence, Java char size is 2 bytes. Java programming language is available in 18 International languages (English, Latin, Greek etc.).

Example 1 :

class Example { public static void main(String [ ] args) { byte b1 = 65; char c = b1; System.out.println(b1); System.out.println(c); } }

Output :

The above program display compile time error because conversion between byte & character is not implicit. Compile time error : possible loss of precision required : char found : byte

Example 2 :

class Example { public static void main(String [ ] args) { byte b1 = 65; char c = (char) b1; System.out.println(b1); System.out.println(c); } }

Output :

65 A

Byte :-

Byte is 1 byte (i.e. 8 bits) integral data type. It accept value which ranges from -128 to +127.

Example 1 :

class Example { public static void main(String [ ] args) { byte b1 = 100; byte b2 = 200; System.out.println(b1 + " " + b2); } }

Output :

The above program display compile time error because '200' is not within the byte range, it is of integer value. Compile time error : possible loss of precision required : byte found : int

Example 2 :

class Example { public static void main(String [ ] args) { byte b1 = 100; byte b2 = 20; byte b3 = b1 / b2; System.out.println(b3); } }

Output :

The above program display compile time error because any arithmetic operation on byte, result it to be of integer type.The conversion between integer and byte is not implicit. Compile time error : possible loss of precision required : byte found : int

Note :

class Example { public static void main(String [ ] args) { byte b1 = 10 + 20; System.out.println(b1); } }

Output :

30


Here, 30 is a constant & within the byte range. Any arithmetic operation on integer constants, result can be byte, short, int or long.

Example 3 :

class Example { public static void main(String [ ] args) { byte b1 = 100 * 20; System.out.println(b1); } }

Output :

The above program display 'possible loss of precision' which shows that the result exceeds the byte range. Compile time error : possible loss of precision required : byte found : int

short :-

short is also an integral data type whose size is 2 bytes. Range of short is -32768 to 32767.

Example :

class Example { public static void main(String [ ] args) { byte b1 = 65; short s1 = 100; int i1 = b1; int i2 = s1; System.out.println(b1); System.out.println(s1); System.out.println(i1); System.out.println(i2); } }

Output :

65 100 65 100

int :-

int is an integral data type whose size is 4 bytes. Range of int is -2147483648 to 2147483647 i.e. -231 to 231-1

It is most commonly used data type.

In C programming language the size of int varies from platform to platform. For 16-bit processor it is 2 bytes but for 32-bit processor it is 4 bytes. The main advantage of this approach is that the read and write operations can be perform very efficiently. But the disadvantage of this approach is that the chance of failing of C program is very high if we are changing the underlying platform and hence, C programming is not considered as Robust.

But in Java, the size of int is always 4 bytes irrespective of any platform. The main advantage of this approach is that the chance of failing of Java program is very less if we are changing the underlying platform and hence, Java is considered as Robust. The main disadvantage of this approach is that the read and write operations will become costly and performance will be reduced.

Example 1 :

class Example { public static void main(String [ ] args) { System.out.println(Integer.MAX_VALUE); } }

Output :

2147483647

Example 2 :

class Example { public static void main(String [ ] args) { int a = 10; System.out.println(a); } }

Output :

10

long :-

long is an integral data type whose size is 8 bytes. Range of long is -9223372036854775808 to 9223372036854775807 i.e. -263 to 263-1

Example 1 :

public class Example { public static void main(String [ ] args) { int x = 100; byte b = 65; char c = 'B'; long l1 = x; long l2 = b; long l3 = c; System.out.println(l1); System.out.println(l2); System.out.println(l3); } }

Output :

100 65 66

Example 2 :

class Example { public static void main(String [ ] args) { long l1 = 100; long l2 = 65536; long l3 = 2147483647; long l4 = 2147483649L; System.out.println(l1); System.out.println(l2); System.out.println(l3); System.out.println(l4); } }

Output :

100 65536 2147483647 2147483649

float :-

float is a floating point data type having single precision whose size is 4 bytes. float value must be prefix with F or f. Range of float is -3.4e38 to 3.4e38. If we want 5 to 6 decimal places of accuracy then we should use float data type.

Example :

class Example { public static void main(String [ ] args) { int x = 100; byte b = 65; char ch = 'A'; float f1 = 1.5f; float f2 = x; float f3 = b; float f4 = ch; System.out.println(f1); System.out.println(f2); System.out.println(f3); System.out.println(f4); } }

Output :

1.5 100.0 65.0 65.0

double :-

double is floating point data type which have double precision whose size is 8 bytes. Range of double is -1.7e308 to 1.7e308. If we want 14 to 15 decimal places of accuracy then we should use double data type.

Example :

class Example { public static void main(String [ ] args) { int x = 100; byte b = 65; char ch = 'A'; float f = 1.5F; long l = 100; double d1 = 1.25; double d2 = x; double d3 = ch; double d4 = f; System.out.println(d1); System.out.println(d2); System.out.println(d3); System.out.println(d4); } }

Output :

1.25 100.0 65 1.5


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.