« Java DataTypes

Java DataTypes

1int a = 10;

Here int says what kind of data we are going to store in the storage. int also says how big or large we want the storage to be. For example in case of int we are taking space of 4 bytes which means 32 bits. 8 bits means 1 byte.

int -> 4 bytes

short -> 2 bytes

long -> 8 bytes

bytes -> 1 byte

More the bytes you have bigger the number we will be able to store.

1int i = 10.5; //error

We can't store a float in integer data type.

1double e = 20.5;
2float f = 13.5f;

For storing decimal numbers we have 2 options double and float. More the bytes you have bigger the number and better the precision we will be able to store.

datatypes_java.jpg

1int i;
2System.out.println(i);
3i = 20;

In languages like C++, it will print some garbage value but in case of java, it will not allow you to use a variable before it is initialized.

1System.out.println(i); //error: cannot find symbol
2int i;
3i = 20;
1int 1i;

Variable name should start with a alphabet or underscore. In java, you cannot start your variable name with numbers and also they cannot contain any other special character except underscore (_) and dollar ($).

1int Var;
2int var;

Variable names are case sensitive. Above two are different variables.

1int j = 10 / 3;
2System.out.println(j);
3j = 10 / 30;
4System.out.println(j);
5j = 3 * 2 / 5;
6System.out.println(j);
7j = 3 * (2 / 5);
8System.out.println(j);
9double k = 10 / 3;
10System.out.println(k);

double k = 10 / 3; will give 3.0 as 10 and 30 are integers so will give integer output which will be stored into double. 3 * 2 / 5 for operators having same priority will be evaluated from left to right. Brackets have the highest priority.

Taking input from user

1int l, m;
2Scanner s = new Scanner(System.in);
3l = s.nextInt();
4m = s.nextInt();
5int o = l + m;
6System.out.println(o);
7double p = s.nextDouble();
8System.out.println(p);
9float q = s.nextFloat();
10System.out.println(q);
11long r = s.nextLong();
12System.out.println(r);
13
14String str = s.next();
15System.out.println(str);
16
17String str1 = s.next();
18char ch = str1.charAt(0);
19System.out.println(ch);
20
21String strr = s.nextLine();
22System.out.println(strr);

We have to mention where do we want Scanner to fetch input from. We can ask Scanner to input from a file as well. Here we needed Scanner to fetch input from console so System.in is mentioned. System.in is input stream. If you ask for a int using s.nextInt() but scanner found a string then it would raise a inputmismatch exception. As these is no s.nextChar so for inputting char there is a totally different way.

1import java.util.Scanner;
2
3public class DataTypes {
4 public static void main(String[] args) {
5 int a = 10;
6 long b = 20;
7 short c = 30;
8 byte d = 40;
9
10 double e = 20.5;
11 float f = 13.5f;
12
13 char g = 'a';
14 boolean h = false;
15
16 // System.out.println(i);
17 int i;
18 // System.out.println(i);
19 i = 20;
20 System.out.println(i);
21
22 /// int 1i; //not allowed
23
24 int Var;
25 int var;
26
27 int j = 10 / 3;
28 System.out.println(j);
29 j = 10 / 30;
30 System.out.println(j);
31 j = 3 * 2 / 5;
32 System.out.println(j);
33 j = 3 * (2 / 5);
34 System.out.println(j);
35 double k = 10 / 3;
36 System.out.println(k);
37
38 int l, m;
39 Scanner s = new Scanner(System.in);
40 l = s.nextInt();
41 m = s.nextInt();
42 int o = l + m;
43 System.out.println(o);
44 double p = s.nextDouble();
45 System.out.println(p);
46 float q = s.nextFloat();
47 System.out.println(q);
48 long r = s.nextLong();
49 System.out.println(r);
50
51 String str = s.next();
52 System.out.println(str);
53
54 String str1 = s.next();
55 char ch = str1.charAt(0);
56 System.out.println(ch);
57
58 String strr = s.nextLine();
59 System.out.println(strr);
60
61 }
62}